111296Ssam #ifndef lint 2*16079Sralph static char *sccsid = "@(#)catman.c 4.6 (Berkeley) 02/23/84"; 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/"; 2411296Ssam char *rindex(); 25964Sbill 26964Sbill main(ac, av) 2711296Ssam int ac; 2811296Ssam char *av[]; 2911296Ssam { 3011296Ssam register char *msp, *csp, *sp; 3111296Ssam register char *sections; 3211296Ssam register int exstat = 0; 3311296Ssam 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) 52*16079Sralph sections = "12345678ln"; 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++) { 6511296Ssam register DIR *mdir; 6611296Ssam register struct direct *dir; 6711296Ssam struct stat sbuf; 6811296Ssam 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) { 7811422Ssam char buf[MAXNAMLEN + 6], *cp, *rindex(); 7911422Ssam 8011422Ssam strcpy(buf, cat); 8111422Ssam cp = rindex(buf, '/'); 8211422Ssam if (cp && cp[1] == '\0') 8311422Ssam *cp = '\0'; 8411422Ssam if (pflag) 8511422Ssam printf("mkdir %s\n", buf); 8611422Ssam else if (mkdir(buf, 0777) < 0) { 8711422Ssam sprintf(buf, "catman: mkdir: %s", cat); 8811422Ssam perror(buf); 8911422Ssam continue; 9011422Ssam } 91964Sbill stat(cat, &sbuf); 92964Sbill } 93964Sbill if ((sbuf.st_mode & 0777) != 0777) 94964Sbill chmod(cat, 0777); 956813Sedward while ((dir = readdir(mdir)) != NULL) { 9611296Ssam time_t time; 9711296Ssam char *tsp; 9811296Ssam FILE *inf; 9911296Ssam 1006813Sedward if (dir->d_ino == 0 || dir->d_name[0] == '.') 101964Sbill continue; 102964Sbill /* 10311296Ssam * Make sure this is a man file, i.e., that it 104964Sbill * ends in .[0-9] or .[0-9][a-z] 105964Sbill */ 1066813Sedward tsp = rindex(dir->d_name, '.'); 107964Sbill if (tsp == NULL) 108964Sbill continue; 109*16079Sralph if (!isdigit(*++tsp) && *tsp != *sp) 110964Sbill continue; 11111296Ssam if (*++tsp && !isalpha(*tsp)) 11211296Ssam continue; 11311296Ssam if (*tsp && *++tsp) 11411296Ssam continue; 1156813Sedward strcpy(msp, dir->d_name); 116964Sbill if ((inf = fopen(man, "r")) == NULL) { 117964Sbill perror(man); 118964Sbill exstat = 1; 119964Sbill continue; 120964Sbill } 121964Sbill if (getc(inf) == '.' && getc(inf) == 's' 122964Sbill && getc(inf) == 'o') { 123964Sbill fclose(inf); 124964Sbill continue; 125964Sbill } 126964Sbill fclose(inf); 1276813Sedward strcpy(csp, dir->d_name); 128964Sbill if (stat(cat, &sbuf) >= 0) { 129964Sbill time = sbuf.st_mtime; 130964Sbill stat(man, &sbuf); 131964Sbill if (time >= sbuf.st_mtime) 132964Sbill continue; 133964Sbill unlink(cat); 134964Sbill } 135964Sbill sprintf(buf, "nroff -man %s > %s", man, cat); 136964Sbill SYSTEM(buf); 137964Sbill changed = 1; 138964Sbill } 1396813Sedward closedir(mdir); 140964Sbill } 141964Sbill if (changed && !nflag) { 142964Sbill whatis: 143964Sbill if (pflag) 144964Sbill printf("/bin/sh /usr/lib/makewhatis\n"); 145964Sbill else { 146964Sbill execl("/bin/sh", "/bin/sh", "/usr/lib/makewhatis", 0); 147964Sbill perror("/bin/sh /usr/lib/makewhatis"); 148964Sbill exstat = 1; 149964Sbill } 150964Sbill } 151964Sbill exit(exstat); 152964Sbill } 153