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