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 | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 7 | * |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Erik Andersen | 3fe2ecf | 1999-12-21 08:52:04 +0000 | [diff] [blame] | 24 | /* |
| 25 | 18-Dec-1999 Konstantin Boldyshev <konst@voshod.com> |
| 26 | |
| 27 | + -q option (be quiet) |
| 28 | + exit code depending on grep result (TRUE or FALSE) |
| 29 | (useful for scripts) |
| 30 | */ |
| 31 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 32 | #include "internal.h" |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 33 | #include "regexp.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
| 35 | #include <dirent.h> |
| 36 | #include <errno.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <signal.h> |
| 39 | #include <time.h> |
| 40 | #include <ctype.h> |
| 41 | |
Eric Andersen | e77ae3a | 1999-10-19 20:03:34 +0000 | [diff] [blame] | 42 | static const char grep_usage[] = |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 43 | "grep [OPTIONS]... PATTERN [FILE]...\n\n" |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 44 | "Search for PATTERN in each FILE or standard input.\n\n" |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 45 | "OPTIONS:\n" |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 46 | "\t-h\tsuppress the prefixing filename on output\n" |
| 47 | "\t-i\tignore case distinctions\n" |
Erik Andersen | 3fe2ecf | 1999-12-21 08:52:04 +0000 | [diff] [blame] | 48 | "\t-n\tprint line number with output lines\n" |
Erik Andersen | 2fe08c7 | 1999-12-29 02:10:35 +0000 | [diff] [blame] | 49 | "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 50 | #if defined BB_REGEXP |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 51 | "This version of grep matches full regular expresions.\n"; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 52 | #else |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 53 | "This version of grep matches strings (not regular expresions).\n"; |
Eric Andersen | aa0765e | 1999-10-22 04:30:20 +0000 | [diff] [blame] | 54 | #endif |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | |
Erik Andersen | 3fe2ecf | 1999-12-21 08:52:04 +0000 | [diff] [blame] | 56 | static int match = FALSE, beQuiet = FALSE; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 57 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 58 | static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine) |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 59 | { |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 60 | char *cp; |
| 61 | long line = 0; |
| 62 | char haystack[BUF_SIZE]; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 64 | while (fgets (haystack, sizeof (haystack), fp)) { |
| 65 | line++; |
| 66 | cp = &haystack[strlen (haystack) - 1]; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 67 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 68 | if (*cp != '\n') |
| 69 | fprintf (stderr, "%s: Line too long\n", fileName); |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 70 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 71 | if (find_match(haystack, needle, ignoreCase) == TRUE) { |
| 72 | if (tellName==TRUE) |
| 73 | printf ("%s:", fileName); |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 74 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 75 | if (tellLine==TRUE) |
| 76 | printf ("%ld:", line); |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 77 | |
Erik Andersen | 3fe2ecf | 1999-12-21 08:52:04 +0000 | [diff] [blame] | 78 | if (beQuiet==FALSE) |
| 79 | fputs (haystack, stdout); |
| 80 | |
| 81 | match = TRUE; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 82 | } |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 83 | } |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 86 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 87 | extern int grep_main (int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 88 | { |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 89 | FILE *fp; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 90 | char *cp; |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 91 | char *needle; |
| 92 | char *fileName; |
| 93 | int tellName=FALSE; |
| 94 | int ignoreCase=FALSE; |
| 95 | int tellLine=FALSE; |
| 96 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 97 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 98 | ignoreCase = FALSE; |
| 99 | tellLine = FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 101 | argc--; |
| 102 | argv++; |
| 103 | if (argc < 1) { |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 104 | usage(grep_usage); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (**argv == '-') { |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 108 | argc--; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 109 | cp = *argv++; |
| 110 | |
| 111 | while (*++cp) |
| 112 | switch (*cp) { |
| 113 | case 'i': |
| 114 | ignoreCase = TRUE; |
| 115 | break; |
| 116 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 117 | case 'h': |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 118 | tellName = TRUE; |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 119 | break; |
| 120 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 121 | case 'n': |
| 122 | tellLine = TRUE; |
| 123 | break; |
| 124 | |
Erik Andersen | 3fe2ecf | 1999-12-21 08:52:04 +0000 | [diff] [blame] | 125 | case 'q': |
| 126 | beQuiet = TRUE; |
| 127 | break; |
| 128 | |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 129 | default: |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 130 | usage(grep_usage); |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
Eric Andersen | 3e0fbae | 1999-10-19 06:02:44 +0000 | [diff] [blame] | 134 | needle = *argv++; |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 135 | argc--; |
| 136 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 137 | if (argc==0) { |
| 138 | do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine); |
| 139 | } else { |
| 140 | while (argc-- > 0) { |
| 141 | fileName = *argv++; |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 142 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 143 | fp = fopen (fileName, "r"); |
| 144 | if (fp == NULL) { |
| 145 | perror (fileName); |
| 146 | continue; |
| 147 | } |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 148 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame] | 149 | do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine); |
| 150 | |
| 151 | if (ferror (fp)) |
| 152 | perror (fileName); |
| 153 | fclose (fp); |
Eric Andersen | fbb39c8 | 1999-11-08 17:00:52 +0000 | [diff] [blame] | 154 | } |
Eric Andersen | 596e546 | 1999-10-07 08:30:23 +0000 | [diff] [blame] | 155 | } |
Erik Andersen | 3fe2ecf | 1999-12-21 08:52:04 +0000 | [diff] [blame] | 156 | exit(match); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 160 | /* END CODE */ |
Eric Andersen | 3cf52d1 | 1999-10-12 22:26:06 +0000 | [diff] [blame] | 161 | |
| 162 | |