2 * Copyright (c) 2002 John Rochester
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/utsname.h>
49 #define DEFAULT_MANPATH "/usr/share/man"
51 #define TOP_LEVEL_DIR 0 /* signifies a top-level man directory */
52 #define MAN_SECTION_DIR 1 /* signifies a man section directory */
53 #define UNKNOWN 2 /* signifies an unclassifiable directory */
55 #define TEST_EXISTS 0x01
57 #define TEST_FILE 0x04
58 #define TEST_READABLE 0x08
59 #define TEST_WRITABLE 0x10
60 #define TEST_EXECUTABLE 0x20
62 static int verbose; /* -v flag: be verbose with warnings */
63 static int pretend; /* -n, -p flags: print out what would be done
64 instead of actually doing it */
65 static int force; /* -f flag: force overwriting all cat pages */
66 static int rm_junk; /* -r flag: remove garbage pages */
67 static char *locale; /* user's locale if -L is used */
68 static char *lang_locale; /* short form of locale */
69 static const char *machine, *machine_arch;
70 static int exit_code; /* exit code to use when finished */
73 * -T argument for nroff
75 static const char *nroff_device = "ascii";
78 * Mapping from locale to nroff device
80 static const char *locale_device[] = {
82 "ISO8859-1", "latin1",
83 "ISO8859-15", "latin1",
87 #define BZ2_CMD "bzip2"
88 #define BZ2_EXT ".bz2"
89 #define BZ2CAT_CMD "bz"
93 enum Ziptype {NONE, BZIP, GZIP};
96 static gid_t gids[NGROUPS_MAX];
98 static int starting_dir;
99 static char tmp_file[MAXPATHLEN];
103 * A hashtable is an array of chains composed of this entry structure.
109 struct hash_entry *next;
112 #define HASHTABLE_ALLOC 16384 /* allocation for hashtable (power of 2) */
113 #define HASH_MASK (HASHTABLE_ALLOC - 1)
115 static struct hash_entry *visited[HASHTABLE_ALLOC];
116 static struct hash_entry *links[HASHTABLE_ALLOC];
119 * Inserts a string into a hashtable keyed by inode & device number.
122 insert_hashtable(struct hash_entry **table,
127 struct hash_entry *new_entry;
128 struct hash_entry **chain;
130 new_entry = (struct hash_entry *) malloc(sizeof(struct hash_entry));
131 if (new_entry == NULL)
132 err(1, "can't insert into hashtable");
133 chain = &table[inode_number & HASH_MASK];
134 new_entry->inode_number = inode_number;
135 new_entry->device_number = device_number;
136 new_entry->data = data;
137 new_entry->next = *chain;
142 * Finds a string in a hashtable keyed by inode & device number.
145 find_hashtable(struct hash_entry **table,
149 struct hash_entry *chain;
151 chain = table[inode_number & HASH_MASK];
152 while (chain != NULL) {
153 if (chain->inode_number == inode_number &&
154 chain->device_number == device_number)
162 trap_signal(int sig __unused)
164 if (tmp_file[0] != '\0')
170 * Deals with junk files in the man or cat section directories.
173 junk(const char *mandir, const char *name, const char *reason)
176 fprintf(stderr, "%s/%s: %s\n", mandir, name, reason);
178 fprintf(stderr, "rm %s/%s\n", mandir, name);
179 if (!pretend && unlink(name) < 0)
180 warn("%s/%s", mandir, name);
185 * Returns TOP_LEVEL_DIR for .../man, MAN_SECTION_DIR for .../manXXX,
186 * and UNKNOWN for everything else.
189 directory_type(char *dir)
194 p = strrchr(dir, '/');
195 if (p == NULL || p[1] != '\0')
203 if (strncmp(p, "man", 3) == 0) {
206 return TOP_LEVEL_DIR;
207 while (isalnum((unsigned char)*p) || *p == '_') {
209 return MAN_SECTION_DIR;
216 * Tests whether the given file name (without a preceding path)
217 * is a proper man page name (like "mk-amd-map.8.gz").
218 * Only alphanumerics and '_' are allowed after the last '.' and
219 * the last '.' can't be the first or last characters.
222 is_manpage_name(char *name)
224 char *lastdot = NULL;
228 if (!isalnum((unsigned char)*n)) {
247 return lastdot > name && lastdot + 1 < n;
251 is_bzipped(char *name)
253 int len = strlen(name);
254 return len >= 5 && strcmp(&name[len - 4], BZ2_EXT) == 0;
258 is_gzipped(char *name)
260 int len = strlen(name);
261 return len >= 4 && strcmp(&name[len - 3], GZ_EXT) == 0;
265 * Converts manXXX to catXXX.
268 get_cat_section(char *section)
272 cat_section = strdup(section);
273 strncpy(cat_section, "cat", 3);
278 * Tests to see if the given directory has already been visited.
281 already_visited(char *mandir, char *dir, int count_visit)
285 if (stat(dir, &st) < 0) {
287 warn("%s/%s", mandir, dir);
293 if (find_hashtable(visited, st.st_ino, st.st_dev) != NULL) {
295 warnx("already visited %s/%s", mandir, dir);
297 warnx("already visited %s", dir);
301 insert_hashtable(visited, st.st_ino, st.st_dev, "");
306 * Returns a set of TEST_* bits describing a file's type and permissions.
307 * If mod_time isn't NULL, it will contain the file's modification time.
310 test_path(char *name, time_t *mod_time)
314 if (stat(name, &test_st) < 0)
316 result = TEST_EXISTS;
317 if (mod_time != NULL)
318 *mod_time = test_st.st_mtime;
319 if (S_ISDIR(test_st.st_mode))
321 else if (S_ISREG(test_st.st_mode))
323 if (test_st.st_uid == uid) {
324 test_st.st_mode >>= 6;
327 for (i = 0; i < ngids; i++) {
328 if (test_st.st_gid == gids[i]) {
329 test_st.st_mode >>= 3;
334 if (test_st.st_mode & S_IROTH)
335 result |= TEST_READABLE;
336 if (test_st.st_mode & S_IWOTH)
337 result |= TEST_WRITABLE;
338 if (test_st.st_mode & S_IXOTH)
339 result |= TEST_EXECUTABLE;
344 * Checks whether a file is a symbolic link.
347 is_symlink(char *path)
351 return lstat(path, &st) >= 0 && S_ISLNK(st.st_mode);
355 * Tests to see if the given directory can be written to.
358 check_writable(char *mandir)
360 if (verbose && !(test_path(mandir, NULL) & TEST_WRITABLE))
361 fprintf(stderr, "%s: not writable - will only be able to write to existing cat directories\n", mandir);
365 * If the directory exists, attempt to make it writable, otherwise
366 * attempt to create it.
369 make_writable_dir(char *mandir, char *dir)
373 if ((test = test_path(dir, NULL)) != 0) {
374 if (!(test & TEST_WRITABLE) && chmod(dir, 0755) < 0) {
375 warn("%s/%s: chmod", mandir, dir);
380 if (verbose || pretend)
381 fprintf(stderr, "mkdir %s\n", dir);
384 if (mkdir(dir, 0755) < 0) {
385 warn("%s/%s: mkdir", mandir, dir);
395 * Processes a single man page source by using nroff to create
396 * the preformatted cat page.
399 process_page(char *mandir, char *src, char *cat, enum Ziptype zipped)
401 int src_test, cat_test;
402 time_t src_mtime, cat_mtime;
403 char cmd[MAXPATHLEN];
406 const char *link_name;
408 src_test = test_path(src, &src_mtime);
409 if (!(src_test & (TEST_FILE|TEST_READABLE))) {
410 if (!(src_test & TEST_DIR)) {
411 warnx("%s/%s: unreadable", mandir, src);
413 if (rm_junk && is_symlink(src))
414 junk(mandir, src, "bogus symlink");
418 src_dev = test_st.st_dev;
419 src_ino = test_st.st_ino;
420 cat_test = test_path(cat, &cat_mtime);
421 if (cat_test & (TEST_FILE|TEST_READABLE)) {
422 if (!force && cat_mtime >= src_mtime) {
424 fprintf(stderr, "\t%s/%s: up to date\n",
431 * Is the man page a link to one we've already processed?
433 if ((link_name = find_hashtable(links, src_ino, src_dev)) != NULL) {
434 if (verbose || pretend) {
435 fprintf(stderr, "%slink %s -> %s\n",
436 verbose ? "\t" : "", cat, link_name);
439 link(link_name, cat);
442 insert_hashtable(links, src_ino, src_dev, strdup(cat));
443 if (verbose || pretend) {
444 fprintf(stderr, "%sformat %s -> %s\n",
445 verbose ? "\t" : "", src, cat);
449 snprintf(tmp_file, sizeof tmp_file, "%s.tmp", cat);
450 snprintf(cmd, sizeof cmd,
451 "%scat %s | tbl | nroff -T%s -man | col | %s > %s.tmp",
452 zipped == BZIP ? BZ2CAT_CMD : zipped == GZIP ? GZCAT_CMD : "",
454 zipped == BZIP ? BZ2_CMD : zipped == GZIP ? GZ_CMD : "cat",
456 if (system(cmd) != 0)
457 err(1, "formatting pipeline");
458 if (rename(tmp_file, cat) < 0)
464 * Scan the man section directory for pages and process each one,
465 * then check for junk in the corresponding cat section.
468 scan_section(char *mandir, char *section, char *cat_section)
470 struct dirent **entries;
471 char **expected = NULL;
477 char page_path[MAXPATHLEN];
478 char cat_path[MAXPATHLEN];
479 char zip_path[MAXPATHLEN];
482 * scan the man section directory for pages
484 npages = scandir(section, &entries, NULL, alphasort);
486 warn("%s/%s", mandir, section);
490 if (verbose || rm_junk) {
492 * Maintain a list of all cat pages that should exist,
493 * corresponding to existing man pages.
495 expected = (char **) calloc(npages, sizeof(char *));
497 for (i = 0; i < npages; free(entries[i++])) {
498 page_name = entries[i]->d_name;
499 snprintf(page_path, sizeof page_path, "%s/%s", section,
501 if (!is_manpage_name(page_name)) {
502 if (!(test_path(page_path, NULL) & TEST_DIR)) {
503 junk(mandir, page_path,
504 "invalid man page name");
508 zipped = is_bzipped(page_name) ? BZIP :
509 is_gzipped(page_name) ? GZIP : NONE;
510 if (zipped != NONE) {
511 snprintf(cat_path, sizeof cat_path, "%s/%s",
512 cat_section, page_name);
513 if (expected != NULL)
514 expected[nexpected++] = strdup(page_name);
515 process_page(mandir, page_path, cat_path, zipped);
518 * We've got an uncompressed man page,
519 * check to see if there's a (preferred)
522 snprintf(zip_path, sizeof zip_path, "%s%s",
524 if (test_path(zip_path, NULL) != 0) {
525 junk(mandir, page_path,
526 "man page unused due to existing " GZ_EXT);
530 "warning, %s is uncompressed\n",
533 snprintf(cat_path, sizeof cat_path, "%s/%s",
534 cat_section, page_name);
535 if (expected != NULL) {
536 asprintf(&expected[nexpected++],
539 process_page(mandir, page_path, cat_path, NONE);
544 if (expected == NULL)
547 * scan cat sections for junk
549 npages = scandir(cat_section, &entries, NULL, alphasort);
551 for (i = 0; i < npages; free(entries[i++])) {
552 const char *junk_reason;
555 page_name = entries[i]->d_name;
556 if (strcmp(page_name, ".") == 0 || strcmp(page_name, "..") == 0)
559 * Keep the index into the expected cat page list
560 * ahead of the name we've found.
562 while (e < nexpected &&
563 (cmp = strcmp(page_name, expected[e])) > 0)
567 /* we have an unexpected page */
568 snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section,
570 if (!is_manpage_name(page_name)) {
571 if (test_path(cat_path, NULL) & TEST_DIR)
573 junk_reason = "invalid cat page name";
574 } else if (!is_gzipped(page_name) && e + 1 < nexpected &&
575 strncmp(page_name, expected[e + 1], strlen(page_name)) == 0 &&
576 strlen(expected[e + 1]) == strlen(page_name) + 3) {
577 junk_reason = "cat page unused due to existing " GZ_EXT;
579 junk_reason = "cat page without man page";
580 junk(mandir, cat_path, junk_reason);
583 while (e < nexpected)
590 * Processes a single man section.
593 process_section(char *mandir, char *section)
597 if (already_visited(mandir, section, 1))
600 fprintf(stderr, " section %s\n", section);
601 cat_section = get_cat_section(section);
602 if (make_writable_dir(mandir, cat_section))
603 scan_section(mandir, section, cat_section);
608 select_sections(struct dirent *entry)
610 return directory_type(entry->d_name) == MAN_SECTION_DIR;
614 * Processes a single top-level man directory. If section isn't NULL,
615 * it will only process that section sub-directory, otherwise it will
616 * process all of them.
619 process_mandir(char *dir_name, char *section)
621 fchdir(starting_dir);
622 if (already_visited(NULL, dir_name, section == NULL))
624 check_writable(dir_name);
626 fprintf(stderr, "man directory %s\n", dir_name);
628 fprintf(stderr, "cd %s\n", dir_name);
629 if (chdir(dir_name) < 0) {
630 warn("%s: chdir", dir_name);
634 if (section != NULL) {
635 process_section(dir_name, section);
637 struct dirent **entries;
638 char *machine_dir, *arch_dir;
642 nsections = scandir(".", &entries, select_sections, alphasort);
644 warn("%s", dir_name);
648 for (i = 0; i < nsections; i++) {
649 process_section(dir_name, entries[i]->d_name);
650 asprintf(&machine_dir, "%s/%s", entries[i]->d_name,
652 if (test_path(machine_dir, NULL) & TEST_DIR)
653 process_section(dir_name, machine_dir);
655 if (strcmp(machine_arch, machine) != 0) {
656 asprintf(&arch_dir, "%s/%s", entries[i]->d_name,
658 if (test_path(arch_dir, NULL) & TEST_DIR)
659 process_section(dir_name, arch_dir);
669 * Processes one argument, which may be a colon-separated list of
673 process_argument(const char *arg)
682 err(1, "out of memory");
683 while ((dir = strsep(&parg, ":")) != NULL) {
684 switch (directory_type(dir)) {
686 if (locale != NULL) {
687 asprintf(&mandir, "%s/%s", dir, locale);
688 process_mandir(mandir, NULL);
690 if (lang_locale != NULL) {
691 asprintf(&mandir, "%s/%s", dir,
693 process_mandir(mandir, NULL);
697 process_mandir(dir, NULL);
700 case MAN_SECTION_DIR: {
701 mandir = strdup(dirname(dir));
702 section = strdup(basename(dir));
703 process_mandir(mandir, section);
709 warnx("%s: directory name not in proper man form", dir);
717 determine_locale(void)
721 if ((locale = setlocale(LC_CTYPE, "")) == NULL) {
722 warnx("-L option used, but no locale found\n");
725 sep = strchr(locale, '_');
726 if (sep != NULL && isupper((unsigned char)sep[1])
727 && isupper((unsigned char)sep[2])) {
728 asprintf(&lang_locale, "%.*s%s", (int)(sep - locale),
731 sep = nl_langinfo(CODESET);
732 if (sep != NULL && *sep != '\0' && strcmp(sep, "US-ASCII") != 0) {
735 for (i = 0; locale_device[i] != NULL; i += 2) {
736 if (strcmp(sep, locale_device[i]) == 0) {
737 nroff_device = locale_device[i + 1];
743 if (lang_locale != NULL)
744 fprintf(stderr, "short locale is %s\n", lang_locale);
745 fprintf(stderr, "nroff device is %s\n", nroff_device);
752 fprintf(stderr, "usage: %s [-fLnrv] [directories ...]\n",
758 main(int argc, char **argv)
762 if ((uid = getuid()) == 0) {
763 fprintf(stderr, "don't run %s as root, use:\n echo", argv[0]);
764 for (optind = 0; optind < argc; optind++) {
765 fprintf(stderr, " %s", argv[optind]);
767 fprintf(stderr, " | nice -5 su -m man\n");
770 while ((opt = getopt(argc, argv, "vnfLrh")) != -1) {
792 ngids = getgroups(NGROUPS_MAX, gids);
793 if ((starting_dir = open(".", 0)) < 0) {
797 signal(SIGINT, trap_signal);
798 signal(SIGHUP, trap_signal);
799 signal(SIGQUIT, trap_signal);
800 signal(SIGTERM, trap_signal);
802 if ((machine = getenv("MACHINE")) == NULL) {
803 static struct utsname utsname;
805 if (uname(&utsname) == -1)
807 machine = utsname.machine;
810 if ((machine_arch = getenv("MACHINE_ARCH")) == NULL)
811 machine_arch = MACHINE_ARCH;
813 if (optind == argc) {
814 const char *manpath = getenv("MANPATH");
816 manpath = DEFAULT_MANPATH;
817 process_argument(manpath);
819 while (optind < argc)
820 process_argument(argv[optind++]);