2 * ult_src.c: Find the ultimate source of a page
4 * Copyright (C) 1994, 1995 Graeme W. Wilford. (Wilf.)
5 * Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007, 2008, 2009, 2010, 2011,
8 * This file is part of man-db.
10 * man-db is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * man-db is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with man-db; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 * code to seek out the original (ultimate) source man file for
25 * any specified man file. Soft and hard links and .so inclusions
26 * are traced. Use: reduce amount of cat files to a minimum.
28 * Mon May 2 11:14:28 BST 1994 Wilf. (G.Wilford@ee.surrey.ac.uk)
33 #endif /* HAVE_CONFIG_H */
39 #include <sys/types.h>
46 #include "canonicalize.h"
49 #include "xvasprintf.h"
52 #define _(String) gettext (String)
54 #include "manconfig.h"
57 #include "decompress.h"
62 /* Find minimum value hard link filename for given file and inode.
63 * Returns a newly allocated string.
65 static char *ult_hardlink (const char *fullpath, ino_t inode)
68 struct dirent *manlist;
69 char *base, *dir, *ret;
72 slash = strrchr (fullpath, '/');
74 dir = xstrndup (fullpath, slash - fullpath);
75 base = xstrdup (++slash);
80 error (0, errno, _("can't search directory %s"), dir);
86 while ((manlist = readdir (mdir))) {
87 if (manlist->d_ino == inode &&
88 strcmp (base, manlist->d_name) > 0) {
90 base = xstrdup (manlist->d_name);
91 debug ("ult_hardlink: (%s)\n", base);
96 /* If we already are the link with the smallest name value */
99 if (strcmp (base, slash) == 0) {
105 ret = xasprintf ("%s/%s", dir, base);
111 /* Resolve all symbolic links within 'fullpath'.
112 * Returns a newly allocated string.
114 static char *ult_softlink (const char *fullpath)
118 resolved_path = canonicalize_file_name (fullpath);
119 if (!resolved_path) {
120 /* discard the unresolved path */
124 _("warning: %s is a dangling symlink"),
127 error (0, errno, _("can't resolve %s"),
133 debug ("ult_softlink: (%s)\n", resolved_path);
135 return resolved_path;
138 /* Test 'buffer' to see if it contains a .so include. If so and it's not an
139 * absolute filename, return newly allocated string whose contents are the
142 static char *test_for_include (const char *buffer)
147 /* strip out any leading whitespace (if any) */
148 while (CTYPE (isspace, *buffer))
151 /* see if the `command' is a .so */
152 if (strncmp (buffer, ".so", 3) == 0) {
155 /* strip out any whitespace between the command and
157 while (CTYPE (isspace, *buffer))
160 /* If .so's argument is an absolute filename, it could be
161 * either (i) a macro inclusion, (ii) a non local manual page
162 * or (iii) a (somewhat bogus) reference to a local manual
165 * If (i) or (ii), we must not follow the reference. (iii) is
166 * a problem with the manual page, thus we don't want to
167 * follow any absolute inclusions in our quest for the
168 * ultimate source file */
169 if (*buffer != '/') {
170 const char *end = buffer;
171 while (*end && !CTYPE (isspace, *end))
173 return xstrndup (buffer, end - buffer);
179 static char *find_include (const char *name, const char *path,
186 /* Restore the original path from before ult_softlink() etc., in
187 * case it went outside the mantree.
189 ret = xasprintf ("%s/%s", path, include);
191 /* If the original path from above doesn't exist, try to create new
192 * path as if the "include" was relative to the current man page.
194 if (CAN_ACCESS (ret, F_OK))
197 dirname = dir_name (name);
198 temp_file = xasprintf ("%s/%s", dirname, include);
201 if (CAN_ACCESS (temp_file, F_OK)) {
202 /* Just plain include. */
204 ret = canonicalize_file_name (temp_file);
206 /* Try globbing - the file suffix might be missing. */
207 char *temp_file_asterisk = xasprintf ("%s*", temp_file);
208 char **candidate_files = expand_path (temp_file_asterisk);
211 free (temp_file_asterisk);
212 if (CAN_ACCESS (candidate_files[0], F_OK)) {
214 ret = canonicalize_file_name (candidate_files[0]);
216 for (i = 0; candidate_files[i]; i++)
217 free (candidate_files[i]);
218 free (candidate_files);
225 static void ult_trace (struct ult_trace *trace, const char *s)
229 if (trace->len >= trace->max) {
231 trace->names = xnrealloc (trace->names, trace->max,
234 trace->names[trace->len++] = xstrdup (s);
237 void free_ult_trace (struct ult_trace *trace)
240 for (i = 0; i < trace->len; ++i)
241 free (trace->names[i]);
246 * recursive function which finds the ultimate source file by following
247 * any ".so filename" directives in the first line of the man pages.
248 * Also (optionally) traces symlinks and hard links(!).
250 * name is full pathname, path is the MANPATH directory (/usr/man)
251 * flags is a combination of SO_LINK | SOFT_LINK | HARD_LINK
253 const char *ult_src (const char *name, const char *path,
254 struct stat *buf, int flags, struct ult_trace *trace)
256 static char *base; /* must be static */
257 static short recurse; /* must be static */
259 /* initialise the function */
265 trace->names = XNMALLOC (trace->max, char *);
267 ult_trace (trace, name);
270 /* as ult_softlink() & ult_hardlink() do all of their respective
271 * resolving in one call, only need to sort them out once
277 base = xstrdup (name);
279 debug ("\nult_src: File %s in mantree %s\n", name, path);
281 /* If we don't have a buf, allocate and assign one */
282 if (!buf && ((flags & SOFT_LINK) || (flags & HARD_LINK))) {
284 if (lstat (base, buf) == -1) {
286 error (0, errno, _("can't resolve %s"),
292 /* Permit semi local (inter-tree) soft links */
293 if (flags & SOFT_LINK) {
294 assert (buf); /* initialised above */
295 if (S_ISLNK (buf->st_mode)) {
296 /* Is a symlink, resolve it. */
297 char *softlink = ult_softlink (base);
306 /* Only deal with local (inter-dir) HARD links */
307 if (flags & HARD_LINK) {
308 assert (buf); /* initialised above */
309 if (buf->st_nlink > 1) {
310 /* Has HARD links, find least value */
311 char *hardlink = ult_hardlink (base,
321 /* keep a check on recursion level */
322 else if (recurse == 10) {
324 error (0, 0, _("%s is self referencing"), name);
328 if (flags & SO_LINK) {
336 if (stat (base, &st) < 0) {
337 struct compression *comp = comp_file (base);
342 comp->stem = NULL; /* steal memory */
345 error (0, errno, _("can't open %s"),
352 /* base may change for recursive calls to ult_src, but
353 * decompress_open doesn't keep its own copy.
355 decomp_base = xstrdup (base);
356 decomp = decompress_open (decomp_base);
359 error (0, errno, _("can't open %s"), base);
363 pipeline_start (decomp);
365 /* make sure that we skip over any comments */
367 buffer = pipeline_readline (decomp);
368 } while (buffer && STRNEQ (buffer, ".\\\"", 3));
370 include = test_for_include (buffer);
376 base = find_include (name, path, include);
379 debug ("ult_src: points to %s\n", base);
382 /* Take a copy; it's unwise to pass base directly to
383 * a recursive call, as it may be freed.
385 new_name = xstrdup (base);
386 ult = ult_src (new_name, path, NULL, flags, trace);
390 pipeline_wait (decomp);
391 pipeline_free (decomp);
396 pipeline_wait (decomp);
397 pipeline_free (decomp);
401 /* We have the ultimate source */
403 ult_trace (trace, base);