xref: /original-bsd/old/catman/catman.c (revision 91043db2)
1 #ifndef lint
2 static char *sccsid = "@(#)catman.c	4.4 (Berkeley) 03/06/83";
3 #endif
4 
5 /*
6  * catman: update cat'able versions of manual pages
7  *  (whatis database also)
8  */
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <stat.h>
12 #include <time.h>
13 #include <dir.h>
14 #include <ctype.h>
15 
16 #define	SYSTEM(str)	(pflag ? printf("%s\n", str) : system(str))
17 
18 char	buf[BUFSIZ];
19 char	pflag;
20 char	nflag;
21 char	wflag;
22 char	man[MAXNAMLEN+6] = "manx/";
23 char	cat[MAXNAMLEN+6] = "catx/";
24 char	*rindex();
25 
26 main(ac, av)
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;
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)
52 		sections = "12345678";
53 	else {
54 usage:
55 		printf("usage: catman [ -p ] [ -n ] [ -w ] [ sections ]\n");
56 		exit(-1);
57 	}
58 	if (wflag)
59 		goto whatis;
60 	chdir("/usr/man");
61 	msp = &man[5];
62 	csp = &cat[5];
63 	umask(0);
64 	for (sp = sections; *sp; sp++) {
65 		register DIR *mdir;
66 		register struct direct *dir;
67 		struct stat sbuf;
68 
69 		man[3] = cat[3] = *sp;
70 		*msp = *csp = '\0';
71 		if ((mdir = opendir(man)) == NULL) {
72 			fprintf(stderr, "opendir:");
73 			perror(man);
74 			exstat = 1;
75 			continue;
76 		}
77 		if (stat(cat, &sbuf) < 0) {
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 			}
91 			stat(cat, &sbuf);
92 		}
93 		if ((sbuf.st_mode & 0777) != 0777)
94 			chmod(cat, 0777);
95 		while ((dir = readdir(mdir)) != NULL) {
96 			time_t time;
97 			char *tsp;
98 			FILE *inf;
99 
100 			if (dir->d_ino == 0 || dir->d_name[0] == '.')
101 				continue;
102 			/*
103 			 * Make sure this is a man file, i.e., that it
104 			 * ends in .[0-9] or .[0-9][a-z]
105 			 */
106 			tsp = rindex(dir->d_name, '.');
107 			if (tsp == NULL)
108 				continue;
109 			if (!isdigit(*++tsp))
110 				continue;
111 			if (*++tsp && !isalpha(*tsp))
112 				continue;
113 			if (*tsp && *++tsp)
114 				continue;
115 			strcpy(msp, dir->d_name);
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);
127 			strcpy(csp, dir->d_name);
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 		}
139 		closedir(mdir);
140 	}
141 	if (changed && !nflag) {
142 whatis:
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 }
153