| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 1 | /* |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 2 | * Mini grep implementation for busybox |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 3 | * |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 4 | * Copyright (C) 1998 by Erik Andersen <andersee@debian.org> |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 5 | * |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 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 |
| 19 | * |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "internal.h" |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <dirent.h> |
| 25 | #include <errno.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <signal.h> |
| 28 | #include <time.h> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | |
| Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 32 | static const char grep_usage[] = |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 33 | "grep [-ihn]... PATTERN [FILE]...\n" |
| 34 | "Search for PATTERN in each FILE or standard input.\n\n" |
| 35 | "\t-h\tsuppress the prefixing filename on output\n" |
| 36 | "\t-i\tignore case distinctions\n" |
| 37 | "\t-n\tprint line number with output lines\n\n" |
| 38 | "This version of grep matches strings (not full regexps).\n"; |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 39 | |
| 40 | |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 41 | /* |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 42 | * See if the specified needle is found in the specified haystack. |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 43 | */ |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 44 | static int search (const char *haystack, const char *needle, int ignoreCase) |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 45 | { |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 46 | |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 47 | if (ignoreCase == FALSE) { |
| 48 | haystack = strstr (haystack, needle); |
| 49 | if (haystack == NULL) |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 50 | return FALSE; |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 51 | return TRUE; |
| 52 | } else { |
| 53 | int i; |
| 54 | char needle1[BUF_SIZE]; |
| 55 | char haystack1[BUF_SIZE]; |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 56 | |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 57 | strncpy( haystack1, haystack, sizeof(haystack1)); |
| 58 | strncpy( needle1, needle, sizeof(needle1)); |
| 59 | for( i=0; i<sizeof(haystack1) && haystack1[i]; i++) |
| 60 | haystack1[i]=tolower( haystack1[i]); |
| 61 | for( i=0; i<sizeof(needle1) && needle1[i]; i++) |
| 62 | needle1[i]=tolower( needle1[i]); |
| 63 | haystack = strstr (haystack1, needle1); |
| 64 | if (haystack == NULL) |
| 65 | return FALSE; |
| 66 | return TRUE; |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 67 | } |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 68 | } |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 69 | |
| 70 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 71 | extern int grep_main (int argc, char **argv) |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 72 | { |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 73 | FILE *fp; |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 74 | const char *needle; |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 75 | const char *name; |
| 76 | const char *cp; |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 77 | int tellName=TRUE; |
| 78 | int ignoreCase=FALSE; |
| 79 | int tellLine=FALSE; |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 80 | long line; |
| 81 | char buf[BUF_SIZE]; |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 82 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 83 | ignoreCase = FALSE; |
| 84 | tellLine = FALSE; |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 86 | argc--; |
| 87 | argv++; |
| 88 | if (argc < 1) { |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 89 | usage(grep_usage); |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | if (**argv == '-') { |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 93 | argc--; |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 94 | cp = *argv++; |
| 95 | |
| 96 | while (*++cp) |
| 97 | switch (*cp) { |
| 98 | case 'i': |
| 99 | ignoreCase = TRUE; |
| 100 | break; |
| 101 | |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 102 | case 'h': |
| 103 | tellName = FALSE; |
| 104 | break; |
| 105 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 106 | case 'n': |
| 107 | tellLine = TRUE; |
| 108 | break; |
| 109 | |
| 110 | default: |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 111 | usage(grep_usage); |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 115 | needle = *argv++; |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 116 | argc--; |
| 117 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 118 | while (argc-- > 0) { |
| 119 | name = *argv++; |
| 120 | |
| 121 | fp = fopen (name, "r"); |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 122 | if (fp == NULL) { |
| 123 | perror (name); |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 124 | continue; |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 127 | line = 0; |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 128 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 129 | while (fgets (buf, sizeof (buf), fp)) { |
| 130 | line++; |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 131 | cp = &buf[strlen (buf) - 1]; |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 132 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 133 | if (*cp != '\n') |
| 134 | fprintf (stderr, "%s: Line too long\n", name); |
| 135 | |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 136 | if (search (buf, needle, ignoreCase)==TRUE) { |
| 137 | if (tellName==TRUE) |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 138 | printf ("%s: ", name); |
| 139 | |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 140 | if (tellLine==TRUE) |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 141 | printf ("%ld: ", line); |
| 142 | |
| 143 | fputs (buf, stdout); |
| 144 | } |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 147 | if (ferror (fp)) |
| 148 | perror (name); |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 149 | |
| Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 150 | fclose (fp); |
| 151 | } |
| Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 152 | exit( TRUE); |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | |
| Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 156 | /* END CODE */ |
| Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 157 | |
| 158 | |