xref: /csrg-svn/old/catman/catman.c (revision 18593)
111296Ssam #ifndef lint
2*18593Ssam static char *sccsid = "@(#)catman.c	4.8 (Berkeley) 04/07/85";
311296Ssam #endif
4964Sbill 
511296Ssam /*
611296Ssam  * catman: update cat'able versions of manual pages
711296Ssam  *  (whatis database also)
811296Ssam  */
911296Ssam #include <stdio.h>
1011296Ssam #include <sys/types.h>
1113549Ssam #include <sys/stat.h>
1213549Ssam #include <sys/time.h>
1313549Ssam #include <sys/dir.h>
1411296Ssam #include <ctype.h>
15964Sbill 
1611296Ssam #define	SYSTEM(str)	(pflag ? printf("%s\n", str) : system(str))
17964Sbill 
1811296Ssam char	buf[BUFSIZ];
1911296Ssam char	pflag;
2011296Ssam char	nflag;
2111296Ssam char	wflag;
2211296Ssam char	man[MAXNAMLEN+6] = "manx/";
2311296Ssam char	cat[MAXNAMLEN+6] = "catx/";
2418592Ssam char	*mandir = "/usr/man";
2511296Ssam char	*rindex();
26964Sbill 
27964Sbill main(ac, av)
2811296Ssam 	int ac;
2911296Ssam 	char *av[];
3011296Ssam {
3111296Ssam 	register char *msp, *csp, *sp;
3211296Ssam 	register char *sections;
3311296Ssam 	register int exstat = 0;
3411296Ssam 	register char changed = 0;
35964Sbill 
3618592Ssam 	ac--, av++;
37*18593Ssam 	while (ac > 0 && av[0][0] == '-') {
3818592Ssam 		switch (av[0][1]) {
3918592Ssam 
4018592Ssam 		case 'p':
41964Sbill 			pflag++;
4218592Ssam 			break;
4318592Ssam 
4418592Ssam 		case 'n':
45964Sbill 			nflag++;
4618592Ssam 			break;
4718592Ssam 
4818592Ssam 		case 'w':
49964Sbill 			wflag++;
5018592Ssam 			break;
5118592Ssam 
5218592Ssam 		case 'M':
5318592Ssam 		case 'P':
5418592Ssam 			if (ac < 1) {
5518592Ssam 				fprintf(stderr, "%s: missing directory\n",
5618592Ssam 				    av[0]);
5718592Ssam 				exit(1);
5818592Ssam 			}
5918592Ssam 			ac--, av++;
6018592Ssam 			mandir = *av;
6118592Ssam 			break;
6218592Ssam 
6318592Ssam 		default:
64964Sbill 			goto usage;
6518592Ssam 		}
6618592Ssam 		ac--, av++;
67964Sbill 	}
68*18593Ssam 	if (ac > 1) {
69964Sbill usage:
7018592Ssam 		printf("usage: catman [ -p ] [ -n ] [ -w ] [ -M path ] [ sections ]\n");
71964Sbill 		exit(-1);
72964Sbill 	}
73*18593Ssam 	sections = (ac == 1) ? *av : "12345678ln";
74964Sbill 	if (wflag)
75964Sbill 		goto whatis;
7618592Ssam 	if (chdir(mandir) < 0) {
7718592Ssam 		fprintf(stderr, "catman: "), perror(mandir);
7818592Ssam 		exit(1);
7918592Ssam 	}
80964Sbill 	msp = &man[5];
81964Sbill 	csp = &cat[5];
82964Sbill 	umask(0);
83964Sbill 	for (sp = sections; *sp; sp++) {
8411296Ssam 		register DIR *mdir;
8511296Ssam 		register struct direct *dir;
8611296Ssam 		struct stat sbuf;
8711296Ssam 
88964Sbill 		man[3] = cat[3] = *sp;
89964Sbill 		*msp = *csp = '\0';
906813Sedward 		if ((mdir = opendir(man)) == NULL) {
916813Sedward 			fprintf(stderr, "opendir:");
92964Sbill 			perror(man);
93964Sbill 			exstat = 1;
94964Sbill 			continue;
95964Sbill 		}
96964Sbill 		if (stat(cat, &sbuf) < 0) {
9711422Ssam 			char buf[MAXNAMLEN + 6], *cp, *rindex();
9811422Ssam 
9911422Ssam 			strcpy(buf, cat);
10011422Ssam 			cp = rindex(buf, '/');
10111422Ssam 			if (cp && cp[1] == '\0')
10211422Ssam 				*cp = '\0';
10311422Ssam 			if (pflag)
10411422Ssam 				printf("mkdir %s\n", buf);
10511422Ssam 			else if (mkdir(buf, 0777) < 0) {
10611422Ssam 				sprintf(buf, "catman: mkdir: %s", cat);
10711422Ssam 				perror(buf);
10811422Ssam 				continue;
10911422Ssam 			}
110964Sbill 			stat(cat, &sbuf);
111964Sbill 		}
112964Sbill 		if ((sbuf.st_mode & 0777) != 0777)
113964Sbill 			chmod(cat, 0777);
1146813Sedward 		while ((dir = readdir(mdir)) != NULL) {
11511296Ssam 			time_t time;
11611296Ssam 			char *tsp;
11711296Ssam 			FILE *inf;
11811296Ssam 
1196813Sedward 			if (dir->d_ino == 0 || dir->d_name[0] == '.')
120964Sbill 				continue;
121964Sbill 			/*
12211296Ssam 			 * Make sure this is a man file, i.e., that it
123964Sbill 			 * ends in .[0-9] or .[0-9][a-z]
124964Sbill 			 */
1256813Sedward 			tsp = rindex(dir->d_name, '.');
126964Sbill 			if (tsp == NULL)
127964Sbill 				continue;
12816079Sralph 			if (!isdigit(*++tsp) && *tsp != *sp)
129964Sbill 				continue;
13011296Ssam 			if (*++tsp && !isalpha(*tsp))
13111296Ssam 				continue;
13211296Ssam 			if (*tsp && *++tsp)
13311296Ssam 				continue;
1346813Sedward 			strcpy(msp, dir->d_name);
135964Sbill 			if ((inf = fopen(man, "r")) == NULL) {
136964Sbill 				perror(man);
137964Sbill 				exstat = 1;
138964Sbill 				continue;
139964Sbill 			}
140964Sbill 			if (getc(inf) == '.' && getc(inf) == 's'
141964Sbill 			    && getc(inf) == 'o') {
142964Sbill 				fclose(inf);
143964Sbill 				continue;
144964Sbill 			}
145964Sbill 			fclose(inf);
1466813Sedward 			strcpy(csp, dir->d_name);
147964Sbill 			if (stat(cat, &sbuf) >= 0) {
148964Sbill 				time = sbuf.st_mtime;
149964Sbill 				stat(man, &sbuf);
150964Sbill 				if (time >= sbuf.st_mtime)
151964Sbill 					continue;
152964Sbill 				unlink(cat);
153964Sbill 			}
154964Sbill 			sprintf(buf, "nroff -man %s > %s", man, cat);
155964Sbill 			SYSTEM(buf);
156964Sbill 			changed = 1;
157964Sbill 		}
1586813Sedward 		closedir(mdir);
159964Sbill 	}
160964Sbill 	if (changed && !nflag) {
161964Sbill whatis:
16218592Ssam 		if (!pflag) {
16318592Ssam 			execl("/bin/sh", "/bin/sh",
16418592Ssam 			    "/usr/lib/makewhatis", mandir, 0);
165964Sbill 			perror("/bin/sh /usr/lib/makewhatis");
166964Sbill 			exstat = 1;
16718592Ssam 		} else
16818592Ssam 			printf("/bin/sh /usr/lib/makewhatis %s\n", mandir);
169964Sbill 	}
170964Sbill 	exit(exstat);
171964Sbill }
172