diff options
| author | Eric Andersen <andersen@codepoet.org> | 1999-10-19 06:02:44 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 1999-10-19 06:02:44 +0000 |
| commit | 3e0fbae15eca940c70075c8eade3f2c735fa34b6 (patch) | |
| tree | 4ce22d21a451f695eaf1ea8ef446438414984ef3 /findutils | |
| parent | abc7d597cb78a0ee3e92ecaeb73f6cbb2a3412bf (diff) | |
| download | busybox-w32-3e0fbae15eca940c70075c8eade3f2c735fa34b6.tar.gz busybox-w32-3e0fbae15eca940c70075c8eade3f2c735fa34b6.tar.bz2 busybox-w32-3e0fbae15eca940c70075c8eade3f2c735fa34b6.zip | |
fixes grep, added loadfont from debian bootfloppies.
-Erik
Diffstat (limited to 'findutils')
| -rw-r--r-- | findutils/grep.c | 153 |
1 files changed, 58 insertions, 95 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 657bb2570..de1c820a0 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
| @@ -1,17 +1,25 @@ | |||
| 1 | /* | 1 | /* |
| 2 | * Copyright (c) 1999 by David I. Bell | 2 | * Mini grep implementation for busybox |
| 3 | * Permission is granted to use, distribute, or modify this source, | ||
| 4 | * provided that this copyright notice remains intact. | ||
| 5 | * | 3 | * |
| 6 | * The "grep" command, taken from sash. | 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| 7 | * This provides basic file searching. | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation; either version 2 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 14 | * General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program; if not, write to the Free Software | ||
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 8 | * | 19 | * |
| 9 | * Permission to distribute this code under the GPL has been granted. | ||
| 10 | * Modified for busybox by Erik Andersen <andersee@debian.org> <andersen@lineo.com> | ||
| 11 | */ | 20 | */ |
| 12 | 21 | ||
| 13 | #include "internal.h" | 22 | #include "internal.h" |
| 14 | |||
| 15 | #include <stdio.h> | 23 | #include <stdio.h> |
| 16 | #include <dirent.h> | 24 | #include <dirent.h> |
| 17 | #include <errno.h> | 25 | #include <errno.h> |
| @@ -22,95 +30,53 @@ | |||
| 22 | 30 | ||
| 23 | 31 | ||
| 24 | const char grep_usage[] = | 32 | const char grep_usage[] = |
| 25 | "Search the input file(s) for lines matching the given pattern.\n" | 33 | "grep [-ihn]... PATTERN [FILE]...\n" |
| 26 | "\tI search stdin if no files are given.\n" | 34 | "Search for PATTERN in each FILE or standard input.\n\n" |
| 27 | "\tI can't grok full regular expressions.\n" | 35 | "\t-h\tsuppress the prefixing filename on output\n" |
| 28 | "usage: grep [in] PATTERN [FILES]...\n" | 36 | "\t-i\tignore case distinctions\n" |
| 29 | "\ti=ignore case, n=list line numbers\n"; | 37 | "\t-n\tprint line number with output lines\n\n" |
| 30 | 38 | "This version of grep matches strings (not full regexps).\n"; | |
| 31 | 39 | ||
| 32 | 40 | ||
| 33 | /* | 41 | /* |
| 34 | * See if the specified word is found in the specified string. | 42 | * See if the specified needle is found in the specified haystack. |
| 35 | */ | 43 | */ |
| 36 | static int search (const char *string, const char *word, int ignoreCase) | 44 | static int search (const char *haystack, const char *needle, int ignoreCase) |
| 37 | { | 45 | { |
| 38 | const char *cp1; | ||
| 39 | const char *cp2; | ||
| 40 | int len; | ||
| 41 | int lowFirst; | ||
| 42 | int ch1; | ||
| 43 | int ch2; | ||
| 44 | |||
| 45 | len = strlen (word); | ||
| 46 | |||
| 47 | if (!ignoreCase) { | ||
| 48 | while (TRUE) { | ||
| 49 | string = strchr (string, word[0]); | ||
| 50 | |||
| 51 | if (string == NULL) | ||
| 52 | return FALSE; | ||
| 53 | |||
| 54 | if (memcmp (string, word, len) == 0) | ||
| 55 | return TRUE; | ||
| 56 | |||
| 57 | string++; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | /* | ||
| 62 | * Here if we need to check case independence. | ||
| 63 | * Do the search by lower casing both strings. | ||
| 64 | */ | ||
| 65 | lowFirst = *word; | ||
| 66 | |||
| 67 | if (isupper (lowFirst)) | ||
| 68 | lowFirst = tolower (lowFirst); | ||
| 69 | |||
| 70 | while (TRUE) { | ||
| 71 | while (*string && (*string != lowFirst) && | ||
| 72 | (!isupper (*string) || (tolower (*string) != lowFirst))) { | ||
| 73 | string++; | ||
| 74 | } | ||
| 75 | 46 | ||
| 76 | if (*string == '\0') | 47 | if (ignoreCase == FALSE) { |
| 48 | haystack = strstr (haystack, needle); | ||
| 49 | if (haystack == NULL) | ||
| 77 | return FALSE; | 50 | return FALSE; |
| 78 | 51 | return TRUE; | |
| 79 | cp1 = string; | 52 | } else { |
| 80 | cp2 = word; | 53 | int i; |
| 81 | 54 | char needle1[BUF_SIZE]; | |
| 82 | do { | 55 | char haystack1[BUF_SIZE]; |
| 83 | if (*cp2 == '\0') | 56 | |
| 84 | return TRUE; | 57 | strncpy( haystack1, haystack, sizeof(haystack1)); |
| 85 | 58 | strncpy( needle1, needle, sizeof(needle1)); | |
| 86 | ch1 = *cp1++; | 59 | for( i=0; i<sizeof(haystack1) && haystack1[i]; i++) |
| 87 | 60 | haystack1[i]=tolower( haystack1[i]); | |
| 88 | if (isupper (ch1)) | 61 | for( i=0; i<sizeof(needle1) && needle1[i]; i++) |
| 89 | ch1 = tolower (ch1); | 62 | needle1[i]=tolower( needle1[i]); |
| 90 | 63 | haystack = strstr (haystack1, needle1); | |
| 91 | ch2 = *cp2++; | 64 | if (haystack == NULL) |
| 92 | 65 | return FALSE; | |
| 93 | if (isupper (ch2)) | 66 | return TRUE; |
| 94 | ch2 = tolower (ch2); | ||
| 95 | |||
| 96 | } | ||
| 97 | while (ch1 == ch2); | ||
| 98 | |||
| 99 | string++; | ||
| 100 | } | 67 | } |
| 101 | return (TRUE); | ||
| 102 | } | 68 | } |
| 103 | 69 | ||
| 104 | 70 | ||
| 105 | extern int grep_main (int argc, char **argv) | 71 | extern int grep_main (int argc, char **argv) |
| 106 | { | 72 | { |
| 107 | FILE *fp; | 73 | FILE *fp; |
| 108 | const char *word; | 74 | const char *needle; |
| 109 | const char *name; | 75 | const char *name; |
| 110 | const char *cp; | 76 | const char *cp; |
| 111 | int tellName; | 77 | int tellName=TRUE; |
| 112 | int ignoreCase; | 78 | int ignoreCase=FALSE; |
| 113 | int tellLine; | 79 | int tellLine=FALSE; |
| 114 | long line; | 80 | long line; |
| 115 | char buf[BUF_SIZE]; | 81 | char buf[BUF_SIZE]; |
| 116 | 82 | ||
| @@ -120,8 +86,7 @@ extern int grep_main (int argc, char **argv) | |||
| 120 | argc--; | 86 | argc--; |
| 121 | argv++; | 87 | argv++; |
| 122 | if (argc < 1) { | 88 | if (argc < 1) { |
| 123 | fprintf (stderr, "%s", grep_usage); | 89 | usage(grep_usage); |
| 124 | return 1; | ||
| 125 | } | 90 | } |
| 126 | 91 | ||
| 127 | if (**argv == '-') { | 92 | if (**argv == '-') { |
| @@ -134,29 +99,28 @@ extern int grep_main (int argc, char **argv) | |||
| 134 | ignoreCase = TRUE; | 99 | ignoreCase = TRUE; |
| 135 | break; | 100 | break; |
| 136 | 101 | ||
| 102 | case 'h': | ||
| 103 | tellName = FALSE; | ||
| 104 | break; | ||
| 105 | |||
| 137 | case 'n': | 106 | case 'n': |
| 138 | tellLine = TRUE; | 107 | tellLine = TRUE; |
| 139 | break; | 108 | break; |
| 140 | 109 | ||
| 141 | default: | 110 | default: |
| 142 | fprintf (stderr, "Unknown option\n"); | 111 | usage(grep_usage); |
| 143 | return 1; | ||
| 144 | } | 112 | } |
| 145 | } | 113 | } |
| 146 | 114 | ||
| 147 | word = *argv++; | 115 | needle = *argv++; |
| 148 | argc--; | 116 | argc--; |
| 149 | 117 | ||
| 150 | tellName = (argc > 1); | ||
| 151 | |||
| 152 | while (argc-- > 0) { | 118 | while (argc-- > 0) { |
| 153 | name = *argv++; | 119 | name = *argv++; |
| 154 | 120 | ||
| 155 | fp = fopen (name, "r"); | 121 | fp = fopen (name, "r"); |
| 156 | |||
| 157 | if (fp == NULL) { | 122 | if (fp == NULL) { |
| 158 | perror (name); | 123 | perror (name); |
| 159 | |||
| 160 | continue; | 124 | continue; |
| 161 | } | 125 | } |
| 162 | 126 | ||
| @@ -164,17 +128,16 @@ extern int grep_main (int argc, char **argv) | |||
| 164 | 128 | ||
| 165 | while (fgets (buf, sizeof (buf), fp)) { | 129 | while (fgets (buf, sizeof (buf), fp)) { |
| 166 | line++; | 130 | line++; |
| 167 | |||
| 168 | cp = &buf[strlen (buf) - 1]; | 131 | cp = &buf[strlen (buf) - 1]; |
| 169 | 132 | ||
| 170 | if (*cp != '\n') | 133 | if (*cp != '\n') |
| 171 | fprintf (stderr, "%s: Line too long\n", name); | 134 | fprintf (stderr, "%s: Line too long\n", name); |
| 172 | 135 | ||
| 173 | if (search (buf, word, ignoreCase)) { | 136 | if (search (buf, needle, ignoreCase)==TRUE) { |
| 174 | if (tellName) | 137 | if (tellName==TRUE) |
| 175 | printf ("%s: ", name); | 138 | printf ("%s: ", name); |
| 176 | 139 | ||
| 177 | if (tellLine) | 140 | if (tellLine==TRUE) |
| 178 | printf ("%ld: ", line); | 141 | printf ("%ld: ", line); |
| 179 | 142 | ||
| 180 | fputs (buf, stdout); | 143 | fputs (buf, stdout); |
| @@ -186,7 +149,7 @@ extern int grep_main (int argc, char **argv) | |||
| 186 | 149 | ||
| 187 | fclose (fp); | 150 | fclose (fp); |
| 188 | } | 151 | } |
| 189 | return 0; | 152 | exit( TRUE); |
| 190 | } | 153 | } |
| 191 | 154 | ||
| 192 | 155 | ||
