Fix the warnings, and fix the following two obvious segfaults:
authorRob Landley <rob@landley.net>
Mon, 5 Sep 2005 06:16:53 +0000 (06:16 -0000)
committerRob Landley <rob@landley.net>
Mon, 5 Sep 2005 06:16:53 +0000 (06:16 -0000)
./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.)

util-linux/getopt.c

index 032d0dc..0ad69ad 100644 (file)
@@ -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;