|
|
1.1 ! root 1: /* $Header$ */ ! 2: ! 3: /* ! 4: * Author: Peter J. Nicklin ! 5: */ ! 6: #include <signal.h> ! 7: #include <stdio.h> ! 8: #include <sys/param.h> ! 9: #include "null.h" ! 10: #include "slist.h" ! 11: #include "system.h" ! 12: ! 13: #define MAXNAMLEN 255 ! 14: #define READ 0 ! 15: #define WRITE 1 ! 16: ! 17: static int popen_pid; /* process identity */ ! 18: ! 19: /* ! 20: * closegrep() closes the pipe from the grep command and returns the ! 21: * the exit status of the grep command. ! 22: */ ! 23: closegrep(fp) ! 24: FILE *fp; /* pipe file pointer */ ! 25: { ! 26: register (*hstat)(); /* hangup function pointer */ ! 27: register (*istat)(); /* interrupt function pointer */ ! 28: register (*qstat)(); /* quit function pointer */ ! 29: register int w; /* a child id */ ! 30: int status; /* child return status */ ! 31: ! 32: fclose(fp); ! 33: ! 34: istat = signal(SIGINT, SIG_IGN); ! 35: qstat = signal(SIGQUIT, SIG_IGN); ! 36: hstat = signal(SIGHUP, SIG_IGN); ! 37: ! 38: while ((w = wait(&status)) != popen_pid && w != -1) ! 39: continue; ! 40: ! 41: signal(SIGINT, istat); ! 42: signal(SIGQUIT, qstat); ! 43: signal(SIGHUP, hstat); ! 44: ! 45: status >>= NBBY; ! 46: status &= 0xff; ! 47: return(status); ! 48: } ! 49: ! 50: ! 51: ! 52: /* ! 53: * grep() returns a singly-linked list of file names from the grep ! 54: * command. NULL is returned on error. ! 55: */ ! 56: SLIST * ! 57: grep(greppath, grepargv) ! 58: char *greppath; /* grep command path */ ! 59: char **grepargv; /* grep argv */ ! 60: { ! 61: char filename[MAXNAMLEN + 1]; /* receiving filename buffer */ ! 62: char *readgrep(); /* read input from grep */ ! 63: char *slappend(); /* append key */ ! 64: FILE *fp; /* input stream from grep */ ! 65: FILE *opengrep(); /* open pipe to grep */ ! 66: int closegrep(); /* close pipe to grep */ ! 67: SLIST *filelist; /* list of file names */ ! 68: SLIST *slinit(); /* initialize list */ ! 69: ! 70: if ((fp = opengrep(greppath, grepargv)) == NULL) ! 71: return(NULL); ! 72: ! 73: filelist = slinit(); ! 74: ! 75: while (readgrep(filename, fp) != NULL) ! 76: if (slappend(filename, filelist) == NULL) ! 77: return(NULL); ! 78: ! 79: if (closegrep(fp) == 2) ! 80: return(NULL); ! 81: return(filelist); ! 82: } ! 83: ! 84: ! 85: ! 86: /* ! 87: * opengrep() opens a pipe to read from the grep command. A file ! 88: * pointer is returned, or NULL on error. ! 89: */ ! 90: FILE * ! 91: opengrep(greppath, grepargv) ! 92: char *greppath; /* grep command path */ ! 93: char **grepargv; /* grep argv */ ! 94: { ! 95: FILE *fdopen(); /* associate stream with file descrip */ ! 96: int p[2]; /* pipe file descriptors */ ! 97: ! 98: if (pipe(p) < 0) ! 99: return(NULL); ! 100: if ((popen_pid = FORK()) == 0) ! 101: { ! 102: close(p[READ]); ! 103: dup2(p[WRITE], WRITE); ! 104: close(p[WRITE]); ! 105: execv(greppath, grepargv); ! 106: _exit(1); ! 107: } ! 108: if (popen_pid == -1) ! 109: return(NULL); ! 110: close(p[WRITE]); ! 111: return(fdopen(p[READ], "r")); ! 112: } ! 113: ! 114: ! 115: ! 116: /* ! 117: * readgrep() reads a line from the grep stream. The newline character ! 118: * is replaced by a null character. Returns buf, or NULL at EOF. ! 119: */ ! 120: char * ! 121: readgrep(buf, fp) ! 122: char *buf; /* receiving buffer */ ! 123: register FILE *fp; /* input stream */ ! 124: { ! 125: register char *bp; /* receiving buffer pointer */ ! 126: register int c; /* current character */ ! 127: ! 128: bp = buf; ! 129: while((c = getc(fp)) != '\n' && c != EOF) ! 130: *bp++ = c; ! 131: if (c == EOF && bp == buf) ! 132: return(NULL); ! 133: *bp = '\0'; ! 134: return(buf); ! 135: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.