#include #include #include #include #include #include #include #include char *defmanpath = "/usr/local/share/man:/usr/share/man:/usr/man"; void adios(char *str) { perror(str); exit(1); } /* ** Function does not return if the page is there. */ int display(char *path, int sec, char *name) { char buf[BUFSIZ]; struct stat sb; char *catprog; int pd1[2], pd2[2], pd3[2]; pid_t pid; snprintf(buf, sizeof buf, "%s/man%d/%s.%d", path, sec, name, sec); if (stat(buf, &sb) != -1) { catprog = "cat"; goto displ; } snprintf(buf, sizeof buf, "%s/man%d/%s.%d.gz", path, sec, name, sec); if (stat(buf, &sb) != -1) { catprog = "zcat"; goto displ; } return 0; displ: /* Have zombies burried automatically */ signal(SIGCHLD, SIG_IGN); if (pipe(pd1) == -1) { adios("pipe"); } if ((pid = fork()) == -1) { adios("fork"); } else if (!pid) { /* child */ close(1); dup(pd1[1]); close(pd1[1]); close(pd1[0]); execlp(catprog, catprog, buf, NULL); adios("exec"); } /* parent */ close(pd1[1]); if (pipe(pd2) == -1) { adios("pipe"); } if ((pid = fork()) == -1) { adios("fork"); } else if (!pid) { /* child */ close(0); dup(pd1[0]); close(pd1[0]); close(1); dup(pd2[1]); close(pd2[1]); close(pd2[0]); execlp("nroff", "nroff", "-Tlp", "-man", NULL); adios("exec"); } /* parent */ close(pd2[1]); if (pipe(pd3) == -1) { adios("pipe"); } if ((pid = fork()) == -1) { adios("fork"); } else if (!pid) { /* child */ close(0); dup(pd2[0]); close(pd2[0]); close(1); dup(pd3[1]); close(pd3[1]); close(pd3[0]); execlp("col", "col", "-x", NULL); adios("exec"); } /* parent */ close(pd3[1]); /* parent */ close(0); dup(pd3[0]); close(pd3[0]); execlp("less", "less", "-is", NULL); adios("exec"); return 0; } int main(int argc, char *argv[]) { int sec; char *name, *manpath, *str, *tok; if (argc != 3) { fprintf(stderr, "Usage: %s SECTION NAME\n", argv[0]); exit(1); } sec = atoi(argv[1]); name = argv[2]; if (!(manpath = getenv("MANPATH"))) { manpath = defmanpath; } if (!(manpath = strdup(manpath))) { adios("strdup"); } for (str=manpath; ; str=NULL) { if (!(tok = strtok(str, ":"))) { break; } display(tok, sec, name); /* return only on failure */ } fprintf(stderr, "No man page for `%s' in section %d\n", name, sec); return 1; }