summaryrefslogtreecommitdiff
blob: 9ecaeb70e16e916f2a442be9335cd36bc58d23ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
http://bugs.gentoo.org/90186

If we have entries in MANPATH that are really symlinks to other entries,
then many man functions will yield duplicate entries.

Without this patch, we see this behavior:
$ echo $MANPATH
/usr/share/man:/usr/man
$ man --path
/usr/share/man:/usr/man
$ ls -ld /usr/share/man /usr/man
lrwxrwxrwx  1 /usr/man -> /usr/share/man
drwxr-xr-x 36 /usr/share/man
$ man -k passwd
passwd               (1)  - change user password
passwd               (1)  - change user password

With this patch, we get:
$ echo $MANPATH
/usr/share/man:/usr/man
$ man --path
/usr/share/man
$ ls -ld /usr/share/man /usr/man
lrwxrwxrwx  1 /usr/man -> /usr/share/man
drwxr-xr-x 36 /usr/share/man
$ man -k passwd
passwd               (1)  - change user password

--- man-1.6c/src/manpath.c
+++ man-1.6c/src/manpath.c
@@ -380,6 +380,44 @@
      }
 }
 
+void trim_symlinked_manpaths (void);
+void
+trim_symlinked_manpaths () {
+	/*
+	 * Skip symlinks to other entries in path.
+	 * Do this after we've built the entire list.
+	 */
+	struct stat *stat_cache;
+	size_t i, j, size;
+
+	if (!mandirlist)
+		return;
+
+	for (size = 0; mandirlist[size]; ++size)
+		/* count # of elements */;
+	if (size == 0)
+		return;
+	/* cache stat information for every element */
+	stat_cache = (struct stat *) my_malloc (size * sizeof(*stat_cache));
+	for (i = 0; i < size; ++i)
+		stat(mandirlist[i], &stat_cache[i]);
+
+#define EQU_STAT(s,d) ((s).st_dev == (d).st_dev && (s).st_ino == (d).st_ino)
+	for (i = 0; i < size; ++i) {
+		for (j = i+1; j < size; ++j) {
+			if (EQU_STAT(stat_cache[i], stat_cache[j])) {
+				/* these two entries are the same, so cut out the second one */
+				memmove(mandirlist+j, mandirlist+j+1, (size-j)*sizeof(*mandirlist));
+				memmove(stat_cache+j, stat_cache+j+1, (size-j)*sizeof(*stat_cache));
+				mandirlist[--size] = NULL;
+				--j;
+			}
+		}
+	}
+
+	free(stat_cache);
+}
+
 void
 init_manpath () {
      static int done = 0;
@@ -391,6 +431,7 @@
               (manp = getenv ("MANPATH")) == NULL)
 	       manp = "";		/* default path */
 	  split (manp, to_mandirlist, 0);
+	  trim_symlinked_manpaths ();
 	  done = 1;
      }
 }