aboutsummaryrefslogtreecommitdiff
path: root/miscutils/strings.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2005-06-07 03:21:20 +0000
committerRob Landley <rob@landley.net>2005-06-07 03:21:20 +0000
commit16cd02e01ed66758935250d8d7c0bee90608807c (patch)
tree2256ae01328b90072bb79b6e0794b8efc6862004 /miscutils/strings.c
parented830e8693941b8e54869ef4e68353ca2d71224e (diff)
downloadbusybox-w32-16cd02e01ed66758935250d8d7c0bee90608807c.tar.gz
busybox-w32-16cd02e01ed66758935250d8d7c0bee90608807c.tar.bz2
busybox-w32-16cd02e01ed66758935250d8d7c0bee90608807c.zip
Clean up strings.c to use busybox's option processing. Bug 006, apparently.
Diffstat (limited to 'miscutils/strings.c')
-rw-r--r--miscutils/strings.c125
1 files changed, 54 insertions, 71 deletions
diff --git a/miscutils/strings.c b/miscutils/strings.c
index 92e9f0d11..d0a092421 100644
--- a/miscutils/strings.c
+++ b/miscutils/strings.c
@@ -27,99 +27,82 @@
27 27
28#include <stdio.h> 28#include <stdio.h>
29#include <stdlib.h> 29#include <stdlib.h>
30#include <string.h>
31#include <getopt.h> 30#include <getopt.h>
32#include <unistd.h>
33#include <ctype.h> 31#include <ctype.h>
34#include "busybox.h" 32#include "busybox.h"
35 33
36#define ISSTR(ch) (isprint(ch) || ch == '\t') 34#define ISSTR(ch) (isprint(ch) || ch == '\t')
37 35
36#define WHOLE_FILE 1
37#define PRINT_NAME 2
38#define PRINT_OFFSET 4
39#define SIZE 8
40
38int strings_main(int argc, char **argv) 41int strings_main(int argc, char **argv)
39{ 42{
40 int n=4, c, i, opt=0, status=EXIT_SUCCESS; 43 int n, c, i = 0, status = EXIT_SUCCESS;
41 long t=0, count; 44 unsigned long opt;
45 unsigned long count;
42 FILE *file = stdin; 46 FILE *file = stdin;
43 char *string=NULL; 47 char *string;
44 const char *fmt="%s: "; 48 const char *fmt = "%s: ";
45 49 char *n_arg = "4";
46 while ((i = getopt(argc, argv, "afon:")) > 0) 50
47 switch(i) 51 opt = bb_getopt_ulflags (argc, argv, "afon:", &n_arg);
48 { 52 /* -a is our default behaviour */
49 case 'a': 53
50 break;
51 case 'f':
52 opt+=1;
53 break;
54 case 'o':
55 opt+=2;
56 break;
57 case 'n':
58 n = bb_xgetlarg(optarg, 10, 1, INT_MAX);
59 break;
60 default:
61 bb_show_usage();
62 }
63
64 argc -= optind; 54 argc -= optind;
65 argv += optind; 55 argv += optind;
66 56
67 i=0; 57 n = bb_xgetlarg(n_arg, 10, 1, INT_MAX);
68 58 string = xcalloc(n + 1, 1);
69 string=xmalloc(n+1); 59 n--;
70 string[n]='\0'; 60
71 n-=1; 61 if ( argc == 0) {
72 62 fmt = "{%s}: ";
73 if(argc==0) 63 *argv = (char *)bb_msg_standard_input;
74 { 64 goto PIPE;
75 fmt="{%s}: ";
76 *argv=(char *)bb_msg_standard_input;
77 goto pipe;
78 } 65 }
79 66
80 for( ;*argv!=NULL && argc>0;argv++) 67 do {
81 { 68 if ((file = bb_wfopen(*argv, "r"))) {
82 if((file=bb_wfopen(*argv,"r"))) 69PIPE:
83 { 70 count = 0;
84pipe: 71 do {
85 72 c = fgetc(file);
86 count=0; 73 if (ISSTR(c)) {
87 do{ 74 if (i <= n) {
88 c=fgetc(file);
89 if(ISSTR(c))
90 {
91 if(i==0)
92 t=count;
93 if(i<=n)
94 string[i]=c; 75 string[i]=c;
95 if(i==n) 76 } else {
96 { 77 putchar(c);
97 if(opt == 1 || opt == 3 ) 78 }
98 printf(fmt,*argv); 79 if (i == n) {
99 if(opt >= 2 ) 80 if (opt & PRINT_NAME) {
100 printf("%7lo ", t); 81 printf(fmt, *argv);
82 }
83 if (opt & PRINT_OFFSET) {
84 printf("%7lo ", count - n );
85 }
101 printf("%s", string); 86 printf("%s", string);
102 } 87 }
103 if(i>n)
104 putchar(c);
105 i++; 88 i++;
106 } 89 } else {
107 else 90 if (i > n) {
108 {
109 if(i>n)
110 putchar('\n'); 91 putchar('\n');
111 i=0; 92 }
93 i = 0;
112 } 94 }
113 count++; 95 count++;
114 }while(c!=EOF); 96 } while (c != EOF);
115
116 bb_fclose_nonstdin(file); 97 bb_fclose_nonstdin(file);
117 } 98 } else {
118 else
119 status=EXIT_FAILURE; 99 status=EXIT_FAILURE;
120 } 100 }
121 /*free(string);*/ 101 } while ( --argc > 0 );
122 exit(status); 102#ifdef CONFIG_FEATURE_CLEAN_UP
103 free(string);
104#endif
105 bb_fflush_stdout_and_exit(status);
123} 106}
124 107
125/* 108/*