From f4964fd80647b920b77a485e80661257ba70a936 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Sun, 4 Aug 2019 17:33:40 -0400 Subject: rewrite byte-at-a-time --- strings.c | 67 ++++++++++++++++++++++++++------------------------------------- 1 file changed, 28 insertions(+), 39 deletions(-) diff --git a/strings.c b/strings.c index 7fbb661..49dbd62 100644 --- a/strings.c +++ b/strings.c @@ -29,7 +29,7 @@ #include #include -static int strings(const char *path, int number, char format) +static int strings(const char *path, size_t number, char format) { FILE *f = stdin; if (path && strcmp(path, "-")) { @@ -40,43 +40,30 @@ static int strings(const char *path, int number, char format) return 1; } - char buf[BUFSIZ]; - char tbuf[BUFSIZ]; - int nread; - int offset = 0; - int i; - int count = 0; - - while (!feof(f)) { - nread = fread(buf, sizeof(char), BUFSIZ, f); - for (i = 0; i < nread; i++) { - if (buf[i] == '\0' || buf[i] == '\n') { - if (count >= number) { - switch (format) { - case 'd': - printf("%d ", offset - count); - break; - case 'o': - printf("%o ", offset - count); - break; - case 'x': - printf("%x ", offset - count); - break; - default: - break; - } - fwrite(tbuf, sizeof(char), count, - stdout); - putchar('\n'); - } - count = 0; - } else if (isprint(buf[i])) { - tbuf[count] = buf[i]; - count++; - } else { - count = 0; + size_t count = 0; + char string[number]; + + int c; + for (size_t offset = 0; (c = fgetc(f)) != EOF; offset++) { + if (!isprint(c)) { + if (count >= number) { + putchar('\n'); + } + count = 0; + + } else if (count == number) { + if (format) { + char fmt[] = { '%', 'z', format, ' ', '\0' }; + printf(fmt, offset - count); } - offset++; + fwrite(string, 1, number, stdout); + count++; + + } else if (count > number) { + putchar(c); + + } else { + string[count++] = c; } } @@ -90,7 +77,7 @@ static int strings(const char *path, int number, char format) int main(int argc, char *argv[]) { int c; - int number = 4; + size_t number = 4; char format = 0; char *end; @@ -102,8 +89,10 @@ int main(int argc, char *argv[]) case 'n': number = strtol(optarg, &end, 10); - if (end != NULL && strlen(end) > 0) + if (end != NULL && strlen(end) > 0) { + fprintf(stderr, "strings: invalid number %s\n", optarg); return 1; + } break; case 't': -- cgit v1.2.1