aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-03-13 18:49:45 +0000
committerEric Andersen <andersen@codepoet.org>2003-03-13 18:49:45 +0000
commit92b7e7baa11a4dd7edd02b358857745a75bcfaad (patch)
tree79a7a8cd373fb95b1763cbbce2c3450d8dd098fc
parent3f1cf455a02bae721263b2a4c82bc4f26be820af (diff)
downloadbusybox-w32-92b7e7baa11a4dd7edd02b358857745a75bcfaad.tar.gz
busybox-w32-92b7e7baa11a4dd7edd02b358857745a75bcfaad.tar.bz2
busybox-w32-92b7e7baa11a4dd7edd02b358857745a75bcfaad.zip
Patch from Tito Ragusa:
The changes I've made: in strings.c: 1) fix memory allocation error I've detected with MALLOC_CHECK_=1 in *string; 2) code clean up; 3) fix compiler warnings about uninitialized vars;
-rw-r--r--miscutils/strings.c104
1 files changed, 53 insertions, 51 deletions
diff --git a/miscutils/strings.c b/miscutils/strings.c
index 9d8012aa2..ace4bed15 100644
--- a/miscutils/strings.c
+++ b/miscutils/strings.c
@@ -37,29 +37,24 @@
37 37
38int strings_main(int argc, char **argv) 38int strings_main(int argc, char **argv)
39{ 39{
40 extern char *optarg; 40 int n=4, c, i, opt=0, a=0, status=EXIT_SUCCESS;
41 extern int optind; 41 long t=0, count;
42 int n=4, c, i, opt=0, a=0;
43 long t, count;
44 FILE *file; 42 FILE *file;
45 char *string; 43 char *string=NULL;
46 44
47 while ((i = getopt(argc, argv, "an:of")) > 0) 45 while ((i = getopt(argc, argv, "afon:")) > 0)
48 switch(i) 46 switch(i)
49 { 47 {
50 case 'a': 48 case 'a':
51 break; 49 break;
52 case 'f': 50 case 'f':
53 opt++; 51 opt+=1;
54 break;
55 case 'n':
56 n = atoi(optarg);
57 if(!(n/1))
58 show_usage();
59 break; 52 break;
60 case 'o': 53 case 'o':
61 opt++; 54 opt+=2;
62 opt++; 55 break;
56 case 'n':
57 n = bb_xgetlarg(optarg, 1, LONG_MAX, 10);
63 break; 58 break;
64 default: 59 default:
65 show_usage(); 60 show_usage();
@@ -70,6 +65,10 @@ int strings_main(int argc, char **argv)
70 65
71 i=0; 66 i=0;
72 67
68 string=xmalloc(n+1);
69 string[n]='\0';
70 n-=1;
71
73 if(!argc ) 72 if(!argc )
74 { 73 {
75 file = stdin; 74 file = stdin;
@@ -78,50 +77,53 @@ int strings_main(int argc, char **argv)
78 77
79 for(a=0;a<argc;a++) 78 for(a=0;a<argc;a++)
80 { 79 {
81 file=xfopen(argv[a],"r"); 80 if((file=fopen(argv[a],"r")))
82
83 pipe:
84
85 count=0;
86 string=xmalloc(n);
87 string[n]='\0';
88 n--;
89 while(1)
90 { 81 {
91 c=fgetc(file); 82pipe:
92 if(ISSTR(c)) 83
84 count=0;
85 do
93 { 86 {
94 if(i==0) 87 c=fgetc(file);
95 t=count; 88 if(ISSTR(c))
96 if(i<=n)
97 string[i]=c;
98 if(i==n)
99 { 89 {
100 if(opt == 1 || opt == 3 ) 90 if(i==0)
101 printf("%s: ",(!argv[a])?"{stdin}":argv[a]); 91 t=count;
102 if(opt >= 2 ) 92 if(i<=n)
103 printf("%7lo ",t); 93 string[i]=c;
104 printf("%s",string); 94 if(i==n)
95 {
96 if(opt == 1 || opt == 3 )
97 printf("%s: ", (!argv[a])? "{stdin}" : argv[a]);
98 if(opt >= 2 )
99 printf("%7lo ", t);
100 printf("%s", string);
101 }
102 if(i>n)
103 putchar(c);
104 i++;
105 } 105 }
106 if(i>n) 106 else
107 putchar(c); 107 {
108 i++; 108 if(i>n)
109 } 109 puts("");
110 else 110 i=0;
111 { 111 }
112 if(i>n) 112 count++;
113 puts("");
114 i=0;
115 } 113 }
116 count++; 114 while(c!=EOF);
117 if(c==EOF) 115
118 break; 116 if(file!=stdin)
117 fclose(file);
118 }
119 else
120 {
121 perror_msg("%s",argv[a]);
122 status=EXIT_FAILURE;
119 } 123 }
120 if(file!=stdin)
121 fclose(file);
122 } 124 }
123 free(string); 125 free(string);
124 exit(EXIT_SUCCESS); 126 exit(status);
125} 127}
126 128
127/* 129/*