]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.4 2011/11/26 22:38:11 schwarze Exp $ */
3 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
33 #define MAN_CONF_FILE "/etc/man.conf"
34 #define MAN_CONF_KEY "_whatdb"
36 static void manpath_add(struct manpaths
*, const char *);
39 manpath_parse(struct manpaths
*dirs
, char *defp
, char *auxp
)
42 manpath_parseline(dirs
, auxp
);
45 defp
= getenv("MANPATH");
48 manpath_parseconf(dirs
);
50 manpath_parseline(dirs
, defp
);
54 * Parse a FULL pathname from a colon-separated list of arrays.
57 manpath_parseline(struct manpaths
*dirs
, char *path
)
64 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
65 manpath_add(dirs
, dir
);
69 * Add a directory to the array, ignoring bad directories.
70 * Grow the array one-by-one for simplicity's sake.
73 manpath_add(struct manpaths
*dirs
, const char *dir
)
79 if (NULL
== (cp
= realpath(dir
, buf
)))
82 for (i
= 0; i
< dirs
->sz
; i
++)
83 if (0 == strcmp(dirs
->paths
[i
], dir
))
86 dirs
->paths
= mandoc_realloc
88 ((size_t)dirs
->sz
+ 1) * sizeof(char *));
90 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
94 manpath_parseconf(struct manpaths
*dirs
)
101 /* Open manpath(1). Ignore errors. */
103 stream
= popen("manpath", "r");
110 /* Read in as much output as we can. */
113 buf
= mandoc_realloc(buf
, bsz
+ 1024);
114 sz
= fread(buf
+ (int)bsz
, 1, 1024, stream
);
118 if ( ! ferror(stream
) && feof(stream
) &&
119 bsz
&& '\n' == buf
[bsz
- 1]) {
121 manpath_parseline(dirs
, buf
);
127 manpath_manconf(MAN_CONF_FILE
, dirs
);
132 manpath_free(struct manpaths
*p
)
136 for (i
= 0; i
< p
->sz
; i
++)
143 manpath_manconf(const char *file
, struct manpaths
*dirs
)
149 keysz
= strlen(MAN_CONF_KEY
);
152 if (NULL
== (stream
= fopen(file
, "r")))
155 while (NULL
!= (p
= fgetln(stream
, &len
))) {
156 if (0 == len
|| '\n' != p
[--len
])
159 while (isspace((unsigned char)*p
))
161 if (strncmp(MAN_CONF_KEY
, p
, keysz
))
168 if (NULL
== (q
= strrchr(p
, '/')))
171 manpath_add(dirs
, p
);