1 /*strings.c - print the strings of printable characters in files.
3 * Copyright 2014 Kyung-su Kim <kaspyx@gmail.com>
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
10 USE_STRINGS(NEWTOY(strings, "an#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
16 usage: strings [-fo] [-n LEN] [FILE...]
18 Display printable strings in a binary file
20 -f Precede strings with filenames
21 -n At least LEN characters form a string (default 4)
22 -o Precede strings with decimal offsets
32 static void do_strings(int fd, char *filename)
34 int nread, i, wlen = TT.num, count = 0;
36 char *string = xzalloc(wlen + 1);
39 nread = read(fd, toybuf, sizeof(toybuf));
40 if (nread < 0) perror_msg_raw(filename);
42 for (i = 0; i < nread; i++, offset++) {
43 if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) {
44 if (count == wlen) fputc(toybuf[i], stdout);
46 string[count++] = toybuf[i];
48 if (toys.optflags & FLAG_f) printf("%s: ", filename);
49 if (toys.optflags & FLAG_o)
50 printf("%7lld ",(long long)(offset - wlen));
55 if (count == wlen) xputc('\n');
64 void strings_main(void)
66 loopfiles(toys.optargs, do_strings);