2 * filenames.c: compose and dissect man page file names
4 * Copyright (C) 1994, 1995 Graeme W. Wilford. (Wilf.)
5 * Copyright (C) 2001, 2002 Colin Watson.
7 * This file is part of man-db.
9 * man-db is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * man-db is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with man-db; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #endif /* HAVE_CONFIG_H */
32 #include "xvasprintf.h"
35 #define _(String) gettext (String)
37 #include "manconfig.h"
41 #include "db_storage.h"
43 #include "filenames.h"
45 static void gripe_bogus_manpage (const char *manpage)
48 error (0, 0, _("warning: %s: ignoring bogus filename"),
52 char *make_filename (const char *path, const char *name,
53 struct mandata *in, const char *type)
58 name = in->name; /* comes from dblookup(), so non-NULL */
60 file = xasprintf ("%s/%s%s/%s.%s", path, type, in->sec, name, in->ext);
62 if (in->comp && *in->comp != '-') /* Is there an extension? */
63 file = appendstr (file, ".", in->comp, NULL);
65 debug ("Checking physical location: %s\n", file);
66 if (!CAN_ACCESS (file, R_OK)) {
74 /* Fill in a mandata structure with information about a file name.
75 * file is the name to examine. info points to the structure to be filled
76 * in. req_name is the page name that was requested.
78 * Returns either a pointer to the buffer which the fields in info point
79 * into, to be freed by the caller, or NULL on error. The buffer will
80 * contain either three or four null-terminated strings: the directory name,
81 * the base of the file name in that directory, the section extension, and
82 * optionally the compression extension (if COMP_SRC is defined).
84 * Only the fields name, ext, sec, and comp are filled in by this function.
85 * name is only set if it differs from req_name; otherwise it remains at
88 char *filename_info (const char *file, struct mandata *info,
91 char *manpage = xstrdup (file);
92 char *slash = strrchr (manpage, '/');
95 memset (info, 0, sizeof (struct mandata));
98 struct compression *comp;
102 *slash = '\0'; /* strip '/base_name' */
103 base_name = slash + 1;
107 /* Bogus files either have (i) no period, ie no extension, (ii)
108 a compression extension, but no sectional extension, (iii)
109 a missmatch between the section they are under and the
110 sectional part of their extension. */
113 comp = comp_info (base_name, 1);
115 info->comp = comp->ext;
116 *(base_name + strlen (comp->stem)) = '\0';
120 #else /* !COMP_SRC */
122 #endif /* COMP_SRC */
125 char *ext = strrchr (base_name, '.');
127 /* no section extension */
128 gripe_bogus_manpage (file);
132 *ext++ = '\0'; /* set section ext */
135 /* zero-length section extension */
136 gripe_bogus_manpage (file);
142 info->sec = strrchr (manpage, '/') + 4; /* set section name */
144 if (strlen (info->sec) >= 1 && strlen (info->ext) >= 1 &&
145 info->sec[0] != info->ext[0]) {
146 /* mismatch in section */
147 gripe_bogus_manpage (file);
152 if (req_name && !STREQ (base_name, req_name))
153 info->name = xstrdup (base_name);