NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: lib/59828: getopt(3) GNU extension wrong behavior



The following reply was made to PR lib/59828; it has been noted by GNATS.

From: Jose Luis Duran <jlduran%gmail.com@localhost>
To: gnats-bugs%netbsd.org@localhost
Cc: 
Subject: Re: lib/59828: getopt(3) GNU extension wrong behavior
Date: Wed, 10 Dec 2025 12:07:43 -0300

 There appears to have been some confusion when filing this bug report.
 It was my suggestion to report it here, since FreeBSD's getopt(3)
 comes from NetBSD.
 
 Given the following MWE script (getopttest.c):
 
 #include <stdio.h>
 #include <unistd.h>
 
 int
 main(int argc, char *argv[])
 {
   int ch;
 
   while ((ch = getopt(argc, argv, "cCd::F:lnoOp:s:u:U:wW")) != -1) {
     printf("ch is %c optarg is %s\n", (char)ch, optarg ? optarg : " NULL");
   }
 
   return (0);
 }
 
 Notice the "d" option has double colons, and is the subject at hand.
 By reading the manual page for getopt(3):
 
   If an individual character is followed by two colons,
   then the option argument is optional; optarg is set
   to the rest of the current argv word,
   or NULL if there were no more characters in the current word.
   This is a NetBSD extension.
 
 The following output from the above MWE program is expected (or is it not?):
 
 1. With a space between "-d" and the option argument:
 $ ./getopttest -l -w -d 14 -s /usr/local/admin/tftproot -u admin -U 0
 ch is l optarg is  NULL
 ch is w optarg is  NULL
 ch is d optarg is 14 # <- XXX [1]
 ch is s optarg is /usr/local/admin/tftproot # <- XXX missing
 ch is u optarg is admin # <- XXX missing
 ch is U optarg is 0 # <- XXX missing
 
 [1]: "It does not matter to getopt() if a following argument has
 leading whitespace."
 
 Current output:
 $ ./getopttest -l -w -d 14 -s /usr/local/admin/tftproot -u admin -U 0
 ch is l optarg is  NULL
 ch is w optarg is  NULL
 ch is d optarg is  NULL
 
 Notice the arguments after "d" are missing.
 
 For completeness, all these other tests currently pass on NetBSD:
 
 2. Without a space between "-d" and the option argument:
 $ ./getopttest -l -w -d14 -s /usr/local/admin/tftproot -u admin -U 0
 ch is l optarg is  NULL
 ch is w optarg is  NULL
 ch is d optarg is 14
 ch is s optarg is /usr/local/admin/tftproot
 ch is u optarg is admin
 ch is U optarg is 0
 
 3. The "d" option without any arguments:
 $ ./getopttest -l -w -d -s /usr/local/admin/tftproot -u admin -U 0
 ch is l optarg is  NULL
 ch is w optarg is  NULL
 ch is d optarg is  NULL
 ch is s optarg is /usr/local/admin/tftproot
 ch is u optarg is admin
 ch is U optarg is 0
 
 4. Invalid options after the "d" option:
 ./getopttest -l -w -d -14 -s /usr/local/admin/tftproot -u admin -U 0
 ch is l optarg is  NULL
 ch is w optarg is  NULL
 ch is d optarg is  NULL
 getopttest: unknown option -- 1
 ch is ? optarg is  NULL
 getopttest: unknown option -- 4
 ch is ? optarg is  NULL
 ch is s optarg is /usr/local/admin/tftproot
 ch is u optarg is admin
 ch is U optarg is 0
 
 Regards,
 


Home | Main Index | Thread Index | Old Index