This is the mail archive of the glibc-bugs@sources.redhat.com mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/569] New: getopt_long option parsing


According to what I saw in the getopt_long source code, the optional argument 
parsing test is: 

                if (o->has_arg>0) { 
                    if (*max=='=') 
                        optarg=max+1; 
                    else { 
                        optarg=argv[optind+1]; 
                        ++optind; 
                        // These two 'if' code blocks fix a bug about optional 
argument missing. 
                        // More, I added two additional test with (optarg[0] 
== '-') as I consider that dash 
                        // specify an option only if it's followed by a 
character different from space or end of string 
                        if ((o->has_arg==1) && (!optarg || ((optarg[0] == '-') 
&& (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* no mandatory argument 
there */ 
                            if (*optstring==':') return ':'; 
                            write(2,"argument required: `",20); 
                            write(2,arg,max-arg); 
                            write(2,"'.\n",3); 
                            return '?'; 
                        } 
                        if ((o->has_arg==2) && (!optarg || ((optarg[0] == '-') 
&& (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* no optional argument 
there */ 
                            optarg = NULL; 
                            if (!o->flag) return o->val; 
                            return 0; 
                        } 
                    } 
                } 

The problem of this code is that it doesn't allow to specify negative values 
as optional parameters such as: 

# my_cmd --setmin -100 --setmax 100 

What I propose is to add an additional test to accept a dash character as 
optional argument if it is followed by a digit: 

                if (o->has_arg>0) { 
                    if (*max=='=') 
                        optarg=max+1; 
                    else { 
                        optarg=argv[optind+1]; 
                        ++optind; 
                        // These two 'if' code blocks fix a bug about optional 
argument missing. 
                        // More, I added two additional test with (optarg[0] 
== '-') as I consider that dash 
                        // specify an option only if it's followed by a 
character different from space or end of string AND it's not a digit like a 
negative value 
                        if ((o->has_arg==1) && (!optarg || ((optarg[0] == '-') 
&& (!isdigit(optarg[1])) && (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* 
no mandatory argument there */ 
                            if (*optstring==':') return ':'; 
                            write(2,"argument required: `",20); 
                            write(2,arg,max-arg); 
                            write(2,"'.\n",3); 
                            return '?'; 
                        } 
                        if ((o->has_arg==2) && (!optarg || ((optarg[0] == '-') 
&& (!isdigit(optarg[1])) && (optarg[1] != ' ') && (optarg[1] != '\0')))) { /* 
no optional argument there */ 
                            optarg = NULL; 
                            if (!o->flag) return o->val; 
                            return 0; 
                        } 
                    } 
                } 


Kind regards, 
A.Bonnefoy

-- 
           Summary: getopt_long option parsing
           Product: glibc
           Version: unspecified
            Status: NEW
          Severity: normal
          Priority: P2
         Component: libc
        AssignedTo: gotom at debian dot or dot jp
        ReportedBy: alain dot bonnefoy at rieter dot com
                CC: glibc-bugs at sources dot redhat dot com
 GCC build triplet: any
  GCC host triplet: any
GCC target triplet: any


http://sources.redhat.com/bugzilla/show_bug.cgi?id=569

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]