1 /*
2 * Copyright 2017 Gary Mills
3 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 * Use is subject to license terms.
5 */
6
7 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
8 /* All Rights Reserved */
9
10 /*
11 * Copyright (c) 1980 Regents of the University of California.
12 * All rights reserved. The Berkeley software License Agreement
13 * specifies the terms and conditions for redistribution.
14 */
15
16
17 #include <stdio.h>
18 #include <ctype.h>
19
20 extern char gfile[];
21
22 void
grepcall(char * in,char * out,char * arg)23 grepcall(char *in, char *out, char *arg)
24 {
25 char line[200], *s, argig[100], *cv[50];
26 char *inp, inb[500];
27 FILE *qf, *gf;
28 int c, alph = 0, nv = 0;
29 int sv0, sv1;
30 strcpy(argig, arg);
31 strcat(argig, ".ig");
32 strcpy(inp = inb, in);
33 if (gfile[0] == 0)
34 sprintf(gfile, "/tmp/rj%dg", getpid());
35 for (cv[nv++] = "fgrep"; c = *inp; inp++) {
36 if (c == ' ')
37 c = *inp = 0;
38 else if (isupper(c))
39 *inp = tolower(c);
40 alph = (c == 0) ? 0 : alph+1;
41 if (alph == 1)
42 cv[nv++] = inp;
43 if (alph > 6)
44 *inp = 0;
45 }
46 {
47 sv0 = dup(0);
48 close(0);
49 if (open(argig, 0) != 0)
50 err("Can't read fgrep index %s", argig);
51 sv1 = dup(1);
52 close(1);
53 if (creat(gfile, 0666) != 1)
54 err("Can't write fgrep output %s", gfile);
55 fgrep(nv, cv);
56 close(0);
57 dup(sv0);
58 close(sv0);
59 close(1);
60 dup(sv1);
61 close(sv1);
62 }
63
64 gf = fopen(gfile, "r");
65 if (gf == NULL)
66 err("can't read fgrep output %s", gfile);
67 while (fgets(line, 100, gf) == line) {
68 line[100] = 0;
69 for (s = line; *s && (*s != '\t'); s++)
70 ;
71 if (*s == '\t') {
72 *s++ = '\n';
73 *s++ = 0;
74 }
75 if (line[0])
76 strcat(out, line);
77 while (*s) s++;
78 if (s[-1] != '\n')
79 while (!feof(gf) && getc(gf) != '\n')
80 ;
81 }
82 fclose(gf);
83 unlink(gfile);
84 }
85
86 void
clfgrep(void)87 clfgrep(void)
88 {
89 if (gfile[0])
90 unlink(gfile);
91 }
92