diff options
author | Rob Landley <rob@landley.net> | 2005-09-05 06:16:53 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-09-05 06:16:53 +0000 |
commit | dbaf97e463a63062e0a1a7f98ee9ff564639fb58 (patch) | |
tree | 3300c1222facdb42a65078914c9bc249659401fe | |
parent | cc1669bcde1fc773accaafcee4279a32bfaffd37 (diff) | |
download | busybox-w32-dbaf97e463a63062e0a1a7f98ee9ff564639fb58.tar.gz busybox-w32-dbaf97e463a63062e0a1a7f98ee9ff564639fb58.tar.bz2 busybox-w32-dbaf97e463a63062e0a1a7f98ee9ff564639fb58.zip |
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.)
-rw-r--r-- | util-linux/getopt.c | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/util-linux/getopt.c b/util-linux/getopt.c index 032d0dc6b..0ad69ad9a 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c | |||
@@ -306,7 +306,7 @@ static const char *shortopts="+ao:l:n:qQs:Tu"; | |||
306 | int getopt_main(int argc, char *argv[]) | 306 | int getopt_main(int argc, char *argv[]) |
307 | { | 307 | { |
308 | const char *optstr = NULL; | 308 | const char *optstr = NULL; |
309 | const char *name = NULL; | 309 | char *name = NULL; |
310 | int opt; | 310 | int opt; |
311 | int compatible=0; | 311 | int compatible=0; |
312 | 312 | ||
@@ -326,11 +326,13 @@ int getopt_main(int argc, char *argv[]) | |||
326 | } | 326 | } |
327 | 327 | ||
328 | if (argv[1][0] != '-' || compatible) { | 328 | if (argv[1][0] != '-' || compatible) { |
329 | char *s; | ||
330 | |||
329 | quote=0; | 331 | quote=0; |
330 | optstr=xmalloc(strlen(argv[1])+1); | 332 | s=xmalloc(strlen(argv[1])+1); |
331 | strcpy(optstr,argv[1]+strspn(argv[1],"-+")); | 333 | strcpy(s,argv[1]+strspn(argv[1],"-+")); |
332 | argv[1]=argv[0]; | 334 | argv[1]=argv[0]; |
333 | return (generate_output(argv+1,argc-1,optstr,long_options)); | 335 | return (generate_output(argv+1,argc-1,s,long_options)); |
334 | } | 336 | } |
335 | 337 | ||
336 | while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) | 338 | while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) |
@@ -339,14 +341,12 @@ int getopt_main(int argc, char *argv[]) | |||
339 | alternative=1; | 341 | alternative=1; |
340 | break; | 342 | break; |
341 | case 'o': | 343 | case 'o': |
342 | free(optstr); | ||
343 | optstr = optarg; | 344 | optstr = optarg; |
344 | break; | 345 | break; |
345 | case 'l': | 346 | case 'l': |
346 | add_long_options(optarg); | 347 | add_long_options(optarg); |
347 | break; | 348 | break; |
348 | case 'n': | 349 | case 'n': |
349 | free(name); | ||
350 | name = optarg; | 350 | name = optarg; |
351 | break; | 351 | break; |
352 | case 'q': | 352 | case 'q': |
@@ -370,10 +370,7 @@ int getopt_main(int argc, char *argv[]) | |||
370 | if (!optstr) { | 370 | if (!optstr) { |
371 | if (optind >= argc) | 371 | if (optind >= argc) |
372 | bb_error_msg_and_die("missing optstring argument"); | 372 | bb_error_msg_and_die("missing optstring argument"); |
373 | else { | 373 | else optstr=argv[optind++]; |
374 | optstr=bb_xstrdup(argv[optind]); | ||
375 | optind++; | ||
376 | } | ||
377 | } | 374 | } |
378 | if (name) | 375 | if (name) |
379 | argv[optind-1]=name; | 376 | argv[optind-1]=name; |