man: accept a list of dirs in $MANPATH

function                                             old     new   delta
add_MANPATH                                            -     143    +143
man_main                                             852     731    -121
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 143/-121)           Total: 22 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
diff --git a/miscutils/man.c b/miscutils/man.c
index 5c1fa2c..ccb57a9 100644
--- a/miscutils/man.c
+++ b/miscutils/man.c
@@ -147,15 +147,49 @@
 	return run_pipe(pager, man_filename, man, level);
 }
 
+static char **add_MANPATH(char **man_path_list, int *count_mp, char *path)
+{
+	if (path) while (*path) {
+		char *next_path;
+		char **path_element;
+
+		next_path = strchr(path, ':');
+		if (next_path) {
+			*next_path = '\0';
+			if (next_path++ == path) /* "::"? */
+				goto next;
+		}
+		/* Do we already have path? */
+		path_element = man_path_list;
+		if (path_element) while (*path_element) {
+			if (strcmp(*path_element, path) == 0)
+				goto skip;
+			path_element++;
+		}
+		man_path_list = xrealloc_vector(man_path_list, 4, *count_mp);
+		man_path_list[*count_mp] = xstrdup(path);
+		(*count_mp)++;
+		/* man_path_list is NULL terminated */
+		/* man_path_list[*count_mp] = NULL; - xrealloc_vector did it */
+ skip:
+		if (!next_path)
+			break;
+ next:
+		path = next_path;
+	}
+	return man_path_list;
+}
+
 int man_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int man_main(int argc UNUSED_PARAM, char **argv)
 {
 	parser_t *parser;
 	const char *pager = ENABLE_LESS ? "less" : "more";
-	char **man_path_list;
 	char *sec_list;
 	char *cur_path, *cur_sect;
-	int count_mp, cur_mp;
+	char **man_path_list;
+	int count_mp;
+	int cur_mp;
 	int opt, not_found;
 	char *token[2];
 
@@ -164,14 +198,20 @@
 	argv += optind;
 
 	sec_list = xstrdup("0p:1:1p:2:3:3p:4:5:6:7:8:9");
-	/* Last valid man_path_list[] is [0x10] */
+
 	count_mp = 0;
-	man_path_list = xzalloc(0x11 * sizeof(man_path_list[0]));
-	man_path_list[0] = getenv("MANPATH");
-	if (!man_path_list[0]) /* default, may be overridden by /etc/man.conf */
+	man_path_list = add_MANPATH(NULL, &count_mp,
+			getenv("MANDATORY_MANPATH"+10) /* "MANPATH" */
+	);
+	if (!man_path_list) {
+		/* default, may be overridden by /etc/man.conf */
+		man_path_list = xzalloc(2 * sizeof(man_path_list[0]));
 		man_path_list[0] = (char*)"/usr/man";
-	else
-		count_mp++;
+		/* count_mp stays 0.
+		 * Thus, man.conf will overwrite man_path_list[0]
+		 * if a path is defined there.
+		 */
+	}
 
 	/* Parse man.conf[ig] or man_db.conf */
 	/* man version 1.6f uses man.config */
@@ -193,35 +233,7 @@
 		if (strcmp("MANDATORY_MANPATH"+10, token[0]) == 0 /* "MANPATH"? */
 		 || strcmp("MANDATORY_MANPATH", token[0]) == 0
 		) {
-			char *path = token[1];
-			while (*path) {
-				char *next_path;
-				char **path_element;
-
-				next_path = strchr(path, ':');
-				if (next_path) {
-					*next_path = '\0';
-					if (next_path++ == path) /* "::"? */
-						goto next;
-				}
-				/* Do we already have path? */
-				path_element = man_path_list;
-				while (*path_element) {
-					if (strcmp(*path_element, path) == 0)
-						goto skip;
-					path_element++;
-				}
-				man_path_list = xrealloc_vector(man_path_list, 4, count_mp);
-				man_path_list[count_mp] = xstrdup(path);
-				count_mp++;
-				/* man_path_list is NULL terminated */
-				/*man_path_list[count_mp] = NULL; - xrealloc_vector did it */
- skip:
-				if (!next_path)
-					break;
- next:
-				path = next_path;
-			}
+			man_path_list = add_MANPATH(man_path_list, &count_mp, token[1]);
 		}
 		if (strcmp("MANSECT", token[0]) == 0) {
 			free(sec_list);