diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-11-09 01:47:36 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-11-09 01:47:36 +0000 |
commit | 50d6360771be509737bb55b2cc5bc5e25f2a4fea (patch) | |
tree | 81d4cfe9ec9b5281924f678c28f61542616a3db7 /findutils/grep.c | |
parent | fbb39c83b69d6c4de943c0b7374000339635d13d (diff) | |
download | busybox-w32-50d6360771be509737bb55b2cc5bc5e25f2a4fea.tar.gz busybox-w32-50d6360771be509737bb55b2cc5bc5e25f2a4fea.tar.bz2 busybox-w32-50d6360771be509737bb55b2cc5bc5e25f2a4fea.zip |
Stuff
Diffstat (limited to 'findutils/grep.c')
-rw-r--r-- | findutils/grep.c | 79 |
1 files changed, 38 insertions, 41 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 50a296178..8dcff0586 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -40,45 +40,46 @@ static const char grep_usage[] = | |||
40 | #if defined BB_REGEXP | 40 | #if defined BB_REGEXP |
41 | "This version of grep matches full regexps.\n"; | 41 | "This version of grep matches full regexps.\n"; |
42 | #else | 42 | #else |
43 | "This version of grep matches strings (not full regexps).\n"; | 43 | "This version of grep matches strings (not regexps).\n"; |
44 | #endif | 44 | #endif |
45 | 45 | ||
46 | int tellName=TRUE; | ||
47 | int ignoreCase=FALSE; | ||
48 | int tellLine=FALSE; | ||
49 | 46 | ||
50 | static do_grep(char* needle, char* haystack ) | 47 | static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine) |
51 | { | 48 | { |
52 | line = 0; | 49 | char *cp; |
50 | long line = 0; | ||
51 | char haystack[BUF_SIZE]; | ||
53 | 52 | ||
54 | while (fgets (haystack, sizeof (haystack), fp)) { | 53 | while (fgets (haystack, sizeof (haystack), fp)) { |
55 | line++; | 54 | line++; |
56 | cp = &haystack[strlen (haystack) - 1]; | 55 | cp = &haystack[strlen (haystack) - 1]; |
57 | 56 | ||
58 | if (*cp != '\n') | 57 | if (*cp != '\n') |
59 | fprintf (stderr, "%s: Line too long\n", name); | 58 | fprintf (stderr, "%s: Line too long\n", fileName); |
60 | 59 | ||
61 | if (find_match(haystack, needle, ignoreCase) == TRUE) { | 60 | if (find_match(haystack, needle, ignoreCase) == TRUE) { |
62 | if (tellName==TRUE) | 61 | if (tellName==TRUE) |
63 | printf ("%s: ", name); | 62 | printf ("%s:", fileName); |
64 | 63 | ||
65 | if (tellLine==TRUE) | 64 | if (tellLine==TRUE) |
66 | printf ("%ld: ", line); | 65 | printf ("%ld:", line); |
67 | 66 | ||
68 | fputs (haystack, stdout); | 67 | fputs (haystack, stdout); |
69 | } | ||
70 | } | 68 | } |
69 | } | ||
71 | } | 70 | } |
72 | 71 | ||
73 | 72 | ||
74 | extern int grep_main (int argc, char **argv) | 73 | extern int grep_main (int argc, char **argv) |
75 | { | 74 | { |
76 | FILE *fp; | 75 | FILE *fp; |
77 | char *needle; | ||
78 | char *name; | ||
79 | char *cp; | 76 | char *cp; |
80 | long line; | 77 | char *needle; |
81 | char haystack[BUF_SIZE]; | 78 | char *fileName; |
79 | int tellName=FALSE; | ||
80 | int ignoreCase=FALSE; | ||
81 | int tellLine=FALSE; | ||
82 | |||
82 | 83 | ||
83 | ignoreCase = FALSE; | 84 | ignoreCase = FALSE; |
84 | tellLine = FALSE; | 85 | tellLine = FALSE; |
@@ -100,7 +101,7 @@ extern int grep_main (int argc, char **argv) | |||
100 | break; | 101 | break; |
101 | 102 | ||
102 | case 'h': | 103 | case 'h': |
103 | tellName = FALSE; | 104 | tellName = TRUE; |
104 | break; | 105 | break; |
105 | 106 | ||
106 | case 'n': | 107 | case 'n': |
@@ -115,28 +116,24 @@ extern int grep_main (int argc, char **argv) | |||
115 | needle = *argv++; | 116 | needle = *argv++; |
116 | argc--; | 117 | argc--; |
117 | 118 | ||
119 | if (argc==0) { | ||
120 | do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine); | ||
121 | } else { | ||
122 | while (argc-- > 0) { | ||
123 | fileName = *argv++; | ||
118 | 124 | ||
119 | while (argc-- > 0) { | 125 | fp = fopen (fileName, "r"); |
120 | 126 | if (fp == NULL) { | |
121 | if (argc==0) { | 127 | perror (fileName); |
122 | file = stdin; | 128 | continue; |
123 | } | 129 | } |
124 | else | ||
125 | file = fopen(*argv, "r"); | ||
126 | |||
127 | 130 | ||
128 | name = *argv++; | 131 | do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine); |
129 | 132 | ||
130 | fp = fopen (name, "r"); | 133 | if (ferror (fp)) |
131 | if (fp == NULL) { | 134 | perror (fileName); |
132 | perror (name); | 135 | fclose (fp); |
133 | continue; | ||
134 | } | 136 | } |
135 | |||
136 | if (ferror (fp)) | ||
137 | perror (name); | ||
138 | |||
139 | fclose (fp); | ||
140 | } | 137 | } |
141 | exit( TRUE); | 138 | exit( TRUE); |
142 | } | 139 | } |