xref: /csrg-svn/old/catman/catman.c (revision 6813)
1*6813Sedward static char *sccsid = "@(#)catman.c	4.2 (Berkeley) 05/12/82";
2964Sbill # include	<stdio.h>
3*6813Sedward # include	<sys/param.h>
4*6813Sedward # include	<stat.h>
5964Sbill # include	<dir.h>
6964Sbill # include	<ctype.h>
7964Sbill 
8964Sbill # define	reg	register
9964Sbill # define	bool	char
10964Sbill 
11964Sbill # define	SYSTEM(str)	(pflag ? printf("%s\n", str) : system(str))
12964Sbill 
13964Sbill char		buf[BUFSIZ],
14964Sbill 		pflag = 0,
15964Sbill 		nflag = 0,
16964Sbill 		wflag = 0;
17964Sbill 
18*6813Sedward char		*rindex();
19*6813Sedward 
20964Sbill main(ac, av)
21964Sbill int	ac;
22964Sbill char	*av[]; {
23964Sbill 
24*6813Sedward 	reg char	*tsp, *msp, *csp, *sp;
25*6813Sedward 	reg FILE	*inf;
26*6813Sedward 	reg DIR		*mdir;
27964Sbill 	reg long	time;
28964Sbill 	reg char	*sections;
29964Sbill 	reg int		exstat = 0;
30964Sbill 	reg bool	changed = 0;
31*6813Sedward 	static char	man[MAXNAMLEN+6] = "manx/";
32*6813Sedward 	static char	cat[MAXNAMLEN+6] = "catx/";
33*6813Sedward 	reg struct direct *dir;
34*6813Sedward 	struct stat	sbuf;
35964Sbill 
36964Sbill 	while (ac > 1) {
37964Sbill 		av++;
38964Sbill 		if (strcmp(*av, "-p") == 0)
39964Sbill 			pflag++;
40964Sbill 		else if (strcmp(*av, "-n") == 0)
41964Sbill 			nflag++;
42964Sbill 		else if (strcmp(*av, "-w") == 0)
43964Sbill 			wflag++;
44964Sbill 		else if (*av[0] == '-')
45964Sbill 			goto usage;
46964Sbill 		else
47964Sbill 			break;
48964Sbill 		ac--;
49964Sbill 	}
50964Sbill 	if (ac == 2)
51964Sbill 		sections = *av;
52964Sbill 	else if (ac < 2)
53964Sbill 		sections = "12345678";
54964Sbill 	else {
55964Sbill usage:
56964Sbill 		printf("usage: catman [ -p ] [ -n ] [ -w ] [ sections ]\n");
57964Sbill 		exit(-1);
58964Sbill 	}
59964Sbill 	if (wflag)
60964Sbill 		goto whatis;
61964Sbill 	chdir("/usr/man");
62964Sbill 	msp = &man[5];
63964Sbill 	csp = &cat[5];
64964Sbill 	umask(0);
65964Sbill 	for (sp = sections; *sp; sp++) {
66964Sbill 		man[3] = cat[3] = *sp;
67964Sbill 		*msp = *csp = '\0';
68*6813Sedward 		if ((mdir = opendir(man)) == NULL) {
69*6813Sedward 			fprintf(stderr, "opendir:");
70964Sbill 			perror(man);
71964Sbill 			exstat = 1;
72964Sbill 			continue;
73964Sbill 		}
74964Sbill 		if (stat(cat, &sbuf) < 0) {
75964Sbill 			sprintf(buf, "mkdir %s", cat);
76964Sbill 			SYSTEM(buf);
77964Sbill 			stat(cat, &sbuf);
78964Sbill 		}
79964Sbill 		if ((sbuf.st_mode & 0777) != 0777)
80964Sbill 			chmod(cat, 0777);
81*6813Sedward 		while ((dir = readdir(mdir)) != NULL) {
82*6813Sedward 			if (dir->d_ino == 0 || dir->d_name[0] == '.')
83964Sbill 				continue;
84964Sbill 			/*
85964Sbill 			 * make sure this is a man file, i.e., that it
86964Sbill 			 * ends in .[0-9] or .[0-9][a-z]
87964Sbill 			 */
88*6813Sedward 			tsp = rindex(dir->d_name, '.');
89964Sbill 			if (tsp == NULL)
90964Sbill 				continue;
91964Sbill 			if (!isdigit(*++tsp) || ((*++tsp && !isalpha(*tsp)) || *++tsp))
92964Sbill 				continue;
93964Sbill 
94*6813Sedward 			strcpy(msp, dir->d_name);
95964Sbill 			if ((inf = fopen(man, "r")) == NULL) {
96964Sbill 				perror(man);
97964Sbill 				exstat = 1;
98964Sbill 				continue;
99964Sbill 			}
100964Sbill 			if (getc(inf) == '.' && getc(inf) == 's'
101964Sbill 			    && getc(inf) == 'o') {
102964Sbill 				fclose(inf);
103964Sbill 				continue;
104964Sbill 			}
105964Sbill 			fclose(inf);
106*6813Sedward 			strcpy(csp, dir->d_name);
107964Sbill 			if (stat(cat, &sbuf) >= 0) {
108964Sbill 				time = sbuf.st_mtime;
109964Sbill 				stat(man, &sbuf);
110964Sbill 				if (time >= sbuf.st_mtime)
111964Sbill 					continue;
112964Sbill 				unlink(cat);
113964Sbill 			}
114964Sbill 			sprintf(buf, "nroff -man %s > %s", man, cat);
115964Sbill 			SYSTEM(buf);
116964Sbill 			changed = 1;
117964Sbill 		}
118*6813Sedward 		closedir(mdir);
119964Sbill 	}
120964Sbill 	if (changed && !nflag) {
121964Sbill whatis:
122964Sbill 		if (pflag)
123964Sbill 			printf("/bin/sh /usr/lib/makewhatis\n");
124964Sbill 		else {
125964Sbill 			execl("/bin/sh", "/bin/sh", "/usr/lib/makewhatis", 0);
126964Sbill 			perror("/bin/sh /usr/lib/makewhatis");
127964Sbill 			exstat = 1;
128964Sbill 		}
129964Sbill 	}
130964Sbill 	exit(exstat);
131964Sbill }
132