# HG changeset patch # User Ashwini Sharma # Date 1401369530 18000 # Node ID 3191aa9490aa78b991dc66033b5f7bcf061d12c3 # Parent c3061b237c4c8178c752220f1ece089653365209 strings - print the strings in the file. diff -r c3061b237c4c -r 3191aa9490aa toys/pending/strings.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/toys/pending/strings.c Thu May 29 08:18:50 2014 -0500 @@ -0,0 +1,66 @@ +/*strings.c - print the strings of printable characters in files. + * + * Copyright 2014 Kyung-su Kim + * Copyright 2014 Kyungwan Han + * + * No Standard + +USE_STRINGS(NEWTOY(strings, "n#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN)) + +config STRINGS + bool "strings" + default n + help + usage: strings [-fo] [-n count] [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 +*/ +#define FOR_strings +#include "toys.h" + +GLOBALS( + long num; +) + +void do_strings(int fd, char *filename) +{ + int nread, i, wlen, count = 0; + off_t offset = 0; + char *string; + + string = xzalloc(TT.num + 1); + wlen = TT.num - 1; + + while ((nread = read(fd,toybuf,sizeof(toybuf)))>0 ) { + for (i = 0; i < nread; i++, offset++) { + if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) { + if (count > wlen) xputc(toybuf[i]); + else { + string[count] = toybuf[i]; + if (count == wlen) { + if (toys.optflags & FLAG_f) xprintf("%s: ", filename); + if (toys.optflags & FLAG_o) + xprintf("%7lld ",(long long)(offset - wlen)); + xprintf("%s",string); + } + count++; + } + } else { + if (count > wlen) xputc('\n'); + count = 0; + } + } + } + xclose(fd); + free(string); +} + +void strings_main(void) +{ + loopfiles(toys.optargs, do_strings); +} +