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