]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
getopt: make user getopt_long parsing to use function pointer
authorSami Kerola <kerolasa@iki.fi>
Sun, 12 Jun 2011 22:49:08 +0000 (00:49 +0200)
committerSami Kerola <kerolasa@iki.fi>
Sat, 25 Jun 2011 14:19:13 +0000 (16:19 +0200)
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
getopt/getopt.c

index ceb9d3dedfdcb4c22b1a68b13cc03c783c031e2c..b857aac83ee6bff01e2297ad21aea3b17b4cb1c1 100644 (file)
@@ -76,7 +76,10 @@ static shell_t shell=BASH; /* The shell we generate output for. */
 static int quiet_errors=0; /* 0 is not quiet. */
 static int quiet_output=0; /* 0 is not quiet. */
 static int quote=1; /* 1 is do quote. */
-static int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
+
+/* Allow changing which getopt is in use with function pointer */
+int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr,
+                      const struct option * longopts, int *longindex);
 
 /* Function prototypes */
 static const char *normalize(const char *arg);
@@ -172,9 +175,7 @@ static int generate_output(char * argv[],int argc,const char *optstr,
                opterr=0;
        optind=0; /* Reset getopt(3) */
 
-       while ((opt = (alternative?
-                     getopt_long_only(argc,argv,optstr,longopts,&longindex):
-                     getopt_long(argc,argv,optstr,longopts,&longindex))) 
+       while ((opt = (getopt_long_fp(argc,argv,optstr,longopts,&longindex)))
                != EOF) 
                if (opt == '?' || opt == ':' )
                        exit_code = GETOPT_EXIT_CODE;
@@ -352,6 +353,7 @@ int main(int argc, char *argv[])
        textdomain(PACKAGE);
 
        init_longopt();
+       getopt_long_fp = getopt_long;
 
        if (getenv("GETOPT_COMPATIBLE")) 
                compatible=1;
@@ -379,7 +381,7 @@ int main(int argc, char *argv[])
        while ((opt=getopt_long(argc,argv,shortopts,longopts,NULL)) != EOF) 
                switch (opt) {
                case 'a':
-                       alternative=1;
+                       getopt_long_fp = getopt_long_only;
                        break;
                case 'h':
                        print_help();