]>
git.cameronkatri.com Git - mandoc.git/blob - manpath.c
1 /* $Id: manpath.c,v 1.3 2011/11/24 10:44:56 kristaps 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.
32 #define MAN_CONF_FILE "/etc/man.conf"
33 #define MAN_CONF_KEY "_whatdb"
35 static void manpath_add(struct manpaths
*, const char *);
38 manpath_parse(struct manpaths
*dirs
, char *defp
, char *auxp
)
41 if (NULL
!= getenv("MANPATH"))
42 defp
= getenv("MANPATH");
45 manpath_parseconf(dirs
);
47 manpath_parseline(dirs
, defp
);
49 manpath_parseline(dirs
, auxp
);
53 * Parse a FULL pathname from a colon-separated list of arrays.
56 manpath_parseline(struct manpaths
*dirs
, char *path
)
63 for (dir
= strtok(path
, ":"); dir
; dir
= strtok(NULL
, ":"))
64 manpath_add(dirs
, dir
);
68 * Add a directory to the array, ignoring bad directories.
69 * Grow the array one-by-one for simplicity's sake.
72 manpath_add(struct manpaths
*dirs
, const char *dir
)
78 if (NULL
== (cp
= realpath(dir
, buf
)))
81 for (i
= 0; i
< dirs
->sz
; i
++)
82 if (0 == strcmp(dirs
->paths
[i
], dir
))
85 dirs
->paths
= mandoc_realloc
87 ((size_t)dirs
->sz
+ 1) * sizeof(char *));
89 dirs
->paths
[dirs
->sz
++] = mandoc_strdup(cp
);
93 manpath_parseconf(struct manpaths
*dirs
)
100 /* Open manpath(1). Ignore errors. */
102 stream
= popen("manpath", "r");
109 /* Read in as much output as we can. */
112 buf
= mandoc_realloc(buf
, bsz
+ 1024);
113 sz
= fread(buf
+ (int)bsz
, 1, 1024, stream
);
117 if ( ! ferror(stream
) && feof(stream
) &&
118 bsz
&& '\n' == buf
[bsz
- 1]) {
120 manpath_parseline(dirs
, buf
);
126 manpath_manconf(MAN_CONF_FILE
, dirs
);
131 manpath_free(struct manpaths
*p
)
135 for (i
= 0; i
< p
->sz
; i
++)
142 manpath_manconf(const char *file
, struct manpaths
*dirs
)
148 keysz
= strlen(MAN_CONF_KEY
);
151 if (NULL
== (stream
= fopen(file
, "r")))
154 while (NULL
!= (p
= fgetln(stream
, &len
))) {
155 if (0 == len
|| '\n' != p
[--len
])
158 while (isspace((unsigned char)*p
))
160 if (strncmp(MAN_CONF_KEY
, p
, keysz
))
167 if (NULL
== (q
= strrchr(p
, '/')))
170 manpath_add(dirs
, p
);