]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
getopt: keep pointer to 'name' in control struct
authorKarel Zak <kzak@redhat.com>
Fri, 22 Apr 2016 09:29:46 +0000 (11:29 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 22 Apr 2016 09:29:46 +0000 (11:29 +0200)
It's seems more readable than call free(argv[0]).

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/getopt.c

index dc2a976df71541dfc265bcfef7c99d712f98c5db..9add5edca5a660071a35458b259f0eabadd27307 100644 (file)
@@ -82,11 +82,11 @@ typedef enum { BASH, TCSH } shell_t;
 struct getopt_control {
        shell_t shell;                  /* the shell we generate output for */
        char *optstr;                   /* getopt(3) optstring */
+       char *name;
        struct option *long_options;    /* long options */
        int long_options_length;        /* length of options array */
        int long_options_nr;            /* number of used elements in array */
        unsigned int
-               free_name:1,            /* free up argv[0] after printout */
                compatible:1,           /* compatibility mode for 'difficult' programs */
                quiet_errors:1,         /* print errors */
                quiet_output:1,         /* print output */
@@ -230,8 +230,7 @@ static int generate_output(struct getopt_control *ctl, char *argv[], int argc)
                free((char *)ctl->long_options[longindex].name);
        free(ctl->long_options);
        free(ctl->optstr);
-       if (ctl->free_name)
-               free(argv[0]);
+       free(ctl->name);
        return exit_code;
 }
 
@@ -357,7 +356,6 @@ int main(int argc, char *argv[])
                .shell = BASH,
                .quote = 1
        };
-       char *name = NULL;
        int opt;
 
        /* Stop scanning as soon as a non-option argument is found! */
@@ -424,9 +422,8 @@ int main(int argc, char *argv[])
                        add_long_options(&ctl, optarg);
                        break;
                case 'n':
-                       free(name);
-                       name = xstrdup(optarg);
-                       ctl.free_name = 1;
+                       free(ctl.name);
+                       ctl.name = xstrdup(optarg);
                        break;
                case 'q':
                        ctl.quiet_errors = 1;
@@ -462,10 +459,10 @@ int main(int argc, char *argv[])
                }
        }
 
-       if (name) {
-               argv[optind - 1] = name;
+       if (ctl.name) {
+               argv[optind - 1] = ctl.name;
 #if defined (HAVE_SETPROGNAME) && !defined (__linux__)
-               setprogname(name);
+               setprogname(ctl.name);
 #endif
        } else
                argv[optind - 1] = argv[0];