|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T d
Length: 6792 (0x1a88) Types: TextFile Names: »dolocal.c«
└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen └─⟦this⟧ »cph85dist/rman/client/dolocal.c«
/* * Copyright (c) 1985 Jonathan C. Broome and The Regents of the * University of California. * * This software may be freely redistributed without licensing * provided that this copyright notice remains intact and no profit * is gained through any redistribution. * * Please report any bug fixes or modifications to the author at: * * broome@ucb-vax.berkeley.edu * or: * ...!ucbvax!broome * * The author and the Regents assume no liability for any damages * or loss of revenue caused directly or indirectly by the use of * this software. */ #ifndef lint static char RCSid[] = "$Header: dolocal.c,v 1.9 85/08/27 15:20:14 broome Exp $"; #endif /* * $Log: dolocal.c,v $ * Revision 1.9 85/08/27 15:20:14 broome * Added copyright/distribution comment. * * Revision 1.8 85/08/27 15:04:13 broome * Final cleanup before sending out. * * Revision 1.7 85/08/05 22:14:03 broome * All new stuff... Uses $MANPATH for searching on local machine, * allows each user to have his own set of man pages. * * Revision 1.6 85/08/04 18:40:45 broome * Fixed to do raw mode files properly. * * Revision 1.5 85/07/24 10:38:45 broome * * Revision 1.4 85/07/16 11:07:52 broome * Added new option "-m" to force use of more, "-" option now disables more. * * Revision 1.3 85/07/13 16:05:37 broome * Added fix for setuid version of man. * * Revision 1.2 85/07/13 15:56:45 broome * Changed to do local reformatting if needed. * (Still need to deal with man running setuid) */ #define NROFF "/usr/bin/nroff" #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/dir.h> #include <stdio.h> static char *manpath[30]; static char *sections; char *getenv(); char *sec; extern char *pager; /* * Handle lookup of local man pages, dealing with $MANPATH, etc ... * Returns 1 if found page here, else 0. */ dolocal (page, sec, israw) char *page; char *sec; int israw; { static int paths = -1; if (paths == -1) { if ((sections = getenv ("MANSEC")) == (char *) 0) /* undocumented */ sections = "1lnpo6823457"; /* allows change of search order */ paths = getpath (manpath); } if (paths == 0) /* no MANPATH set */ return (0); return (find (page, sec, manpath, israw)); } /* * See if the page we're looking for is somewhere in $MANPATH, * show it if it found. */ find (page, section, dirs, mode) char *page; char *section; char *dirs[]; int mode; { DIR *dirp; /* pointer to directory to search */ struct direct *dp; /* one directory entry */ char cat[512]; /* name of catable page */ char man[512]; /* name of source page */ char mandir[512]; /* full pathname of this (sub)directory */ char base[512]; /* basename of file to find */ int len; /* length of file basename */ int s; /* index into section list */ int suff = section[0]; /* base suffix for this directory */ for ( ; *dirs; ++dirs) { /* for each root directory in $MANPATH */ if (chdir (*dirs)) /* chdir to handle .so directives properly */ continue; /* if cd fails, then we can't search dir */ for (s = 0; sections[s]; s++) { /* for each subdirectory ... */ if (section && sections[s] != suff) /* wrong section */ continue; sprintf (mandir, "%s/man%c", *dirs, sections[s]); if ((dirp = opendir (mandir)) == (DIR *) 0) /* bad directory */ continue; sprintf (base, "%s.%c", page, sections[s]); len = strlen (base); while (dp = readdir (dirp)) { if (strncmp (base, dp->d_name, len) == 0) { sprintf (man, "%s/%s", mandir, dp->d_name); sprintf (cat, "%s/cat%c/%s", *dirs, sections[s],dp->d_name); if (showpage (man, cat, mode)) return (1); } } closedir (dirp); } } return (0); } /* * Do the right thing to show this page. Returns 1 if successful, 0 on error. */ showpage (man, cat, israw) char *man; char *cat; int israw; { struct stat st; long time; FILE *fp; extern int use_more; register int ch, pid; if (israw) strcpy (cat, man); else { stat (man, &st); time = st.st_mtime; if (stat (cat, &st) < 0 || st.st_mtime < time || st.st_size == 0) { fprintf (stderr, "Reformatting page. Wait ... "); (void) fflush (stderr); if (format (man, cat)) { fprintf (stderr, "aborted (sorry)\n"); return (0); } fprintf (stderr, "done.\n"); } } if (!use_more) { if ((fp = fopen (cat, "r")) == NULL) return (0); while ((ch = getc (fp)) != EOF) putchar (ch); fclose (fp); } else { if ((pid = fork()) == 0) { #ifdef SETUID setuid (getuid ()); #endif execlp (pager, pager, "-s", cat, 0); perror (pager); _exit (1); } else { while (wait (0) != pid) ; } } return (1); } /* * Format the man page, placing the output into `cat'. */ format (man, cat) char *man, *cat; { int fd, pid; union wait status; if ((fd = creat (cat, 0644)) < 0) /* cannot create output file */ return (1); if ((pid = fork()) == 0) { /* child */ dup2 (fd, 1); execl (NROFF, "nroff", "-man", man, 0); perror ("exec"); _exit (1); } else if (pid > 0) { while (wait (&status) != pid) ; if (status.w_coredump) { (void) unlink ("core"); return (1); } if (status.w_status != 0) return (1); return (0); } return (1); } /* * Get the MANPATH environment variable and turn it * into a set of strings in an array so we can use it. */ getpath (manpath) char *manpath[]; { char **ap; char *getenv(); char *env; if ((env = getenv ("MANPATH")) == (char *) 0) #ifdef LOCAL env = "/usr/man"; /* give them the default local path */ #else return (0); #endif ap = manpath; while (*env) { *ap = env; while (*env) { if (*env == ':') { *env++ = '\0'; break; } env++; } if (*ap < env) ap++; } *ap = (char *) 0; return (ap - manpath); }