-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path7-7.c
102 lines (91 loc) · 2.36 KB
/
7-7.c
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* Exercise 7-7. Modify the pattern finding program of Chapter 5 to take its
* input from a set named files or, if no files are named as arguments, from
* the standard input. Should the file name be printed when a matching line is
* found?
*
* By Faisal Saadatmand
*/
/* Answer: only when files are named as arguments */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 1000
/* globals */
char *prog; /* program name */
/* functions */
int getLine(char *, size_t, FILE *);
FILE* loadFile(char *);
int findPattern(FILE *, const char *, const char *, const int, const int);
/* getLine: read line, return length - file pointer version */
int getLine(char *line, size_t max, FILE *fp)
{
if (fgets(line, max, fp) == NULL)
return 0;
return strlen(line);
}
FILE* loadFile(char *fileName)
{
FILE* fp;
if (!(fp = fopen(fileName, "r"))) {
fprintf(stderr, "%s: can't open %s\n", prog, fileName);
exit(EXIT_FAILURE);
}
return fp;
}
int findPattern(FILE *fp, const char *fileName, const char *pattern,
const int except, const int number)
{
char line[MAXLEN];
long lineno;
int found;
lineno = found = 0;
while (getLine(line, MAXLEN, fp) > 0) {
++lineno;
if ((strstr(line, pattern) != NULL) != except) {
if (fileName)
fprintf(stdout, "%s:", fileName);
if (number)
fprintf (stdout, "%ld:", lineno);
fprintf(stdout, "%s", line);
++found;
}
}
return found;
}
/* find: print lines that match pattern from 1s arg */
int main(int argc, char *argv[])
{
int c, except, number, found;
char *pattern;
FILE *file;
prog = argv[0];
except = number = found = 0;
while (--argc > 0 && (*++argv)[0] == '-') /* check for flags */
while ((c = *++argv[0]))
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
default:
fprintf(stderr, "%s: illegal option %c\n", prog, c);
argc = 0;
found = -1;
break;
}
pattern = *argv++; /* save a pointer to the pattern */
if (argc < 1)
fprintf(stderr, "Usage: %s [-xn] PATTERN [FILE...]\n", prog);
else if (argc == 1) /* input from stdin */
found += findPattern(stdin, NULL, pattern, except, number);
else /* input from file or set of files */
while (argc-- > 1) {
file = loadFile(*argv++);
found += findPattern(file, *argv, pattern, except, number);
fclose(file);
}
return found;
}