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