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;
This commit is contained in:
Eric Andersen 2003-03-13 18:49:45 +00:00
parent 3f1cf455a0
commit 92b7e7baa1

View File

@ -37,29 +37,24 @@
int strings_main(int argc, char **argv) int strings_main(int argc, char **argv)
{ {
extern char *optarg; int n=4, c, i, opt=0, a=0, status=EXIT_SUCCESS;
extern int optind; long t=0, count;
int n=4, c, i, opt=0, a=0;
long t, count;
FILE *file; FILE *file;
char *string; char *string=NULL;
while ((i = getopt(argc, argv, "an:of")) > 0) while ((i = getopt(argc, argv, "afon:")) > 0)
switch(i) switch(i)
{ {
case 'a': case 'a':
break; break;
case 'f': case 'f':
opt++; opt+=1;
break;
case 'n':
n = atoi(optarg);
if(!(n/1))
show_usage();
break; break;
case 'o': case 'o':
opt++; opt+=2;
opt++; break;
case 'n':
n = bb_xgetlarg(optarg, 1, LONG_MAX, 10);
break; break;
default: default:
show_usage(); show_usage();
@ -70,6 +65,10 @@ int strings_main(int argc, char **argv)
i=0; i=0;
string=xmalloc(n+1);
string[n]='\0';
n-=1;
if(!argc ) if(!argc )
{ {
file = stdin; file = stdin;
@ -78,50 +77,53 @@ int strings_main(int argc, char **argv)
for(a=0;a<argc;a++) for(a=0;a<argc;a++)
{ {
file=xfopen(argv[a],"r"); if((file=fopen(argv[a],"r")))
pipe:
count=0;
string=xmalloc(n);
string[n]='\0';
n--;
while(1)
{ {
c=fgetc(file); pipe:
if(ISSTR(c))
count=0;
do
{ {
if(i==0) c=fgetc(file);
t=count; if(ISSTR(c))
if(i<=n)
string[i]=c;
if(i==n)
{ {
if(opt == 1 || opt == 3 ) if(i==0)
printf("%s: ",(!argv[a])?"{stdin}":argv[a]); t=count;
if(opt >= 2 ) if(i<=n)
printf("%7lo ",t); string[i]=c;
printf("%s",string); if(i==n)
{
if(opt == 1 || opt == 3 )
printf("%s: ", (!argv[a])? "{stdin}" : argv[a]);
if(opt >= 2 )
printf("%7lo ", t);
printf("%s", string);
}
if(i>n)
putchar(c);
i++;
} }
if(i>n) else
putchar(c); {
i++; if(i>n)
puts("");
i=0;
}
count++;
} }
else while(c!=EOF);
{
if(i>n) if(file!=stdin)
puts(""); fclose(file);
i=0; }
} else
count++; {
if(c==EOF) perror_msg("%s",argv[a]);
break; status=EXIT_FAILURE;
} }
if(file!=stdin)
fclose(file);
} }
free(string); free(string);
exit(EXIT_SUCCESS); exit(status);
} }
/* /*