annotate toys/pending/strings.c @ 1324:3191aa9490aa draft

strings - print the strings in the file.
author Ashwini Sharma <ak.ashwini1981@gmail.com>
date Thu, 29 May 2014 08:18:50 -0500
parents
children bf2e5591bdf0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1324
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
1 /*strings.c - print the strings of printable characters in files.
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
2 *
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
3 * Copyright 2014 Kyung-su Kim <kaspyx@gmail.com>
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
4 * Copyright 2014 Kyungwan Han <asura321@gmail.com>
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
5 *
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
6 * No Standard
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
7
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
8 USE_STRINGS(NEWTOY(strings, "n#=4<1fo", TOYFLAG_USR|TOYFLAG_BIN))
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
9
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
10 config STRINGS
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
11 bool "strings"
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
12 default n
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
13 help
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
14 usage: strings [-fo] [-n count] [FILE] ...
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
15
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
16 Display printable strings in a binary file
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
17
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
18 -f Precede strings with filenames
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
19 -n [LEN] At least LEN characters form a string (default 4)
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
20 -o Precede strings with decimal offsets
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
21 */
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
22 #define FOR_strings
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
23 #include "toys.h"
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
24
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
25 GLOBALS(
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
26 long num;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
27 )
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
28
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
29 void do_strings(int fd, char *filename)
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
30 {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
31 int nread, i, wlen, count = 0;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
32 off_t offset = 0;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
33 char *string;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
34
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
35 string = xzalloc(TT.num + 1);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
36 wlen = TT.num - 1;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
37
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
38 while ((nread = read(fd,toybuf,sizeof(toybuf)))>0 ) {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
39 for (i = 0; i < nread; i++, offset++) {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
40 if (((toybuf[i] >= 32) && (toybuf[i] <= 126)) || (toybuf[i] == '\t')) {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
41 if (count > wlen) xputc(toybuf[i]);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
42 else {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
43 string[count] = toybuf[i];
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
44 if (count == wlen) {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
45 if (toys.optflags & FLAG_f) xprintf("%s: ", filename);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
46 if (toys.optflags & FLAG_o)
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
47 xprintf("%7lld ",(long long)(offset - wlen));
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
48 xprintf("%s",string);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
49 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
50 count++;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
51 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
52 } else {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
53 if (count > wlen) xputc('\n');
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
54 count = 0;
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
55 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
56 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
57 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
58 xclose(fd);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
59 free(string);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
60 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
61
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
62 void strings_main(void)
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
63 {
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
64 loopfiles(toys.optargs, do_strings);
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
65 }
3191aa9490aa strings - print the strings in the file.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff changeset
66