- /*strings.c - print the strings of printable characters in files.
- *
- * Copyright 2014 Kyung-su Kim <kaspyx@gmail.com>
- * Copyright 2014 Kyungwan Han <asura321@gmail.com>
- *
- * No Standard
- * TODO: utf8 strings
- * TODO: posix -t
- USE_STRINGS(NEWTOY(strings, "an#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
- config STRINGS
- bool "strings"
- default n
- help
- usage: strings [-fo] [-n LEN] [FILE...]
- Display printable strings in a binary file
- -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"
- GLOBALS(
- long num;
- )
- void do_strings(int fd, char *filename)
- {
- int nread, i, wlen = TT.num, count = 0;
- off_t offset = 0;
- char *string = xzalloc(wlen + 1);
- 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) fputc(toybuf[i], stdout);
- else {
- string[count++] = toybuf[i];
- if (count == wlen) {
- if (toys.optflags & FLAG_f) printf("%s: ", filename);
- if (toys.optflags & FLAG_o)
- printf("%7lld ",(long long)(offset - wlen));
- printf("%s", string);
- }
- }
- } else {
- if (count == wlen) xputc('\n');
- count = 0;
- }
- }
- }
- xclose(fd);
- free(string);
- }
- void strings_main(void)
- {
- loopfiles(toys.optargs, do_strings);
- }
Tweak help so -N is consistently using "LEN" isntead of sometimes "count".
Redo the read() part to catch errors in reading from the file. (I'd like to have a warning-only version of xread() but we'd still have to check for errors on return to break out of the file, so it wouldn't save us much. Have to think about it...)
This doesn't handle UTF8 strings. At some point in the future I may want to redo it so it does. (I guess 4 consecutive valid utf8 characters count as a hit, and then continue as long as the utf8 sequences are valid? Not sure how that would affect the false positive rate...)
Posix wants -t and hex output, this doesn't do that. Might also revisit for that. Accept (and ignore) -a.
Redo output so only the newline is using xputc(), and the other stuff is using printf() and fputc(). The rationale is to let the stdio buffer do its thing, rather than making a syscall for every byte. Hopefully if we have a short write stdio will notice and either retry or set the error flag, and then xputc() catches the error flag and the error case of nontransient output failure (disk full, pipe to a dead process, etc). For the common case stdio should buffer the output and xputc flush it anyway.
trivial: I made wlen be equal to TT.len instead of TT.len-1, had the assignment postincrement, and checked for equality rather than greater than. (Having the constraint be TT.len-1 and the allocation be TT.len+1 looked like a fencepost error. It wasn't, but I didn't want other readers to have to work out why.)
Couple whitespace tweaks. Other than that, it was in pretty good shape.
Rob _______________________________________________ Toybox mailing list http://lists.landley.net/listinfo.cgi/toybox-landley.net |
'IT > Toybox' 카테고리의 다른 글
Toybox 오픈소스 프로젝트 참여하기 -2 (0) | 2015.11.27 |
---|---|
Toybox 오픈소스 프로젝트 참여하기 -1 (0) | 2015.10.22 |
Toybox 오픈소스 프로젝트 소개 (2) | 2015.09.23 |