diff options
| author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
|---|---|---|
| committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
| commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
| tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /findutils | |
| parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
| download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip | |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'findutils')
| -rw-r--r-- | findutils/find.c | 142 | ||||
| -rw-r--r-- | findutils/grep.c | 182 |
2 files changed, 164 insertions, 160 deletions
diff --git a/findutils/find.c b/findutils/find.c index 50872b7f1..6346d454f 100644 --- a/findutils/find.c +++ b/findutils/find.c | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 1 | /* | 2 | /* |
| 2 | * Mini find implementation for busybox | 3 | * Mini find implementation for busybox |
| 3 | * | 4 | * |
| @@ -28,92 +29,95 @@ | |||
| 28 | #include <dirent.h> | 29 | #include <dirent.h> |
| 29 | 30 | ||
| 30 | 31 | ||
| 31 | static char* pattern=NULL; | 32 | static char *pattern = NULL; |
| 32 | static char* directory="."; | 33 | static char *directory = "."; |
| 33 | static int dereferenceFlag=FALSE; | 34 | static int dereferenceFlag = FALSE; |
| 34 | 35 | ||
| 35 | static const char find_usage[] = "find [PATH...] [EXPRESSION]\n\n" | 36 | static const char find_usage[] = "find [PATH...] [EXPRESSION]\n\n" |
| 36 | "Search for files in a directory hierarchy. The default PATH is\n" | 37 | "Search for files in a directory hierarchy. The default PATH is\n" |
| 37 | "the current directory; default EXPRESSION is '-print'\n\n" | 38 | "the current directory; default EXPRESSION is '-print'\n\n" |
| 38 | "\nEXPRESSION may consist of:\n" | 39 | "\nEXPRESSION may consist of:\n" |
| 39 | "\t-follow\n\t\tDereference symbolic links.\n" | 40 | "\t-follow\n\t\tDereference symbolic links.\n" |
| 40 | "\t-name PATTERN\n\t\tFile name (with leading directories removed) matches PATTERN.\n" | 41 | "\t-name PATTERN\n\t\tFile name (with leading directories removed) matches PATTERN.\n" |
| 41 | "\t-print\n\t\tprint the full file name followed by a newline to stdout.\n\n" | 42 | "\t-print\n\t\tprint the full file name followed by a newline to stdout.\n\n" |
| 42 | #if defined BB_REGEXP | 43 | #if defined BB_REGEXP |
| 43 | "This version of find matches full regular expresions.\n"; | 44 | "This version of find matches full regular expresions.\n"; |
| 44 | #else | 45 | #else |
| 45 | "This version of find matches strings (not regular expresions).\n"; | 46 | "This version of find matches strings (not regular expresions).\n"; |
| 46 | #endif | 47 | #endif |
| 47 | 48 | ||
| 48 | 49 | ||
| 49 | 50 | ||
| 50 | static int fileAction(const char *fileName, struct stat* statbuf) | 51 | static int fileAction(const char *fileName, struct stat *statbuf) |
| 51 | { | 52 | { |
| 52 | if (pattern==NULL) | 53 | if (pattern == NULL) |
| 53 | fprintf(stdout, "%s\n", fileName); | 54 | fprintf(stdout, "%s\n", fileName); |
| 54 | else { | 55 | else { |
| 55 | char* tmp = strrchr( fileName, '/'); | 56 | char *tmp = strrchr(fileName, '/'); |
| 56 | if (tmp == NULL) | 57 | |
| 57 | tmp = (char*)fileName; | 58 | if (tmp == NULL) |
| 58 | else | 59 | tmp = (char *) fileName; |
| 59 | tmp++; | 60 | else |
| 60 | if (check_wildcard_match(tmp, pattern) == TRUE) | 61 | tmp++; |
| 61 | fprintf(stdout, "%s\n", fileName); | 62 | if (check_wildcard_match(tmp, pattern) == TRUE) |
| 62 | } | 63 | fprintf(stdout, "%s\n", fileName); |
| 63 | return( TRUE); | 64 | } |
| 65 | return (TRUE); | ||
| 64 | } | 66 | } |
| 65 | 67 | ||
| 66 | int find_main(int argc, char **argv) | 68 | int find_main(int argc, char **argv) |
| 67 | { | 69 | { |
| 68 | /* peel off the "find" */ | 70 | /* peel off the "find" */ |
| 69 | argc--; | ||
| 70 | argv++; | ||
| 71 | |||
| 72 | if ( argc > 0 && **argv != '-') { | ||
| 73 | directory = *argv; | ||
| 74 | argc--; | 71 | argc--; |
| 75 | argv++; | 72 | argv++; |
| 76 | } | ||
| 77 | 73 | ||
| 78 | /* Parse any options */ | 74 | if (argc > 0 && **argv != '-') { |
| 79 | while (argc > 0 && **argv == '-') { | 75 | directory = *argv; |
| 80 | int stopit=FALSE; | 76 | argc--; |
| 81 | while (*++(*argv) && stopit==FALSE) switch (**argv) { | 77 | argv++; |
| 82 | case 'f': | ||
| 83 | if (strcmp(*argv, "follow")==0) { | ||
| 84 | argc--; | ||
| 85 | argv++; | ||
| 86 | dereferenceFlag=TRUE; | ||
| 87 | } | ||
| 88 | break; | ||
| 89 | case 'n': | ||
| 90 | if (strcmp(*argv, "name")==0) { | ||
| 91 | if (argc-- > 1) { | ||
| 92 | pattern = *(++argv); | ||
| 93 | stopit = TRUE; | ||
| 94 | } else { | ||
| 95 | usage (find_usage); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | break; | ||
| 99 | case '-': | ||
| 100 | /* Ignore all long options */ | ||
| 101 | break; | ||
| 102 | default: | ||
| 103 | usage (find_usage); | ||
| 104 | } | 78 | } |
| 105 | if (argc-- > 1) | ||
| 106 | argv++; | ||
| 107 | if (**argv != '-') | ||
| 108 | break; | ||
| 109 | else | ||
| 110 | break; | ||
| 111 | } | ||
| 112 | 79 | ||
| 113 | if (recursiveAction(directory, TRUE, FALSE, FALSE, | 80 | /* Parse any options */ |
| 114 | fileAction, fileAction) == FALSE) { | 81 | while (argc > 0 && **argv == '-') { |
| 115 | exit( FALSE); | 82 | int stopit = FALSE; |
| 116 | } | 83 | |
| 84 | while (*++(*argv) && stopit == FALSE) | ||
| 85 | switch (**argv) { | ||
| 86 | case 'f': | ||
| 87 | if (strcmp(*argv, "follow") == 0) { | ||
| 88 | argc--; | ||
| 89 | argv++; | ||
| 90 | dereferenceFlag = TRUE; | ||
| 91 | } | ||
| 92 | break; | ||
| 93 | case 'n': | ||
| 94 | if (strcmp(*argv, "name") == 0) { | ||
| 95 | if (argc-- > 1) { | ||
| 96 | pattern = *(++argv); | ||
| 97 | stopit = TRUE; | ||
| 98 | } else { | ||
| 99 | usage(find_usage); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | break; | ||
| 103 | case '-': | ||
| 104 | /* Ignore all long options */ | ||
| 105 | break; | ||
| 106 | default: | ||
| 107 | usage(find_usage); | ||
| 108 | } | ||
| 109 | if (argc-- > 1) | ||
| 110 | argv++; | ||
| 111 | if (**argv != '-') | ||
| 112 | break; | ||
| 113 | else | ||
| 114 | break; | ||
| 115 | } | ||
| 116 | |||
| 117 | if (recursiveAction(directory, TRUE, FALSE, FALSE, | ||
| 118 | fileAction, fileAction) == FALSE) { | ||
| 119 | exit(FALSE); | ||
| 120 | } | ||
| 117 | 121 | ||
| 118 | exit(TRUE); | 122 | exit(TRUE); |
| 119 | } | 123 | } |
diff --git a/findutils/grep.c b/findutils/grep.c index 287d9f80d..d8d2f1837 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
| @@ -1,3 +1,4 @@ | |||
| 1 | /* vi: set sw=4 ts=4: */ | ||
| 1 | /* | 2 | /* |
| 2 | * Mini grep implementation for busybox | 3 | * Mini grep implementation for busybox |
| 3 | * | 4 | * |
| @@ -40,126 +41,125 @@ | |||
| 40 | #include <ctype.h> | 41 | #include <ctype.h> |
| 41 | 42 | ||
| 42 | static const char grep_usage[] = | 43 | static const char grep_usage[] = |
| 43 | "grep [OPTIONS]... PATTERN [FILE]...\n\n" | 44 | "grep [OPTIONS]... PATTERN [FILE]...\n\n" |
| 44 | "Search for PATTERN in each FILE or standard input.\n\n" | 45 | "Search for PATTERN in each FILE or standard input.\n\n" |
| 45 | "OPTIONS:\n" | 46 | "OPTIONS:\n" |
| 46 | "\t-h\tsuppress the prefixing filename on output\n" | 47 | "\t-h\tsuppress the prefixing filename on output\n" |
| 47 | "\t-i\tignore case distinctions\n" | 48 | "\t-i\tignore case distinctions\n" |
| 48 | "\t-n\tprint line number with output lines\n" | 49 | "\t-n\tprint line number with output lines\n" |
| 49 | "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" | 50 | "\t-q\tbe quiet. Returns 0 if result was found, 1 otherwise\n\n" |
| 50 | #if defined BB_REGEXP | 51 | #if defined BB_REGEXP |
| 51 | "This version of grep matches full regular expresions.\n"; | 52 | "This version of grep matches full regular expresions.\n"; |
| 52 | #else | 53 | #else |
| 53 | "This version of grep matches strings (not regular expresions).\n"; | 54 | "This version of grep matches strings (not regular expresions).\n"; |
| 54 | #endif | 55 | #endif |
| 55 | 56 | ||
| 56 | static int match = FALSE, beQuiet = FALSE; | 57 | static int match = FALSE, beQuiet = FALSE; |
| 57 | 58 | ||
| 58 | static void do_grep(FILE *fp, char* needle, char *fileName, int tellName, int ignoreCase, int tellLine) | 59 | static void do_grep(FILE * fp, char *needle, char *fileName, int tellName, |
| 60 | int ignoreCase, int tellLine) | ||
| 59 | { | 61 | { |
| 60 | char *cp; | 62 | char *cp; |
| 61 | long line = 0; | 63 | long line = 0; |
| 62 | char haystack[BUF_SIZE]; | 64 | char haystack[BUF_SIZE]; |
| 63 | 65 | ||
| 64 | while (fgets (haystack, sizeof (haystack), fp)) { | 66 | while (fgets(haystack, sizeof(haystack), fp)) { |
| 65 | line++; | 67 | line++; |
| 66 | cp = &haystack[strlen (haystack) - 1]; | 68 | cp = &haystack[strlen(haystack) - 1]; |
| 67 | 69 | ||
| 68 | if (*cp != '\n') | 70 | if (*cp != '\n') |
| 69 | fprintf (stderr, "%s: Line too long\n", fileName); | 71 | fprintf(stderr, "%s: Line too long\n", fileName); |
| 70 | 72 | ||
| 71 | if (find_match(haystack, needle, ignoreCase) == TRUE) { | 73 | if (find_match(haystack, needle, ignoreCase) == TRUE) { |
| 72 | if (tellName==TRUE) | 74 | if (tellName == TRUE) |
| 73 | printf ("%s:", fileName); | 75 | printf("%s:", fileName); |
| 74 | 76 | ||
| 75 | if (tellLine==TRUE) | 77 | if (tellLine == TRUE) |
| 76 | printf ("%ld:", line); | 78 | printf("%ld:", line); |
| 77 | 79 | ||
| 78 | if (beQuiet==FALSE) | 80 | if (beQuiet == FALSE) |
| 79 | fputs (haystack, stdout); | 81 | fputs(haystack, stdout); |
| 80 | 82 | ||
| 81 | match = TRUE; | 83 | match = TRUE; |
| 84 | } | ||
| 82 | } | 85 | } |
| 83 | } | ||
| 84 | } | 86 | } |
| 85 | 87 | ||
| 86 | 88 | ||
| 87 | extern int grep_main (int argc, char **argv) | 89 | extern int grep_main(int argc, char **argv) |
| 88 | { | 90 | { |
| 89 | FILE *fp; | 91 | FILE *fp; |
| 90 | char *cp; | 92 | char *cp; |
| 91 | char *needle; | 93 | char *needle; |
| 92 | char *fileName; | 94 | char *fileName; |
| 93 | int tellName=TRUE; | 95 | int tellName = TRUE; |
| 94 | int ignoreCase=TRUE; | 96 | int ignoreCase = TRUE; |
| 95 | int tellLine=FALSE; | 97 | int tellLine = FALSE; |
| 96 | 98 | ||
| 97 | 99 | ||
| 98 | ignoreCase = FALSE; | 100 | ignoreCase = FALSE; |
| 99 | tellLine = FALSE; | 101 | tellLine = FALSE; |
| 100 | 102 | ||
| 101 | argc--; | ||
| 102 | argv++; | ||
| 103 | if (argc < 1) { | ||
| 104 | usage(grep_usage); | ||
| 105 | } | ||
| 106 | |||
| 107 | if (**argv == '-') { | ||
| 108 | argc--; | 103 | argc--; |
| 109 | cp = *argv++; | 104 | argv++; |
| 105 | if (argc < 1) { | ||
| 106 | usage(grep_usage); | ||
| 107 | } | ||
| 110 | 108 | ||
| 111 | while (*++cp) | 109 | if (**argv == '-') { |
| 112 | switch (*cp) { | 110 | argc--; |
| 113 | case 'i': | 111 | cp = *argv++; |
| 114 | ignoreCase = TRUE; | ||
| 115 | break; | ||
| 116 | 112 | ||
| 117 | case 'h': | 113 | while (*++cp) |
| 118 | tellName = FALSE; | 114 | switch (*cp) { |
| 119 | break; | 115 | case 'i': |
| 116 | ignoreCase = TRUE; | ||
| 117 | break; | ||
| 120 | 118 | ||
| 121 | case 'n': | 119 | case 'h': |
| 122 | tellLine = TRUE; | 120 | tellName = FALSE; |
| 123 | break; | 121 | break; |
| 124 | 122 | ||
| 125 | case 'q': | 123 | case 'n': |
| 126 | beQuiet = TRUE; | 124 | tellLine = TRUE; |
| 127 | break; | 125 | break; |
| 128 | 126 | ||
| 129 | default: | 127 | case 'q': |
| 130 | usage(grep_usage); | 128 | beQuiet = TRUE; |
| 131 | } | 129 | break; |
| 132 | } | 130 | |
| 133 | 131 | default: | |
| 134 | needle = *argv++; | 132 | usage(grep_usage); |
| 135 | argc--; | 133 | } |
| 136 | |||
| 137 | if (argc==0) { | ||
| 138 | do_grep( stdin, needle, "stdin", FALSE, ignoreCase, tellLine); | ||
| 139 | } else { | ||
| 140 | /* Never print the filename for just one file */ | ||
| 141 | if (argc==1) | ||
| 142 | tellName=FALSE; | ||
| 143 | while (argc-- > 0) { | ||
| 144 | fileName = *argv++; | ||
| 145 | |||
| 146 | fp = fopen (fileName, "r"); | ||
| 147 | if (fp == NULL) { | ||
| 148 | perror (fileName); | ||
| 149 | continue; | ||
| 150 | } | ||
| 151 | |||
| 152 | do_grep( fp, needle, fileName, tellName, ignoreCase, tellLine); | ||
| 153 | |||
| 154 | if (ferror (fp)) | ||
| 155 | perror (fileName); | ||
| 156 | fclose (fp); | ||
| 157 | } | 134 | } |
| 158 | } | ||
| 159 | exit(match); | ||
| 160 | } | ||
| 161 | 135 | ||
| 136 | needle = *argv++; | ||
| 137 | argc--; | ||
| 162 | 138 | ||
| 163 | /* END CODE */ | 139 | if (argc == 0) { |
| 140 | do_grep(stdin, needle, "stdin", FALSE, ignoreCase, tellLine); | ||
| 141 | } else { | ||
| 142 | /* Never print the filename for just one file */ | ||
| 143 | if (argc == 1) | ||
| 144 | tellName = FALSE; | ||
| 145 | while (argc-- > 0) { | ||
| 146 | fileName = *argv++; | ||
| 147 | |||
| 148 | fp = fopen(fileName, "r"); | ||
| 149 | if (fp == NULL) { | ||
| 150 | perror(fileName); | ||
| 151 | continue; | ||
| 152 | } | ||
| 153 | |||
| 154 | do_grep(fp, needle, fileName, tellName, ignoreCase, tellLine); | ||
| 155 | |||
| 156 | if (ferror(fp)) | ||
| 157 | perror(fileName); | ||
| 158 | fclose(fp); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | exit(match); | ||
| 162 | } | ||
| 164 | 163 | ||
| 165 | 164 | ||
| 165 | /* END CODE */ | ||
