Fix the warnings, and fix the following two obvious segfaults:

./busybox getopt -n one -n two woot
	./busybox getopt -o one -o two woot

This entire applet is still an enormous pile of garbage, which I can't clean
up because I really have no idea what it's for.  (Both "man getopt" and trying
it out on the command line a bit fail to enlighten me.  Reading the code, the
fact half of it seems to be special cases for bash vs tcsh does not fill me
with confidence.)
This commit is contained in:
Rob Landley 2005-09-05 06:16:53 +00:00
parent cc1669bcde
commit dbaf97e463

View File

@ -306,7 +306,7 @@ static const char *shortopts="+ao:l:n:qQs:Tu";
int getopt_main(int argc, char *argv[]) int getopt_main(int argc, char *argv[])
{ {
const char *optstr = NULL; const char *optstr = NULL;
const char *name = NULL; char *name = NULL;
int opt; int opt;
int compatible=0; int compatible=0;
@ -326,11 +326,13 @@ int getopt_main(int argc, char *argv[])
} }
if (argv[1][0] != '-' || compatible) { if (argv[1][0] != '-' || compatible) {
char *s;
quote=0; quote=0;
optstr=xmalloc(strlen(argv[1])+1); s=xmalloc(strlen(argv[1])+1);
strcpy(optstr,argv[1]+strspn(argv[1],"-+")); strcpy(s,argv[1]+strspn(argv[1],"-+"));
argv[1]=argv[0]; argv[1]=argv[0];
return (generate_output(argv+1,argc-1,optstr,long_options)); return (generate_output(argv+1,argc-1,s,long_options));
} }
while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF)
@ -339,14 +341,12 @@ int getopt_main(int argc, char *argv[])
alternative=1; alternative=1;
break; break;
case 'o': case 'o':
free(optstr);
optstr = optarg; optstr = optarg;
break; break;
case 'l': case 'l':
add_long_options(optarg); add_long_options(optarg);
break; break;
case 'n': case 'n':
free(name);
name = optarg; name = optarg;
break; break;
case 'q': case 'q':
@ -370,10 +370,7 @@ int getopt_main(int argc, char *argv[])
if (!optstr) { if (!optstr) {
if (optind >= argc) if (optind >= argc)
bb_error_msg_and_die("missing optstring argument"); bb_error_msg_and_die("missing optstring argument");
else { else optstr=argv[optind++];
optstr=bb_xstrdup(argv[optind]);
optind++;
}
} }
if (name) if (name)
argv[optind-1]=name; argv[optind-1]=name;