diff options
| author | Eric Andersen <andersen@codepoet.org> | 2003-01-13 23:19:31 +0000 |
|---|---|---|
| committer | Eric Andersen <andersen@codepoet.org> | 2003-01-13 23:19:31 +0000 |
| commit | 65ddf77fbd0f1ec832f062cf05a15bbd2cf1080e (patch) | |
| tree | c71c597efb86273b1f5e6ecb13beac51abbb3221 | |
| parent | b4a1baa312746a1ddc934b1ddb035b64e72e746b (diff) | |
| download | busybox-w32-65ddf77fbd0f1ec832f062cf05a15bbd2cf1080e.tar.gz busybox-w32-65ddf77fbd0f1ec832f062cf05a15bbd2cf1080e.tar.bz2 busybox-w32-65ddf77fbd0f1ec832f062cf05a15bbd2cf1080e.zip | |
Patch from Tito to rework strings applet, and some testsuite
stuff to test it added by me.
| -rw-r--r-- | miscutils/strings.c | 133 | ||||
| -rw-r--r-- | testsuite/strings/strings-works-like-GNU | 8 |
2 files changed, 78 insertions, 63 deletions
diff --git a/miscutils/strings.c b/miscutils/strings.c index 649485033..9d8012aa2 100644 --- a/miscutils/strings.c +++ b/miscutils/strings.c | |||
| @@ -22,99 +22,106 @@ | |||
| 22 | * Original copyright notice is retained at the end of this file. | 22 | * Original copyright notice is retained at the end of this file. |
| 23 | * | 23 | * |
| 24 | * Modified for BusyBox by Erik Andersen <andersee@debian.org> | 24 | * Modified for BusyBox by Erik Andersen <andersee@debian.org> |
| 25 | * Badly hacked by Tito Ragusa <farmatito@tiscali.it> | ||
| 25 | */ | 26 | */ |
| 26 | 27 | ||
| 27 | #include <stdio.h> | 28 | #include <stdio.h> |
| 28 | #include <stdlib.h> | 29 | #include <stdlib.h> |
| 29 | #include <string.h> | 30 | #include <string.h> |
| 30 | #include <getopt.h> | 31 | #include <getopt.h> |
| 31 | #include <unistd.h> | 32 | #include <unistd.h> |
| 32 | #include <ctype.h> | 33 | #include <ctype.h> |
| 33 | #include "busybox.h" | 34 | #include "busybox.h" |
| 34 | 35 | ||
| 35 | #define ISSTR(ch) (isprint(ch) || ch == '\t') | 36 | #define ISSTR(ch) (isprint(ch) || ch == '\t') |
| 36 | 37 | ||
| 37 | int strings_main(int argc, char **argv) | 38 | int strings_main(int argc, char **argv) |
| 38 | { | 39 | { |
| 39 | extern char *optarg; | 40 | extern char *optarg; |
| 40 | extern int optind; | 41 | extern int optind; |
| 41 | int ch, cnt; | 42 | int n=4, c, i, opt=0, a=0; |
| 42 | int exitcode; | 43 | long t, count; |
| 43 | int oflg, fflg; | 44 | FILE *file; |
| 44 | char *file; | 45 | char *string; |
| 45 | size_t foff, minlen; | 46 | |
| 46 | unsigned char *bfr, *C; | 47 | while ((i = getopt(argc, argv, "an:of")) > 0) |
| 47 | int i; | 48 | switch(i) |
| 48 | 49 | { | |
| 49 | exitcode = fflg = oflg = 0; | ||
| 50 | minlen = -1; | ||
| 51 | while ((ch = getopt(argc, argv, "an:of")) > 0) | ||
| 52 | switch(ch) { | ||
| 53 | case 'a': | 50 | case 'a': |
| 54 | break; | 51 | break; |
| 55 | case 'f': | 52 | case 'f': |
| 56 | fflg = 1; | 53 | opt++; |
| 57 | break; | 54 | break; |
| 58 | case 'n': | 55 | case 'n': |
| 59 | minlen = atoi(optarg); | 56 | n = atoi(optarg); |
| 57 | if(!(n/1)) | ||
| 58 | show_usage(); | ||
| 60 | break; | 59 | break; |
| 61 | case 'o': | 60 | case 'o': |
| 62 | oflg = 1; | 61 | opt++; |
| 62 | opt++; | ||
| 63 | break; | 63 | break; |
| 64 | default: | 64 | default: |
| 65 | show_usage(); | 65 | show_usage(); |
| 66 | } | 66 | } |
| 67 | |||
| 67 | argc -= optind; | 68 | argc -= optind; |
| 68 | argv += optind; | 69 | argv += optind; |
| 69 | 70 | ||
| 70 | if (minlen == -1) | 71 | i=0; |
| 71 | minlen = 4; | ||
| 72 | 72 | ||
| 73 | bfr = xmalloc(minlen); | 73 | if(!argc ) |
| 74 | bfr[minlen] = '\0'; | 74 | { |
| 75 | file = "stdin"; | 75 | file = stdin; |
| 76 | do { | 76 | goto pipe; |
| 77 | if (*argv) { | 77 | } |
| 78 | fprintf(stderr, "opening '%s'\n", *argv); | ||
| 79 | file = *argv++; | ||
| 80 | if (!freopen(file, "r", stdin)) { | ||
| 81 | perror_msg("%s", file); | ||
| 82 | exitcode = EXIT_FAILURE; | ||
| 83 | continue; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | foff = 0; | ||
| 87 | 78 | ||
| 88 | for (cnt = 0; (ch = getchar()) != EOF;) { | 79 | for(a=0;a<argc;a++) |
| 89 | foff++; | 80 | { |
| 90 | if (ISSTR(ch)) { | 81 | file=xfopen(argv[a],"r"); |
| 91 | if (!cnt) | 82 | |
| 92 | C = bfr; | 83 | pipe: |
| 93 | *C++ = ch; | 84 | |
| 94 | if (++cnt < minlen) | 85 | count=0; |
| 95 | continue; | 86 | string=xmalloc(n); |
| 96 | if (fflg) | 87 | string[n]='\0'; |
| 97 | printf("%s:", file); | 88 | n--; |
| 98 | if (oflg) | 89 | while(1) |
| 99 | printf("%7lo %s", (long)(foff - minlen), (char *)bfr); | 90 | { |
| 100 | else | 91 | c=fgetc(file); |
| 101 | printf("%s", bfr); | 92 | if(ISSTR(c)) |
| 102 | i=0; | 93 | { |
| 103 | while ((ch = getchar()) != EOF && ISSTR(ch)) | 94 | if(i==0) |
| 95 | t=count; | ||
| 96 | if(i<=n) | ||
| 97 | string[i]=c; | ||
| 98 | if(i==n) | ||
| 104 | { | 99 | { |
| 105 | putchar((char)ch); | 100 | if(opt == 1 || opt == 3 ) |
| 106 | i++; | 101 | printf("%s: ",(!argv[a])?"{stdin}":argv[a]); |
| 102 | if(opt >= 2 ) | ||
| 103 | printf("%7lo ",t); | ||
| 104 | printf("%s",string); | ||
| 107 | } | 105 | } |
| 108 | if(i>0) | 106 | if(i>n) |
| 109 | foff+=(i+1); | 107 | putchar(c); |
| 110 | else | 108 | i++; |
| 111 | foff++; | 109 | } |
| 112 | putchar('\n'); | 110 | else |
| 111 | { | ||
| 112 | if(i>n) | ||
| 113 | puts(""); | ||
| 114 | i=0; | ||
| 113 | } | 115 | } |
| 114 | cnt = 0; | 116 | count++; |
| 117 | if(c==EOF) | ||
| 118 | break; | ||
| 115 | } | 119 | } |
| 116 | } while (*argv); | 120 | if(file!=stdin) |
| 117 | exit(exitcode); | 121 | fclose(file); |
| 122 | } | ||
| 123 | free(string); | ||
| 124 | exit(EXIT_SUCCESS); | ||
| 118 | } | 125 | } |
| 119 | 126 | ||
| 120 | /* | 127 | /* |
| @@ -130,7 +137,7 @@ int strings_main(int argc, char **argv) | |||
| 130 | * notice, this list of conditions and the following disclaimer in the | 137 | * notice, this list of conditions and the following disclaimer in the |
| 131 | * documentation and/or other materials provided with the distribution. | 138 | * documentation and/or other materials provided with the distribution. |
| 132 | * | 139 | * |
| 133 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change | 140 | * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change |
| 134 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> | 141 | * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> |
| 135 | * | 142 | * |
| 136 | * 4. Neither the name of the University nor the names of its contributors | 143 | * 4. Neither the name of the University nor the names of its contributors |
diff --git a/testsuite/strings/strings-works-like-GNU b/testsuite/strings/strings-works-like-GNU new file mode 100644 index 000000000..02e423134 --- /dev/null +++ b/testsuite/strings/strings-works-like-GNU | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | rm -f foo bar | ||
| 2 | strings -af ../../busybox > foo | ||
| 3 | busybox strings -af ../../busybox > bar | ||
| 4 | test ! -f foo -a -f bar | ||
| 5 | if [ $? = 0 ] ; then | ||
| 6 | diff -q foo bar | ||
| 7 | fi; | ||
| 8 | |||
