/usr.bin/catman/catman.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 812 lines · 631 code · 61 blank · 120 comment · 190 complexity · 83873fff408918165f441e66ac5b32f7 MD5 · raw file

  1. /*-
  2. * Copyright (c) 2002 John Rochester
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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
  16. *
  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.
  27. */
  28. #include <sys/cdefs.h>
  29. __FBSDID("$FreeBSD$");
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <sys/param.h>
  33. #include <sys/utsname.h>
  34. #include <ctype.h>
  35. #include <dirent.h>
  36. #include <err.h>
  37. #include <fcntl.h>
  38. #include <locale.h>
  39. #include <langinfo.h>
  40. #include <libgen.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #define DEFAULT_MANPATH "/usr/share/man"
  46. #define TOP_LEVEL_DIR 0 /* signifies a top-level man directory */
  47. #define MAN_SECTION_DIR 1 /* signifies a man section directory */
  48. #define UNKNOWN 2 /* signifies an unclassifiable directory */
  49. #define TEST_EXISTS 0x01
  50. #define TEST_DIR 0x02
  51. #define TEST_FILE 0x04
  52. #define TEST_READABLE 0x08
  53. #define TEST_WRITABLE 0x10
  54. static int verbose; /* -v flag: be verbose with warnings */
  55. static int pretend; /* -n, -p flags: print out what would be done
  56. instead of actually doing it */
  57. static int force; /* -f flag: force overwriting all cat pages */
  58. static int rm_junk; /* -r flag: remove garbage pages */
  59. static char *locale; /* user's locale if -L is used */
  60. static char *lang_locale; /* short form of locale */
  61. static const char *machine, *machine_arch;
  62. static int exit_code; /* exit code to use when finished */
  63. /*
  64. * -T argument for nroff
  65. */
  66. static const char *nroff_device = "ascii";
  67. /*
  68. * Mapping from locale to nroff device
  69. */
  70. static const char *locale_device[] = {
  71. "KOI8-R", "koi8-r",
  72. "ISO8859-1", "latin1",
  73. "ISO8859-15", "latin1",
  74. NULL
  75. };
  76. #define BZ2_CMD "bzip2"
  77. #define BZ2_EXT ".bz2"
  78. #define BZ2CAT_CMD "bz"
  79. #define GZ_CMD "gzip"
  80. #define GZ_EXT ".gz"
  81. #define GZCAT_CMD "z"
  82. enum Ziptype {NONE, BZIP, GZIP};
  83. static uid_t uid;
  84. static int starting_dir;
  85. static char tmp_file[MAXPATHLEN];
  86. static struct stat test_st;
  87. /*
  88. * A hashtable is an array of chains composed of this entry structure.
  89. */
  90. struct hash_entry {
  91. ino_t inode_number;
  92. dev_t device_number;
  93. const char *data;
  94. struct hash_entry *next;
  95. };
  96. #define HASHTABLE_ALLOC 16384 /* allocation for hashtable (power of 2) */
  97. #define HASH_MASK (HASHTABLE_ALLOC - 1)
  98. static struct hash_entry *visited[HASHTABLE_ALLOC];
  99. static struct hash_entry *links[HASHTABLE_ALLOC];
  100. /*
  101. * Inserts a string into a hashtable keyed by inode & device number.
  102. */
  103. static void
  104. insert_hashtable(struct hash_entry **table,
  105. ino_t inode_number,
  106. dev_t device_number,
  107. const char *data)
  108. {
  109. struct hash_entry *new_entry;
  110. struct hash_entry **chain;
  111. new_entry = (struct hash_entry *) malloc(sizeof(struct hash_entry));
  112. if (new_entry == NULL)
  113. err(1, "can't insert into hashtable");
  114. chain = &table[inode_number & HASH_MASK];
  115. new_entry->inode_number = inode_number;
  116. new_entry->device_number = device_number;
  117. new_entry->data = data;
  118. new_entry->next = *chain;
  119. *chain = new_entry;
  120. }
  121. /*
  122. * Finds a string in a hashtable keyed by inode & device number.
  123. */
  124. static const char *
  125. find_hashtable(struct hash_entry **table,
  126. ino_t inode_number,
  127. dev_t device_number)
  128. {
  129. struct hash_entry *chain;
  130. chain = table[inode_number & HASH_MASK];
  131. while (chain != NULL) {
  132. if (chain->inode_number == inode_number &&
  133. chain->device_number == device_number)
  134. return chain->data;
  135. chain = chain->next;
  136. }
  137. return NULL;
  138. }
  139. static void
  140. trap_signal(int sig __unused)
  141. {
  142. if (tmp_file[0] != '\0')
  143. unlink(tmp_file);
  144. exit(1);
  145. }
  146. /*
  147. * Deals with junk files in the man or cat section directories.
  148. */
  149. static void
  150. junk(const char *mandir, const char *name, const char *reason)
  151. {
  152. if (verbose)
  153. fprintf(stderr, "%s/%s: %s\n", mandir, name, reason);
  154. if (rm_junk) {
  155. fprintf(stderr, "rm %s/%s\n", mandir, name);
  156. if (!pretend && unlink(name) < 0)
  157. warn("%s/%s", mandir, name);
  158. }
  159. }
  160. /*
  161. * Returns TOP_LEVEL_DIR for .../man, MAN_SECTION_DIR for .../manXXX,
  162. * and UNKNOWN for everything else.
  163. */
  164. static int
  165. directory_type(char *dir)
  166. {
  167. char *p;
  168. for (;;) {
  169. p = strrchr(dir, '/');
  170. if (p == NULL || p[1] != '\0')
  171. break;
  172. *p = '\0';
  173. }
  174. if (p == NULL)
  175. p = dir;
  176. else
  177. p++;
  178. if (strncmp(p, "man", 3) == 0) {
  179. p += 3;
  180. if (*p == '\0')
  181. return TOP_LEVEL_DIR;
  182. while (isalnum((unsigned char)*p) || *p == '_') {
  183. if (*++p == '\0')
  184. return MAN_SECTION_DIR;
  185. }
  186. }
  187. return UNKNOWN;
  188. }
  189. /*
  190. * Tests whether the given file name (without a preceding path)
  191. * is a proper man page name (like "mk-amd-map.8.gz").
  192. * Only alphanumerics and '_' are allowed after the last '.' and
  193. * the last '.' can't be the first or last characters.
  194. */
  195. static int
  196. is_manpage_name(char *name)
  197. {
  198. char *lastdot = NULL;
  199. char *n = name;
  200. while (*n != '\0') {
  201. if (!isalnum((unsigned char)*n)) {
  202. switch (*n) {
  203. case '_':
  204. break;
  205. case '-':
  206. case '+':
  207. case '[':
  208. case ':':
  209. lastdot = NULL;
  210. break;
  211. case '.':
  212. lastdot = n;
  213. break;
  214. default:
  215. return 0;
  216. }
  217. }
  218. n++;
  219. }
  220. return lastdot > name && lastdot + 1 < n;
  221. }
  222. static int
  223. is_bzipped(char *name)
  224. {
  225. int len = strlen(name);
  226. return len >= 5 && strcmp(&name[len - 4], BZ2_EXT) == 0;
  227. }
  228. static int
  229. is_gzipped(char *name)
  230. {
  231. int len = strlen(name);
  232. return len >= 4 && strcmp(&name[len - 3], GZ_EXT) == 0;
  233. }
  234. /*
  235. * Converts manXXX to catXXX.
  236. */
  237. static char *
  238. get_cat_section(char *section)
  239. {
  240. char *cat_section;
  241. cat_section = strdup(section);
  242. strncpy(cat_section, "cat", 3);
  243. return cat_section;
  244. }
  245. /*
  246. * Tests to see if the given directory has already been visited.
  247. */
  248. static int
  249. already_visited(char *mandir, char *dir, int count_visit)
  250. {
  251. struct stat st;
  252. if (stat(dir, &st) < 0) {
  253. if (mandir != NULL)
  254. warn("%s/%s", mandir, dir);
  255. else
  256. warn("%s", dir);
  257. exit_code = 1;
  258. return 1;
  259. }
  260. if (find_hashtable(visited, st.st_ino, st.st_dev) != NULL) {
  261. if (mandir != NULL)
  262. warnx("already visited %s/%s", mandir, dir);
  263. else
  264. warnx("already visited %s", dir);
  265. return 1;
  266. }
  267. if (count_visit)
  268. insert_hashtable(visited, st.st_ino, st.st_dev, "");
  269. return 0;
  270. }
  271. /*
  272. * Returns a set of TEST_* bits describing a file's type and permissions.
  273. * If mod_time isn't NULL, it will contain the file's modification time.
  274. */
  275. static int
  276. test_path(char *name, time_t *mod_time)
  277. {
  278. int result;
  279. if (stat(name, &test_st) < 0)
  280. return 0;
  281. result = TEST_EXISTS;
  282. if (mod_time != NULL)
  283. *mod_time = test_st.st_mtime;
  284. if (S_ISDIR(test_st.st_mode))
  285. result |= TEST_DIR;
  286. else if (S_ISREG(test_st.st_mode))
  287. result |= TEST_FILE;
  288. if (access(name, R_OK))
  289. result |= TEST_READABLE;
  290. if (access(name, W_OK))
  291. result |= TEST_WRITABLE;
  292. return result;
  293. }
  294. /*
  295. * Checks whether a file is a symbolic link.
  296. */
  297. static int
  298. is_symlink(char *path)
  299. {
  300. struct stat st;
  301. return lstat(path, &st) >= 0 && S_ISLNK(st.st_mode);
  302. }
  303. /*
  304. * Tests to see if the given directory can be written to.
  305. */
  306. static void
  307. check_writable(char *mandir)
  308. {
  309. if (verbose && !(test_path(mandir, NULL) & TEST_WRITABLE))
  310. fprintf(stderr, "%s: not writable - will only be able to write to existing cat directories\n", mandir);
  311. }
  312. /*
  313. * If the directory exists, attempt to make it writable, otherwise
  314. * attempt to create it.
  315. */
  316. static int
  317. make_writable_dir(char *mandir, char *dir)
  318. {
  319. int test;
  320. if ((test = test_path(dir, NULL)) != 0) {
  321. if (!(test & TEST_WRITABLE) && chmod(dir, 0755) < 0) {
  322. warn("%s/%s: chmod", mandir, dir);
  323. exit_code = 1;
  324. return 0;
  325. }
  326. } else {
  327. if (verbose || pretend)
  328. fprintf(stderr, "mkdir %s\n", dir);
  329. if (!pretend) {
  330. unlink(dir);
  331. if (mkdir(dir, 0755) < 0) {
  332. warn("%s/%s: mkdir", mandir, dir);
  333. exit_code = 1;
  334. return 0;
  335. }
  336. }
  337. }
  338. return 1;
  339. }
  340. /*
  341. * Processes a single man page source by using nroff to create
  342. * the preformatted cat page.
  343. */
  344. static void
  345. process_page(char *mandir, char *src, char *cat, enum Ziptype zipped)
  346. {
  347. int src_test, cat_test;
  348. time_t src_mtime, cat_mtime;
  349. char cmd[MAXPATHLEN];
  350. dev_t src_dev;
  351. ino_t src_ino;
  352. const char *link_name;
  353. src_test = test_path(src, &src_mtime);
  354. if (!(src_test & (TEST_FILE|TEST_READABLE))) {
  355. if (!(src_test & TEST_DIR)) {
  356. warnx("%s/%s: unreadable", mandir, src);
  357. exit_code = 1;
  358. if (rm_junk && is_symlink(src))
  359. junk(mandir, src, "bogus symlink");
  360. }
  361. return;
  362. }
  363. src_dev = test_st.st_dev;
  364. src_ino = test_st.st_ino;
  365. cat_test = test_path(cat, &cat_mtime);
  366. if (cat_test & (TEST_FILE|TEST_READABLE)) {
  367. if (!force && cat_mtime >= src_mtime) {
  368. if (verbose) {
  369. fprintf(stderr, "\t%s/%s: up to date\n",
  370. mandir, src);
  371. }
  372. return;
  373. }
  374. }
  375. /*
  376. * Is the man page a link to one we've already processed?
  377. */
  378. if ((link_name = find_hashtable(links, src_ino, src_dev)) != NULL) {
  379. if (verbose || pretend) {
  380. fprintf(stderr, "%slink %s -> %s\n",
  381. verbose ? "\t" : "", cat, link_name);
  382. }
  383. if (!pretend)
  384. link(link_name, cat);
  385. return;
  386. }
  387. insert_hashtable(links, src_ino, src_dev, strdup(cat));
  388. if (verbose || pretend) {
  389. fprintf(stderr, "%sformat %s -> %s\n",
  390. verbose ? "\t" : "", src, cat);
  391. if (pretend)
  392. return;
  393. }
  394. snprintf(tmp_file, sizeof tmp_file, "%s.tmp", cat);
  395. snprintf(cmd, sizeof cmd,
  396. "%scat %s | tbl | nroff -c -T%s -man | %s > %s.tmp",
  397. zipped == BZIP ? BZ2CAT_CMD : zipped == GZIP ? GZCAT_CMD : "",
  398. src, nroff_device,
  399. zipped == BZIP ? BZ2_CMD : zipped == GZIP ? GZ_CMD : "cat",
  400. cat);
  401. if (system(cmd) != 0)
  402. err(1, "formatting pipeline");
  403. if (rename(tmp_file, cat) < 0)
  404. warn("%s", cat);
  405. tmp_file[0] = '\0';
  406. }
  407. /*
  408. * Scan the man section directory for pages and process each one,
  409. * then check for junk in the corresponding cat section.
  410. */
  411. static void
  412. scan_section(char *mandir, char *section, char *cat_section)
  413. {
  414. struct dirent **entries;
  415. char **expected = NULL;
  416. int npages;
  417. int nexpected = 0;
  418. int i, e;
  419. enum Ziptype zipped;
  420. char *page_name;
  421. char page_path[MAXPATHLEN];
  422. char cat_path[MAXPATHLEN];
  423. char zip_path[MAXPATHLEN];
  424. /*
  425. * scan the man section directory for pages
  426. */
  427. npages = scandir(section, &entries, NULL, alphasort);
  428. if (npages < 0) {
  429. warn("%s/%s", mandir, section);
  430. exit_code = 1;
  431. return;
  432. }
  433. if (verbose || rm_junk) {
  434. /*
  435. * Maintain a list of all cat pages that should exist,
  436. * corresponding to existing man pages.
  437. */
  438. expected = (char **) calloc(npages, sizeof(char *));
  439. }
  440. for (i = 0; i < npages; free(entries[i++])) {
  441. page_name = entries[i]->d_name;
  442. snprintf(page_path, sizeof page_path, "%s/%s", section,
  443. page_name);
  444. if (!is_manpage_name(page_name)) {
  445. if (!(test_path(page_path, NULL) & TEST_DIR)) {
  446. junk(mandir, page_path,
  447. "invalid man page name");
  448. }
  449. continue;
  450. }
  451. zipped = is_bzipped(page_name) ? BZIP :
  452. is_gzipped(page_name) ? GZIP : NONE;
  453. if (zipped != NONE) {
  454. snprintf(cat_path, sizeof cat_path, "%s/%s",
  455. cat_section, page_name);
  456. if (expected != NULL)
  457. expected[nexpected++] = strdup(page_name);
  458. process_page(mandir, page_path, cat_path, zipped);
  459. } else {
  460. /*
  461. * We've got an uncompressed man page,
  462. * check to see if there's a (preferred)
  463. * compressed one.
  464. */
  465. snprintf(zip_path, sizeof zip_path, "%s%s",
  466. page_path, GZ_EXT);
  467. if (test_path(zip_path, NULL) != 0) {
  468. junk(mandir, page_path,
  469. "man page unused due to existing " GZ_EXT);
  470. } else {
  471. if (verbose) {
  472. fprintf(stderr,
  473. "warning, %s is uncompressed\n",
  474. page_path);
  475. }
  476. snprintf(cat_path, sizeof cat_path, "%s/%s",
  477. cat_section, page_name);
  478. if (expected != NULL) {
  479. asprintf(&expected[nexpected++],
  480. "%s", page_name);
  481. }
  482. process_page(mandir, page_path, cat_path, NONE);
  483. }
  484. }
  485. }
  486. free(entries);
  487. if (expected == NULL)
  488. return;
  489. /*
  490. * scan cat sections for junk
  491. */
  492. npages = scandir(cat_section, &entries, NULL, alphasort);
  493. e = 0;
  494. for (i = 0; i < npages; free(entries[i++])) {
  495. const char *junk_reason;
  496. int cmp = 1;
  497. page_name = entries[i]->d_name;
  498. if (strcmp(page_name, ".") == 0 || strcmp(page_name, "..") == 0)
  499. continue;
  500. /*
  501. * Keep the index into the expected cat page list
  502. * ahead of the name we've found.
  503. */
  504. while (e < nexpected &&
  505. (cmp = strcmp(page_name, expected[e])) > 0)
  506. free(expected[e++]);
  507. if (cmp == 0)
  508. continue;
  509. /* we have an unexpected page */
  510. snprintf(cat_path, sizeof cat_path, "%s/%s", cat_section,
  511. page_name);
  512. if (!is_manpage_name(page_name)) {
  513. if (test_path(cat_path, NULL) & TEST_DIR)
  514. continue;
  515. junk_reason = "invalid cat page name";
  516. } else if (!is_gzipped(page_name) && e + 1 < nexpected &&
  517. strncmp(page_name, expected[e + 1], strlen(page_name)) == 0 &&
  518. strlen(expected[e + 1]) == strlen(page_name) + 3) {
  519. junk_reason = "cat page unused due to existing " GZ_EXT;
  520. } else
  521. junk_reason = "cat page without man page";
  522. junk(mandir, cat_path, junk_reason);
  523. }
  524. free(entries);
  525. while (e < nexpected)
  526. free(expected[e++]);
  527. free(expected);
  528. }
  529. /*
  530. * Processes a single man section.
  531. */
  532. static void
  533. process_section(char *mandir, char *section)
  534. {
  535. char *cat_section;
  536. if (already_visited(mandir, section, 1))
  537. return;
  538. if (verbose)
  539. fprintf(stderr, " section %s\n", section);
  540. cat_section = get_cat_section(section);
  541. if (make_writable_dir(mandir, cat_section))
  542. scan_section(mandir, section, cat_section);
  543. free(cat_section);
  544. }
  545. static int
  546. select_sections(const struct dirent *entry)
  547. {
  548. char *name;
  549. int ret;
  550. name = strdup(entry->d_name);
  551. ret = directory_type(name) == MAN_SECTION_DIR;
  552. free(name);
  553. return (ret);
  554. }
  555. /*
  556. * Processes a single top-level man directory. If section isn't NULL,
  557. * it will only process that section sub-directory, otherwise it will
  558. * process all of them.
  559. */
  560. static void
  561. process_mandir(char *dir_name, char *section)
  562. {
  563. fchdir(starting_dir);
  564. if (already_visited(NULL, dir_name, section == NULL))
  565. return;
  566. check_writable(dir_name);
  567. if (verbose)
  568. fprintf(stderr, "man directory %s\n", dir_name);
  569. if (pretend)
  570. fprintf(stderr, "cd %s\n", dir_name);
  571. if (chdir(dir_name) < 0) {
  572. warn("%s: chdir", dir_name);
  573. exit_code = 1;
  574. return;
  575. }
  576. if (section != NULL) {
  577. process_section(dir_name, section);
  578. } else {
  579. struct dirent **entries;
  580. char *machine_dir, *arch_dir;
  581. int nsections;
  582. int i;
  583. nsections = scandir(".", &entries, select_sections, alphasort);
  584. if (nsections < 0) {
  585. warn("%s", dir_name);
  586. exit_code = 1;
  587. return;
  588. }
  589. for (i = 0; i < nsections; i++) {
  590. process_section(dir_name, entries[i]->d_name);
  591. asprintf(&machine_dir, "%s/%s", entries[i]->d_name,
  592. machine);
  593. if (test_path(machine_dir, NULL) & TEST_DIR)
  594. process_section(dir_name, machine_dir);
  595. free(machine_dir);
  596. if (strcmp(machine_arch, machine) != 0) {
  597. asprintf(&arch_dir, "%s/%s", entries[i]->d_name,
  598. machine_arch);
  599. if (test_path(arch_dir, NULL) & TEST_DIR)
  600. process_section(dir_name, arch_dir);
  601. free(arch_dir);
  602. }
  603. free(entries[i]);
  604. }
  605. free(entries);
  606. }
  607. }
  608. /*
  609. * Processes one argument, which may be a colon-separated list of
  610. * directories.
  611. */
  612. static void
  613. process_argument(const char *arg)
  614. {
  615. char *dir;
  616. char *mandir;
  617. char *section;
  618. char *parg;
  619. parg = strdup(arg);
  620. if (parg == NULL)
  621. err(1, "out of memory");
  622. while ((dir = strsep(&parg, ":")) != NULL) {
  623. switch (directory_type(dir)) {
  624. case TOP_LEVEL_DIR:
  625. if (locale != NULL) {
  626. asprintf(&mandir, "%s/%s", dir, locale);
  627. process_mandir(mandir, NULL);
  628. free(mandir);
  629. if (lang_locale != NULL) {
  630. asprintf(&mandir, "%s/%s", dir,
  631. lang_locale);
  632. process_mandir(mandir, NULL);
  633. free(mandir);
  634. }
  635. } else {
  636. process_mandir(dir, NULL);
  637. }
  638. break;
  639. case MAN_SECTION_DIR: {
  640. mandir = strdup(dirname(dir));
  641. section = strdup(basename(dir));
  642. process_mandir(mandir, section);
  643. free(mandir);
  644. free(section);
  645. break;
  646. }
  647. default:
  648. warnx("%s: directory name not in proper man form", dir);
  649. exit_code = 1;
  650. }
  651. }
  652. free(parg);
  653. }
  654. static void
  655. determine_locale(void)
  656. {
  657. char *sep;
  658. if ((locale = setlocale(LC_CTYPE, "")) == NULL) {
  659. warnx("-L option used, but no locale found\n");
  660. return;
  661. }
  662. sep = strchr(locale, '_');
  663. if (sep != NULL && isupper((unsigned char)sep[1])
  664. && isupper((unsigned char)sep[2])) {
  665. asprintf(&lang_locale, "%.*s%s", (int)(sep - locale),
  666. locale, &sep[3]);
  667. }
  668. sep = nl_langinfo(CODESET);
  669. if (sep != NULL && *sep != '\0' && strcmp(sep, "US-ASCII") != 0) {
  670. int i;
  671. for (i = 0; locale_device[i] != NULL; i += 2) {
  672. if (strcmp(sep, locale_device[i]) == 0) {
  673. nroff_device = locale_device[i + 1];
  674. break;
  675. }
  676. }
  677. }
  678. if (verbose) {
  679. if (lang_locale != NULL)
  680. fprintf(stderr, "short locale is %s\n", lang_locale);
  681. fprintf(stderr, "nroff device is %s\n", nroff_device);
  682. }
  683. }
  684. static void
  685. usage(void)
  686. {
  687. fprintf(stderr, "usage: %s [-fLnrv] [directories ...]\n",
  688. getprogname());
  689. exit(1);
  690. }
  691. int
  692. main(int argc, char **argv)
  693. {
  694. int opt;
  695. if ((uid = getuid()) == 0) {
  696. fprintf(stderr, "don't run %s as root, use:\n echo", argv[0]);
  697. for (optind = 0; optind < argc; optind++) {
  698. fprintf(stderr, " %s", argv[optind]);
  699. }
  700. fprintf(stderr, " | nice -5 su -m man\n");
  701. exit(1);
  702. }
  703. while ((opt = getopt(argc, argv, "vnfLrh")) != -1) {
  704. switch (opt) {
  705. case 'f':
  706. force++;
  707. break;
  708. case 'L':
  709. determine_locale();
  710. break;
  711. case 'n':
  712. pretend++;
  713. break;
  714. case 'r':
  715. rm_junk++;
  716. break;
  717. case 'v':
  718. verbose++;
  719. break;
  720. default:
  721. usage();
  722. /* NOTREACHED */
  723. }
  724. }
  725. if ((starting_dir = open(".", 0)) < 0) {
  726. err(1, ".");
  727. }
  728. umask(022);
  729. signal(SIGINT, trap_signal);
  730. signal(SIGHUP, trap_signal);
  731. signal(SIGQUIT, trap_signal);
  732. signal(SIGTERM, trap_signal);
  733. if ((machine = getenv("MACHINE")) == NULL) {
  734. static struct utsname utsname;
  735. if (uname(&utsname) == -1)
  736. err(1, "uname");
  737. machine = utsname.machine;
  738. }
  739. if ((machine_arch = getenv("MACHINE_ARCH")) == NULL)
  740. machine_arch = MACHINE_ARCH;
  741. if (optind == argc) {
  742. const char *manpath = getenv("MANPATH");
  743. if (manpath == NULL)
  744. manpath = DEFAULT_MANPATH;
  745. process_argument(manpath);
  746. } else {
  747. while (optind < argc)
  748. process_argument(argv[optind++]);
  749. }
  750. exit(exit_code);
  751. }