# HG changeset patch # User Rob Landley # Date 1402491775 18000 # Node ID bf2e5591bdf0210f26b85adb2ecaac482c3aeaba # Parent 63d8181b0f924b926d7d093e222820a1aede1da8 Cleanup strings. diff -r 63d8181b0f92 -r bf2e5591bdf0 toys/pending/strings.c --- a/toys/pending/strings.c Tue Jun 10 21:57:05 2014 -0500 +++ b/toys/pending/strings.c Wed Jun 11 08:02:55 2014 -0500 @@ -4,21 +4,24 @@ * Copyright 2014 Kyungwan Han * * No Standard + * TODO: utf8 strings + * TODO: posix -t -USE_STRINGS(NEWTOY(strings, "n#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN)) +USE_STRINGS(NEWTOY(strings, "an#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN)) config STRINGS bool "strings" default n help - usage: strings [-fo] [-n count] [FILE] ... + usage: strings [-fo] [-n LEN] [FILE...] Display printable strings in a binary file - -f Precede strings with filenames - -n [LEN] At least LEN characters form a string (default 4) - -o Precede strings with decimal offsets + -f Precede strings with filenames + -n At least LEN characters form a string (default 4) + -o Precede strings with decimal offsets */ + #define FOR_strings #include "toys.h" @@ -28,29 +31,28 @@ void do_strings(int fd, char *filename) { - int nread, i, wlen, count = 0; + int nread, i, wlen = TT.num, count = 0; off_t offset = 0; - char *string; + char *string = xzalloc(wlen + 1); - string = xzalloc(TT.num + 1); - wlen = TT.num - 1; - - while ((nread = read(fd,toybuf,sizeof(toybuf)))>0 ) { + for (;;) { + nread = read(fd, toybuf, sizeof(toybuf)); + if (nread < 0) perror_msg("%s", filename); + if (nread < 1) break; for (i = 0; i < nread; i++, offset++) { if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) { - if (count > wlen) xputc(toybuf[i]); + if (count == wlen) fputc(toybuf[i], stdout); else { - string[count] = toybuf[i]; + string[count++] = toybuf[i]; if (count == wlen) { - if (toys.optflags & FLAG_f) xprintf("%s: ", filename); + if (toys.optflags & FLAG_f) printf("%s: ", filename); if (toys.optflags & FLAG_o) - xprintf("%7lld ",(long long)(offset - wlen)); - xprintf("%s",string); + printf("%7lld ",(long long)(offset - wlen)); + printf("%s", string); } - count++; } } else { - if (count > wlen) xputc('\n'); + if (count == wlen) xputc('\n'); count = 0; } } @@ -61,6 +63,5 @@ void strings_main(void) { - loopfiles(toys.optargs, do_strings); + loopfiles(toys.optargs, do_strings); } -