xref: /csrg-svn/old/catman/catman.c (revision 11296)
1*11296Ssam #ifndef lint
2*11296Ssam static char *sccsid = "@(#)catman.c	4.3 (Berkeley) 02/26/83";
3*11296Ssam #endif
4964Sbill 
5*11296Ssam /*
6*11296Ssam  * catman: update cat'able versions of manual pages
7*11296Ssam  *  (whatis database also)
8*11296Ssam  */
9*11296Ssam #include <stdio.h>
10*11296Ssam #include <sys/types.h>
11*11296Ssam #include <stat.h>
12*11296Ssam #include <time.h>
13*11296Ssam #include <dir.h>
14*11296Ssam #include <ctype.h>
15964Sbill 
16*11296Ssam #define	SYSTEM(str)	(pflag ? printf("%s\n", str) : system(str))
17964Sbill 
18*11296Ssam char	buf[BUFSIZ];
19*11296Ssam char	pflag;
20*11296Ssam char	nflag;
21*11296Ssam char	wflag;
22*11296Ssam char	man[MAXNAMLEN+6] = "manx/";
23*11296Ssam char	cat[MAXNAMLEN+6] = "catx/";
24*11296Ssam char	*rindex();
25964Sbill 
26964Sbill main(ac, av)
27*11296Ssam 	int ac;
28*11296Ssam 	char *av[];
29*11296Ssam {
30*11296Ssam 	register char *msp, *csp, *sp;
31*11296Ssam 	register char *sections;
32*11296Ssam 	register int exstat = 0;
33*11296Ssam 	register char changed = 0;
34964Sbill 
35964Sbill 	while (ac > 1) {
36964Sbill 		av++;
37964Sbill 		if (strcmp(*av, "-p") == 0)
38964Sbill 			pflag++;
39964Sbill 		else if (strcmp(*av, "-n") == 0)
40964Sbill 			nflag++;
41964Sbill 		else if (strcmp(*av, "-w") == 0)
42964Sbill 			wflag++;
43964Sbill 		else if (*av[0] == '-')
44964Sbill 			goto usage;
45964Sbill 		else
46964Sbill 			break;
47964Sbill 		ac--;
48964Sbill 	}
49964Sbill 	if (ac == 2)
50964Sbill 		sections = *av;
51964Sbill 	else if (ac < 2)
52964Sbill 		sections = "12345678";
53964Sbill 	else {
54964Sbill usage:
55964Sbill 		printf("usage: catman [ -p ] [ -n ] [ -w ] [ sections ]\n");
56964Sbill 		exit(-1);
57964Sbill 	}
58964Sbill 	if (wflag)
59964Sbill 		goto whatis;
60964Sbill 	chdir("/usr/man");
61964Sbill 	msp = &man[5];
62964Sbill 	csp = &cat[5];
63964Sbill 	umask(0);
64964Sbill 	for (sp = sections; *sp; sp++) {
65*11296Ssam 		register DIR *mdir;
66*11296Ssam 		register struct direct *dir;
67*11296Ssam 		struct stat sbuf;
68*11296Ssam 
69964Sbill 		man[3] = cat[3] = *sp;
70964Sbill 		*msp = *csp = '\0';
716813Sedward 		if ((mdir = opendir(man)) == NULL) {
726813Sedward 			fprintf(stderr, "opendir:");
73964Sbill 			perror(man);
74964Sbill 			exstat = 1;
75964Sbill 			continue;
76964Sbill 		}
77964Sbill 		if (stat(cat, &sbuf) < 0) {
78964Sbill 			sprintf(buf, "mkdir %s", cat);
79964Sbill 			SYSTEM(buf);
80964Sbill 			stat(cat, &sbuf);
81964Sbill 		}
82964Sbill 		if ((sbuf.st_mode & 0777) != 0777)
83964Sbill 			chmod(cat, 0777);
846813Sedward 		while ((dir = readdir(mdir)) != NULL) {
85*11296Ssam 			time_t time;
86*11296Ssam 			char *tsp;
87*11296Ssam 			FILE *inf;
88*11296Ssam 
896813Sedward 			if (dir->d_ino == 0 || dir->d_name[0] == '.')
90964Sbill 				continue;
91964Sbill 			/*
92*11296Ssam 			 * Make sure this is a man file, i.e., that it
93964Sbill 			 * ends in .[0-9] or .[0-9][a-z]
94964Sbill 			 */
956813Sedward 			tsp = rindex(dir->d_name, '.');
96964Sbill 			if (tsp == NULL)
97964Sbill 				continue;
98*11296Ssam 			if (!isdigit(*++tsp))
99964Sbill 				continue;
100*11296Ssam 			if (*++tsp && !isalpha(*tsp))
101*11296Ssam 				continue;
102*11296Ssam 			if (*tsp && *++tsp)
103*11296Ssam 				continue;
1046813Sedward 			strcpy(msp, dir->d_name);
105964Sbill 			if ((inf = fopen(man, "r")) == NULL) {
106964Sbill 				perror(man);
107964Sbill 				exstat = 1;
108964Sbill 				continue;
109964Sbill 			}
110964Sbill 			if (getc(inf) == '.' && getc(inf) == 's'
111964Sbill 			    && getc(inf) == 'o') {
112964Sbill 				fclose(inf);
113964Sbill 				continue;
114964Sbill 			}
115964Sbill 			fclose(inf);
1166813Sedward 			strcpy(csp, dir->d_name);
117964Sbill 			if (stat(cat, &sbuf) >= 0) {
118964Sbill 				time = sbuf.st_mtime;
119964Sbill 				stat(man, &sbuf);
120964Sbill 				if (time >= sbuf.st_mtime)
121964Sbill 					continue;
122964Sbill 				unlink(cat);
123964Sbill 			}
124964Sbill 			sprintf(buf, "nroff -man %s > %s", man, cat);
125964Sbill 			SYSTEM(buf);
126964Sbill 			changed = 1;
127964Sbill 		}
1286813Sedward 		closedir(mdir);
129964Sbill 	}
130964Sbill 	if (changed && !nflag) {
131964Sbill whatis:
132964Sbill 		if (pflag)
133964Sbill 			printf("/bin/sh /usr/lib/makewhatis\n");
134964Sbill 		else {
135964Sbill 			execl("/bin/sh", "/bin/sh", "/usr/lib/makewhatis", 0);
136964Sbill 			perror("/bin/sh /usr/lib/makewhatis");
137964Sbill 			exstat = 1;
138964Sbill 		}
139964Sbill 	}
140964Sbill 	exit(exstat);
141964Sbill }
142