Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 33c7fa1

Browse files
minor tweaks
1 parent 6b286da commit 33c7fa1

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

‎chapter07/7-7.c‎

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@
44
* the standard input. Should the file name be printed when a matching line is
55
* found?
66
*
7-
* Answer: of course!
8-
*
97
* By Faisal Saadatmand
108
*/
119

10+
/* Answer: only when files are named as arguments */
11+
1212
#include <stdio.h>
13-
#include <string.h>
1413
#include <stdlib.h>
14+
#include <string.h>
1515

16-
#define MAXLINE 1000
16+
#define MAXLEN 1000
1717

1818
/* globals */
19-
char *progName;
19+
char *prog; /* program name */
2020

2121
/* functions */
2222
int getLine(char *, size_t, FILE *);
2323
FILE* loadFile(char *);
24-
int findPattern(FILE *, char *, char *, int, int);
24+
int findPattern(FILE *, constchar *, constchar *, constint,const int);
2525

2626
/* getLine: read line, return length - file pointer version */
2727
int getLine(char *line, size_t max, FILE *fp)
@@ -35,38 +35,44 @@ FILE* loadFile(char *fileName)
3535
{
3636
FILE* fp;
3737
if (!(fp = fopen(fileName, "r"))) {
38-
fprintf(stderr, "%s: can't open %s\n", progName, fileName);
38+
fprintf(stderr, "%s: can't open %s\n", prog, fileName);
3939
exit(EXIT_FAILURE);
4040
}
4141
return fp;
4242
}
4343

44-
int findPattern(FILE *fp, char *fileName, char *pattern, int except, int number)
44+
int findPattern(FILE *fp, const char *fileName, const char *pattern,
45+
const int except, const int number)
4546
{
46-
char line[MAXLINE];
47-
long int lineno, found = 0;
47+
char line[MAXLEN];
48+
long lineno;
49+
int found;
4850

49-
for (lineno = 1; getLine(line, MAXLINE, fp) > 0; lineno++)
51+
lineno = found = 0;
52+
while (getLine(line, MAXLEN, fp) > 0) {
53+
++lineno;
5054
if ((strstr(line, pattern) != NULL) != except) {
5155
if (fileName)
52-
printf("%s:", fileName);
56+
fprintf(stdout, "%s:", fileName);
5357
if (number)
54-
printf ("%ld:", lineno);
55-
printf("%s", line);
56-
found++;
58+
fprintf (stdout, "%ld:", lineno);
59+
fprintf(stdout, "%s", line);
60+
++found;
5761
}
62+
}
5863
return found;
5964
}
6065

6166
/* find: print lines that match pattern from 1s arg */
6267
int main(int argc, char *argv[])
6368
{
64-
int c, except=0, number=0, found=0;
65-
char *pattern=NULL;
66-
FILE *file=NULL;
69+
int c, except, number, found;
70+
char *pattern;
71+
FILE *file;
6772

68-
progName = argv[0];
69-
while (--argc > 0 && (*++argv)[0] == '-') /* check for flags */
73+
prog = argv[0];
74+
except = number = found = 0;
75+
while (--argc > 0 && (*++argv)[0] == '-') /* check for flags */
7076
while ((c = *++argv[0]))
7177
switch (c) {
7278
case 'x':
@@ -76,20 +82,19 @@ int main(int argc, char *argv[])
7682
number = 1;
7783
break;
7884
default:
79-
printf("%s: illegal option %c\n", progName, c);
85+
fprintf(stderr, "%s: illegal option %c\n", prog, c);
8086
argc = 0;
8187
found = -1;
8288
break;
8389
}
84-
85-
pattern = *argv; /* save a pointer to the pattern */
90+
pattern = *argv++; /* save a pointer to the pattern */
8691
if (argc < 1)
87-
printf("Usage: %s -x -n pattern\n", progName);
88-
else if (argc == 1) /* input from stdin */
92+
fprintf(stderr, "Usage: %s [-xn] PATTERN [FILE...]\n", prog);
93+
else if (argc == 1) /* input from stdin */
8994
found += findPattern(stdin, NULL, pattern, except, number);
90-
else /* input from file or set of files */
95+
else /* input from file or set of files */
9196
while (argc-- > 1) {
92-
file = loadFile(*++argv);
97+
file = loadFile(*argv++);
9398
found += findPattern(file, *argv, pattern, except, number);
9499
fclose(file);
95100
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /