strings: strings a b was processing a twice, fix that

This commit is contained in:
Denis Vlasenko 2007-06-17 12:19:07 +00:00
parent ea9e798004
commit 787d92645b

View File

@ -19,9 +19,10 @@
int strings_main(int argc, char **argv); int strings_main(int argc, char **argv);
int strings_main(int argc, char **argv) int strings_main(int argc, char **argv)
{ {
int n, c, i = 0, status = EXIT_SUCCESS; int n, c, status = EXIT_SUCCESS;
unsigned opt; unsigned opt;
unsigned long count; unsigned count;
off_t offset;
FILE *file = stdin; FILE *file = stdin;
char *string; char *string;
const char *fmt = "%s: "; const char *fmt = "%s: ";
@ -29,56 +30,56 @@ int strings_main(int argc, char **argv)
opt = getopt32(argc, argv, "afon:", &n_arg); opt = getopt32(argc, argv, "afon:", &n_arg);
/* -a is our default behaviour */ /* -a is our default behaviour */
/*argc -= optind;*/
argc -= optind;
argv += optind; argv += optind;
n = xatoul_range(n_arg, 1, INT_MAX); n = xatou_range(n_arg, 1, INT_MAX);
string = xzalloc(n + 1); string = xzalloc(n + 1);
n--; n--;
if (argc == 0) { if (!*argv) {
fmt = "{%s}: "; fmt = "{%s}: ";
*argv = (char *)bb_msg_standard_input; *--argv = (char *)bb_msg_standard_input;
goto PIPE; goto PIPE;
} }
do { do {
file = fopen_or_warn(*argv, "r"); file = fopen_or_warn(*argv, "r");
if (file) { if (!file) {
PIPE: status = EXIT_FAILURE;
count = 0; continue;
do { }
c = fgetc(file); PIPE:
if (isprint(c) || c == '\t') { offset = 0;
if (i <= n) { count = 0;
string[i] = c; do {
} else { c = fgetc(file);
putchar(c); if (isprint(c) || c == '\t') {
} if (count > n) {
if (i == n) { putchar(c);
} else {
string[count] = c;
if (count == n) {
if (opt & PRINT_NAME) { if (opt & PRINT_NAME) {
printf(fmt, *argv); printf(fmt, *argv);
} }
if (opt & PRINT_OFFSET) { if (opt & PRINT_OFFSET) {
printf("%7lo ", count - n); printf("%7"OFF_FMT"o ", offset - n);
} }
printf("%s", string); fputs(string, stdout);
} }
i++; count++;
} else {
if (i > n) {
putchar('\n');
}
i = 0;
} }
count++; } else {
} while (c != EOF); if (count > n) {
fclose_if_not_stdin(file); putchar('\n');
} else { }
status = EXIT_FAILURE; count = 0;
} }
} while (--argc > 0); offset++;
} while (c != EOF);
fclose_if_not_stdin(file);
} while (*++argv);
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
free(string); free(string);