From mboxrd@z Thu Jan 1 00:00:00 1970 From: "alain.bonnefoy@rieter.com" To: glibc-bugs@sources.redhat.com Subject: [Bug libc/569] New: getopt_long option parsing Date: Tue, 23 Nov 2004 07:12:00 -0000 Message-id: <20041123071225.569.alain.bonnefoy@rieter.com> X-SW-Source: 2004-11/msg00187.html List-Id: 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.