comparison toys/pending/strings.c @ 1349:bf2e5591bdf0 draft

Cleanup strings.
author Rob Landley <rob@landley.net>
date Wed, 11 Jun 2014 08:02:55 -0500
parents 3191aa9490aa
children
comparison
equal deleted inserted replaced
1348:63d8181b0f92 1349:bf2e5591bdf0
2 * 2 *
3 * Copyright 2014 Kyung-su Kim <kaspyx@gmail.com> 3 * Copyright 2014 Kyung-su Kim <kaspyx@gmail.com>
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com> 4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
5 * 5 *
6 * No Standard 6 * No Standard
7 * TODO: utf8 strings
8 * TODO: posix -t
7 9
8 USE_STRINGS(NEWTOY(strings, "n#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN)) 10 USE_STRINGS(NEWTOY(strings, "an#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
9 11
10 config STRINGS 12 config STRINGS
11 bool "strings" 13 bool "strings"
12 default n 14 default n
13 help 15 help
14 usage: strings [-fo] [-n count] [FILE] ... 16 usage: strings [-fo] [-n LEN] [FILE...]
15 17
16 Display printable strings in a binary file 18 Display printable strings in a binary file
17 19
18 -f Precede strings with filenames 20 -f Precede strings with filenames
19 -n [LEN] At least LEN characters form a string (default 4) 21 -n At least LEN characters form a string (default 4)
20 -o Precede strings with decimal offsets 22 -o Precede strings with decimal offsets
21 */ 23 */
24
22 #define FOR_strings 25 #define FOR_strings
23 #include "toys.h" 26 #include "toys.h"
24 27
25 GLOBALS( 28 GLOBALS(
26 long num; 29 long num;
27 ) 30 )
28 31
29 void do_strings(int fd, char *filename) 32 void do_strings(int fd, char *filename)
30 { 33 {
31 int nread, i, wlen, count = 0; 34 int nread, i, wlen = TT.num, count = 0;
32 off_t offset = 0; 35 off_t offset = 0;
33 char *string; 36 char *string = xzalloc(wlen + 1);
34 37
35 string = xzalloc(TT.num + 1); 38 for (;;) {
36 wlen = TT.num - 1; 39 nread = read(fd, toybuf, sizeof(toybuf));
37 40 if (nread < 0) perror_msg("%s", filename);
38 while ((nread = read(fd,toybuf,sizeof(toybuf)))>0 ) { 41 if (nread < 1) break;
39 for (i = 0; i < nread; i++, offset++) { 42 for (i = 0; i < nread; i++, offset++) {
40 if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) { 43 if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) {
41 if (count > wlen) xputc(toybuf[i]); 44 if (count == wlen) fputc(toybuf[i], stdout);
42 else { 45 else {
43 string[count] = toybuf[i]; 46 string[count++] = toybuf[i];
44 if (count == wlen) { 47 if (count == wlen) {
45 if (toys.optflags & FLAG_f) xprintf("%s: ", filename); 48 if (toys.optflags & FLAG_f) printf("%s: ", filename);
46 if (toys.optflags & FLAG_o) 49 if (toys.optflags & FLAG_o)
47 xprintf("%7lld ",(long long)(offset - wlen)); 50 printf("%7lld ",(long long)(offset - wlen));
48 xprintf("%s",string); 51 printf("%s", string);
49 } 52 }
50 count++;
51 } 53 }
52 } else { 54 } else {
53 if (count > wlen) xputc('\n'); 55 if (count == wlen) xputc('\n');
54 count = 0; 56 count = 0;
55 } 57 }
56 } 58 }
57 } 59 }
58 xclose(fd); 60 xclose(fd);
59 free(string); 61 free(string);
60 } 62 }
61 63
62 void strings_main(void) 64 void strings_main(void)
63 { 65 {
64 loopfiles(toys.optargs, do_strings); 66 loopfiles(toys.optargs, do_strings);
65 } 67 }
66