4
4
* the standard input. Should the file name be printed when a matching line is
5
5
* found?
6
6
*
7
- * Answer: of course!
8
- *
9
7
* By Faisal Saadatmand
10
8
*/
11
9
10
+ /* Answer: only when files are named as arguments */
11
+
12
12
#include <stdio.h>
13
- #include <string.h>
14
13
#include <stdlib.h>
14
+ #include <string.h>
15
15
16
- #define MAXLINE 1000
16
+ #define MAXLEN 1000
17
17
18
18
/* globals */
19
- char * progName ;
19
+ char * prog ; /* program name */
20
20
21
21
/* functions */
22
22
int getLine (char * , size_t , FILE * );
23
23
FILE * loadFile (char * );
24
- int findPattern (FILE * , char * , char * , int , int );
24
+ int findPattern (FILE * , const char * , const char * , const int , const int );
25
25
26
26
/* getLine: read line, return length - file pointer version */
27
27
int getLine (char * line , size_t max , FILE * fp )
@@ -35,38 +35,44 @@ FILE* loadFile(char *fileName)
35
35
{
36
36
FILE * fp ;
37
37
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 );
39
39
exit (EXIT_FAILURE );
40
40
}
41
41
return fp ;
42
42
}
43
43
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 )
45
46
{
46
- char line [MAXLINE ];
47
- long int lineno , found = 0 ;
47
+ char line [MAXLEN ];
48
+ long lineno ;
49
+ int found ;
48
50
49
- for (lineno = 1 ; getLine (line , MAXLINE , fp ) > 0 ; lineno ++ )
51
+ lineno = found = 0 ;
52
+ while (getLine (line , MAXLEN , fp ) > 0 ) {
53
+ ++ lineno ;
50
54
if ((strstr (line , pattern ) != NULL ) != except ) {
51
55
if (fileName )
52
- printf ( "%s:" , fileName );
56
+ fprintf ( stdout , "%s:" , fileName );
53
57
if (number )
54
- printf ( "%ld:" , lineno );
55
- printf ( "%s" , line );
56
- found ++ ;
58
+ fprintf ( stdout , "%ld:" , lineno );
59
+ fprintf ( stdout , "%s" , line );
60
+ ++ found ;
57
61
}
62
+ }
58
63
return found ;
59
64
}
60
65
61
66
/* find: print lines that match pattern from 1s arg */
62
67
int main (int argc , char * argv [])
63
68
{
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 ;
67
72
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 */
70
76
while ((c = * ++ argv [0 ]))
71
77
switch (c ) {
72
78
case 'x' :
@@ -76,20 +82,19 @@ int main(int argc, char *argv[])
76
82
number = 1 ;
77
83
break ;
78
84
default :
79
- printf ( "%s: illegal option %c\n" , progName , c );
85
+ fprintf ( stderr , "%s: illegal option %c\n" , prog , c );
80
86
argc = 0 ;
81
87
found = -1 ;
82
88
break ;
83
89
}
84
-
85
- pattern = * argv ; /* save a pointer to the pattern */
90
+ pattern = * argv ++ ; /* save a pointer to the pattern */
86
91
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 */
89
94
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 */
91
96
while (argc -- > 1 ) {
92
- file = loadFile (* ++ argv );
97
+ file = loadFile (* argv ++ );
93
98
found += findPattern (file , * argv , pattern , except , number );
94
99
fclose (file );
95
100
}
0 commit comments