diff options
author | Rob Landley <rob@landley.net> | 2014-06-11 08:02:55 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2014-06-11 08:02:55 -0500 |
commit | 11c3924f8c29a36a80233326937ad21f504ae658 (patch) | |
tree | b34ff2be049a1b7ad7940672513af9a7fbba5807 | |
parent | 8431692738dc14412c236ab4ebc6464463b6327a (diff) | |
download | toybox-11c3924f8c29a36a80233326937ad21f504ae658.tar.gz |
Cleanup strings.
-rw-r--r-- | toys/pending/strings.c | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/toys/pending/strings.c b/toys/pending/strings.c index 1b53d8df..a1104e5e 100644 --- a/toys/pending/strings.c +++ b/toys/pending/strings.c @@ -4,21 +4,24 @@ * Copyright 2014 Kyungwan Han <asura321@gmail.com> * * 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 @@ GLOBALS( 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; - - string = xzalloc(TT.num + 1); - wlen = TT.num - 1; + char *string = xzalloc(wlen + 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 do_strings(int fd, char *filename) void strings_main(void) { - loopfiles(toys.optargs, do_strings); + loopfiles(toys.optargs, do_strings); } - |