File: grep.c

package info (click to toggle)
dpuser 4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,628 kB
  • sloc: cpp: 148,693; ansic: 18,648; fortran: 5,815; lex: 1,116; makefile: 760; yacc: 741; sh: 390; pascal: 98
file content (66 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifdef vms
#include stdio
#else
#include <stdio.h>
#endif

/*
 * Rudimentary grep to test regex routines.
 *
 * DEBUG is only applicable to oz version of regex. Make sure to 
 * compile regex.c with -DDEBUG as well.
 *
 */
extern char *re_comp();
extern re_exec();

main(argc,argv)
char *argv[];
{
	char str[512];
	FILE *f;
	register int n;
	register char *p;

	if (argc < 2)
		error("usage: grep pat [file]");

	if ((p = re_comp(argv[1])) != 0) {
		printf("\t%s: %s\n", p, argv[1]);
		exit(1);
	}
#ifdef DEBUG
	symbolic(argv[1]);
#endif
	if (p = argv[2]) {
		if ((f = fopen(p, "r")) == NULL) {
			printf("cannot open %s\n", argv[2]);
			exit(1);
		}
		while ((n = load(str, f)) != EOF)
			if (re_exec(str))
				printf("%s\n",str);

	}
	exit(0);
}
load (s, f)
char *s;
FILE *f;
{
	register int c;
	static int lineno = 0;

	while ((c = getc(f)) != '\n' && c != EOF)
		*s++ = c;
	if (c == EOF)
		return (EOF);
	*s = (char) 0;
	return (++lineno);
}

error(s) char *s ; 
{ 
	fprintf(stderr,"%s\n",s); 
	exit(1); 
}