diff options
author | Rob Landley | 2005-09-05 06:16:53 +0000 |
---|---|---|
committer | Rob Landley | 2005-09-05 06:16:53 +0000 |
commit | dbaf97e463a63062e0a1a7f98ee9ff564639fb58 (patch) | |
tree | 3300c1222facdb42a65078914c9bc249659401fe /util-linux/getopt.c | |
parent | cc1669bcde1fc773accaafcee4279a32bfaffd37 (diff) | |
download | busybox-dbaf97e463a63062e0a1a7f98ee9ff564639fb58.zip busybox-dbaf97e463a63062e0a1a7f98ee9ff564639fb58.tar.gz |
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.)
Diffstat (limited to 'util-linux/getopt.c')
-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 032d0dc..0ad69ad 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c @@ -306,7 +306,7 @@ static const char *shortopts="+ao:l:n:qQs:Tu"; int getopt_main(int argc, char *argv[]) { const char *optstr = NULL; - const char *name = NULL; + char *name = NULL; int opt; int compatible=0; @@ -326,11 +326,13 @@ int getopt_main(int argc, char *argv[]) } if (argv[1][0] != '-' || compatible) { + char *s; + quote=0; - optstr=xmalloc(strlen(argv[1])+1); - strcpy(optstr,argv[1]+strspn(argv[1],"-+")); + s=xmalloc(strlen(argv[1])+1); + strcpy(s,argv[1]+strspn(argv[1],"-+")); 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) @@ -339,14 +341,12 @@ int getopt_main(int argc, char *argv[]) alternative=1; break; case 'o': - free(optstr); optstr = optarg; break; case 'l': add_long_options(optarg); break; case 'n': - free(name); name = optarg; break; case 'q': @@ -370,10 +370,7 @@ int getopt_main(int argc, char *argv[]) if (!optstr) { if (optind >= argc) bb_error_msg_and_die("missing optstring argument"); - else { - optstr=bb_xstrdup(argv[optind]); - optind++; - } + else optstr=argv[optind++]; } if (name) argv[optind-1]=name; |