From 11c3924f8c29a36a80233326937ad21f504ae658 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 11 Jun 2014 08:02:55 -0500 Subject: Cleanup strings. --- toys/pending/strings.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'toys/pending/strings.c') 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 * * 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); } - -- cgit v1.2.3