Mercurial > hg > toybox
changeset 1349:bf2e5591bdf0 draft
Cleanup strings.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 11 Jun 2014 08:02:55 -0500 |
parents | 63d8181b0f92 |
children | bb9c601122b8 |
files | toys/pending/strings.c |
diffstat | 1 files changed, 21 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- 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 <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 @@ 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); } -