xref: /csrg-svn/old/catman/catman.c (revision 22487)
1*22487Sdist /*
2*22487Sdist  * Copyright (c) 1980 Regents of the University of California.
3*22487Sdist  * All rights reserved.  The Berkeley software License Agreement
4*22487Sdist  * specifies the terms and conditions for redistribution.
5*22487Sdist  */
6*22487Sdist 
711296Ssam #ifndef lint
8*22487Sdist char copyright[] =
9*22487Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10*22487Sdist  All rights reserved.\n";
11*22487Sdist #endif not lint
12964Sbill 
13*22487Sdist #ifndef lint
14*22487Sdist static char sccsid[] = "@(#)catman.c	5.1 (Berkeley) 06/06/85";
15*22487Sdist #endif not lint
16*22487Sdist 
1711296Ssam /*
1811296Ssam  * catman: update cat'able versions of manual pages
1911296Ssam  *  (whatis database also)
2011296Ssam  */
2111296Ssam #include <stdio.h>
2211296Ssam #include <sys/types.h>
2313549Ssam #include <sys/stat.h>
2413549Ssam #include <sys/time.h>
2513549Ssam #include <sys/dir.h>
2611296Ssam #include <ctype.h>
27964Sbill 
2811296Ssam #define	SYSTEM(str)	(pflag ? printf("%s\n", str) : system(str))
29964Sbill 
3011296Ssam char	buf[BUFSIZ];
3111296Ssam char	pflag;
3211296Ssam char	nflag;
3311296Ssam char	wflag;
3411296Ssam char	man[MAXNAMLEN+6] = "manx/";
3511296Ssam char	cat[MAXNAMLEN+6] = "catx/";
3618592Ssam char	*mandir = "/usr/man";
3711296Ssam char	*rindex();
38964Sbill 
39964Sbill main(ac, av)
4011296Ssam 	int ac;
4111296Ssam 	char *av[];
4211296Ssam {
4311296Ssam 	register char *msp, *csp, *sp;
4411296Ssam 	register char *sections;
4511296Ssam 	register int exstat = 0;
4611296Ssam 	register char changed = 0;
47964Sbill 
4818592Ssam 	ac--, av++;
4918593Ssam 	while (ac > 0 && av[0][0] == '-') {
5018592Ssam 		switch (av[0][1]) {
5118592Ssam 
5218592Ssam 		case 'p':
53964Sbill 			pflag++;
5418592Ssam 			break;
5518592Ssam 
5618592Ssam 		case 'n':
57964Sbill 			nflag++;
5818592Ssam 			break;
5918592Ssam 
6018592Ssam 		case 'w':
61964Sbill 			wflag++;
6218592Ssam 			break;
6318592Ssam 
6418592Ssam 		case 'M':
6518592Ssam 		case 'P':
6618592Ssam 			if (ac < 1) {
6718592Ssam 				fprintf(stderr, "%s: missing directory\n",
6818592Ssam 				    av[0]);
6918592Ssam 				exit(1);
7018592Ssam 			}
7118592Ssam 			ac--, av++;
7218592Ssam 			mandir = *av;
7318592Ssam 			break;
7418592Ssam 
7518592Ssam 		default:
76964Sbill 			goto usage;
7718592Ssam 		}
7818592Ssam 		ac--, av++;
79964Sbill 	}
8018593Ssam 	if (ac > 1) {
81964Sbill usage:
8218592Ssam 		printf("usage: catman [ -p ] [ -n ] [ -w ] [ -M path ] [ sections ]\n");
83964Sbill 		exit(-1);
84964Sbill 	}
8518593Ssam 	sections = (ac == 1) ? *av : "12345678ln";
86964Sbill 	if (wflag)
87964Sbill 		goto whatis;
8818592Ssam 	if (chdir(mandir) < 0) {
8918592Ssam 		fprintf(stderr, "catman: "), perror(mandir);
9018592Ssam 		exit(1);
9118592Ssam 	}
92964Sbill 	msp = &man[5];
93964Sbill 	csp = &cat[5];
94964Sbill 	umask(0);
95964Sbill 	for (sp = sections; *sp; sp++) {
9611296Ssam 		register DIR *mdir;
9711296Ssam 		register struct direct *dir;
9811296Ssam 		struct stat sbuf;
9911296Ssam 
100964Sbill 		man[3] = cat[3] = *sp;
101964Sbill 		*msp = *csp = '\0';
1026813Sedward 		if ((mdir = opendir(man)) == NULL) {
1036813Sedward 			fprintf(stderr, "opendir:");
104964Sbill 			perror(man);
105964Sbill 			exstat = 1;
106964Sbill 			continue;
107964Sbill 		}
108964Sbill 		if (stat(cat, &sbuf) < 0) {
10911422Ssam 			char buf[MAXNAMLEN + 6], *cp, *rindex();
11011422Ssam 
11111422Ssam 			strcpy(buf, cat);
11211422Ssam 			cp = rindex(buf, '/');
11311422Ssam 			if (cp && cp[1] == '\0')
11411422Ssam 				*cp = '\0';
11511422Ssam 			if (pflag)
11611422Ssam 				printf("mkdir %s\n", buf);
11711422Ssam 			else if (mkdir(buf, 0777) < 0) {
11811422Ssam 				sprintf(buf, "catman: mkdir: %s", cat);
11911422Ssam 				perror(buf);
12011422Ssam 				continue;
12111422Ssam 			}
122964Sbill 			stat(cat, &sbuf);
123964Sbill 		}
124964Sbill 		if ((sbuf.st_mode & 0777) != 0777)
125964Sbill 			chmod(cat, 0777);
1266813Sedward 		while ((dir = readdir(mdir)) != NULL) {
12711296Ssam 			time_t time;
12811296Ssam 			char *tsp;
12911296Ssam 			FILE *inf;
13011296Ssam 
1316813Sedward 			if (dir->d_ino == 0 || dir->d_name[0] == '.')
132964Sbill 				continue;
133964Sbill 			/*
13411296Ssam 			 * Make sure this is a man file, i.e., that it
135964Sbill 			 * ends in .[0-9] or .[0-9][a-z]
136964Sbill 			 */
1376813Sedward 			tsp = rindex(dir->d_name, '.');
138964Sbill 			if (tsp == NULL)
139964Sbill 				continue;
14016079Sralph 			if (!isdigit(*++tsp) && *tsp != *sp)
141964Sbill 				continue;
14211296Ssam 			if (*++tsp && !isalpha(*tsp))
14311296Ssam 				continue;
14411296Ssam 			if (*tsp && *++tsp)
14511296Ssam 				continue;
1466813Sedward 			strcpy(msp, dir->d_name);
147964Sbill 			if ((inf = fopen(man, "r")) == NULL) {
148964Sbill 				perror(man);
149964Sbill 				exstat = 1;
150964Sbill 				continue;
151964Sbill 			}
152964Sbill 			if (getc(inf) == '.' && getc(inf) == 's'
153964Sbill 			    && getc(inf) == 'o') {
154964Sbill 				fclose(inf);
155964Sbill 				continue;
156964Sbill 			}
157964Sbill 			fclose(inf);
1586813Sedward 			strcpy(csp, dir->d_name);
159964Sbill 			if (stat(cat, &sbuf) >= 0) {
160964Sbill 				time = sbuf.st_mtime;
161964Sbill 				stat(man, &sbuf);
162964Sbill 				if (time >= sbuf.st_mtime)
163964Sbill 					continue;
164964Sbill 				unlink(cat);
165964Sbill 			}
166964Sbill 			sprintf(buf, "nroff -man %s > %s", man, cat);
167964Sbill 			SYSTEM(buf);
168964Sbill 			changed = 1;
169964Sbill 		}
1706813Sedward 		closedir(mdir);
171964Sbill 	}
172964Sbill 	if (changed && !nflag) {
173964Sbill whatis:
17418592Ssam 		if (!pflag) {
17518592Ssam 			execl("/bin/sh", "/bin/sh",
17618592Ssam 			    "/usr/lib/makewhatis", mandir, 0);
177964Sbill 			perror("/bin/sh /usr/lib/makewhatis");
178964Sbill 			exstat = 1;
17918592Ssam 		} else
18018592Ssam 			printf("/bin/sh /usr/lib/makewhatis %s\n", mandir);
181964Sbill 	}
182964Sbill 	exit(exstat);
183964Sbill }
184