Commit | Line | Data |
---|---|---|
dbfb06ee | 1 | static char *sccsid = "@(#)catman.c 4.2 (Berkeley) %G%"; |
a930013b | 2 | # include <stdio.h> |
dbfb06ee | 3 | # include <sys/param.h> |
a930013b | 4 | # include <stat.h> |
dbfb06ee | 5 | # include <dir.h> |
a930013b BJ |
6 | # include <ctype.h> |
7 | ||
8 | # define reg register | |
9 | # define bool char | |
10 | ||
11 | # define SYSTEM(str) (pflag ? printf("%s\n", str) : system(str)) | |
12 | ||
13 | char buf[BUFSIZ], | |
14 | pflag = 0, | |
15 | nflag = 0, | |
16 | wflag = 0; | |
17 | ||
dbfb06ee EW |
18 | char *rindex(); |
19 | ||
a930013b BJ |
20 | main(ac, av) |
21 | int ac; | |
22 | char *av[]; { | |
23 | ||
dbfb06ee EW |
24 | reg char *tsp, *msp, *csp, *sp; |
25 | reg FILE *inf; | |
26 | reg DIR *mdir; | |
a930013b BJ |
27 | reg long time; |
28 | reg char *sections; | |
29 | reg int exstat = 0; | |
30 | reg bool changed = 0; | |
dbfb06ee EW |
31 | static char man[MAXNAMLEN+6] = "manx/"; |
32 | static char cat[MAXNAMLEN+6] = "catx/"; | |
33 | reg struct direct *dir; | |
34 | struct stat sbuf; | |
a930013b BJ |
35 | |
36 | while (ac > 1) { | |
37 | av++; | |
38 | if (strcmp(*av, "-p") == 0) | |
39 | pflag++; | |
40 | else if (strcmp(*av, "-n") == 0) | |
41 | nflag++; | |
42 | else if (strcmp(*av, "-w") == 0) | |
43 | wflag++; | |
44 | else if (*av[0] == '-') | |
45 | goto usage; | |
46 | else | |
47 | break; | |
48 | ac--; | |
49 | } | |
50 | if (ac == 2) | |
51 | sections = *av; | |
52 | else if (ac < 2) | |
53 | sections = "12345678"; | |
54 | else { | |
55 | usage: | |
56 | printf("usage: catman [ -p ] [ -n ] [ -w ] [ sections ]\n"); | |
57 | exit(-1); | |
58 | } | |
59 | if (wflag) | |
60 | goto whatis; | |
61 | chdir("/usr/man"); | |
a930013b BJ |
62 | msp = &man[5]; |
63 | csp = &cat[5]; | |
64 | umask(0); | |
65 | for (sp = sections; *sp; sp++) { | |
66 | man[3] = cat[3] = *sp; | |
67 | *msp = *csp = '\0'; | |
dbfb06ee EW |
68 | if ((mdir = opendir(man)) == NULL) { |
69 | fprintf(stderr, "opendir:"); | |
a930013b BJ |
70 | perror(man); |
71 | exstat = 1; | |
72 | continue; | |
73 | } | |
74 | if (stat(cat, &sbuf) < 0) { | |
75 | sprintf(buf, "mkdir %s", cat); | |
76 | SYSTEM(buf); | |
77 | stat(cat, &sbuf); | |
78 | } | |
79 | if ((sbuf.st_mode & 0777) != 0777) | |
80 | chmod(cat, 0777); | |
dbfb06ee EW |
81 | while ((dir = readdir(mdir)) != NULL) { |
82 | if (dir->d_ino == 0 || dir->d_name[0] == '.') | |
a930013b BJ |
83 | continue; |
84 | /* | |
85 | * make sure this is a man file, i.e., that it | |
86 | * ends in .[0-9] or .[0-9][a-z] | |
87 | */ | |
dbfb06ee | 88 | tsp = rindex(dir->d_name, '.'); |
a930013b BJ |
89 | if (tsp == NULL) |
90 | continue; | |
91 | if (!isdigit(*++tsp) || ((*++tsp && !isalpha(*tsp)) || *++tsp)) | |
92 | continue; | |
93 | ||
dbfb06ee | 94 | strcpy(msp, dir->d_name); |
a930013b BJ |
95 | if ((inf = fopen(man, "r")) == NULL) { |
96 | perror(man); | |
97 | exstat = 1; | |
98 | continue; | |
99 | } | |
100 | if (getc(inf) == '.' && getc(inf) == 's' | |
101 | && getc(inf) == 'o') { | |
102 | fclose(inf); | |
103 | continue; | |
104 | } | |
105 | fclose(inf); | |
dbfb06ee | 106 | strcpy(csp, dir->d_name); |
a930013b BJ |
107 | if (stat(cat, &sbuf) >= 0) { |
108 | time = sbuf.st_mtime; | |
109 | stat(man, &sbuf); | |
110 | if (time >= sbuf.st_mtime) | |
111 | continue; | |
112 | unlink(cat); | |
113 | } | |
114 | sprintf(buf, "nroff -man %s > %s", man, cat); | |
115 | SYSTEM(buf); | |
116 | changed = 1; | |
117 | } | |
dbfb06ee | 118 | closedir(mdir); |
a930013b BJ |
119 | } |
120 | if (changed && !nflag) { | |
121 | whatis: | |
122 | if (pflag) | |
123 | printf("/bin/sh /usr/lib/makewhatis\n"); | |
124 | else { | |
125 | execl("/bin/sh", "/bin/sh", "/usr/lib/makewhatis", 0); | |
126 | perror("/bin/sh /usr/lib/makewhatis"); | |
127 | exstat = 1; | |
128 | } | |
129 | } | |
130 | exit(exstat); | |
131 | } |