Hello Eric,
On 2019-01-12 8:42 a.m., Eric Blake wrote:
On 1/11/19 6:23 PM, Assaf Gordon wrote:
- optind = 0;
+ optind = 1;
Ouch. You're hitting the portability problem of the difference between
BSD and glibc.
Otherwise many things fail like so:
$ ./src/dd
./src/dd: unrecognized operand ‘./src/dd’
Try './src/dd --help' for more information.
That's the symptoms on BSD for optind = 0 (there, you HAVE to use
optreset=optind=1 for a complete reset; or plain optind=1 for a soft
reset where the man page is not clear if it will always work). But on
glibc, optind=1 does a soft reset (works if the optstring does not start
with '-' or '+' and if you did not change POSIXLY_CORRECT), but MUST use
optind = 0 if you want a hard reset.
I only tested on Debian Stretch (with Debian GLIBC 2.24-11+deb9u3),
did not yet test on BSDs.
With "optind=1", I see the following:
===
$ ./src/hostid
ec68f06c
$ ./src/sleep
./src/sleep: missing operand
Try './src/sleep --help' for more information.
$ ./src/uptime
11:14:05 up 23 days 21:23, 4 users, load average: 1.16, 1.05, 0.52
$ ./src/users
gordon gordon gordon gordon
$ ./src/nohup
./src/nohup: missing operand
Try './src/nohup --help' for more information.
$ ./src/dd ## waits for CTRL-C
^C
0+0 records in
0+0 records out
0 bytes copied, 1.10243 s, 0.0 kB/s
$ ./src/yes | head -n1
y
===
With "optind=0" I see the following:
===
$ ./src/hostid
./src/hostid: extra operand ‘./src/hostid’
Try './src/hostid --help' for more information.
$ ./src/sleep
./src/sleep: missing operand
Try './src/sleep --help' for more information.
$ ./src/users
$ ./src/users | od -tx1
0000000 02 e2 03 0a
0000004
$ ./src/users /var/log/wtmp
./src/users: extra operand ‘/var/log/wtmp’
Try './src/users --help' for more information.
$ ./src/nohup
./src/nohup: ignoring input and appending output to 'nohup.out'
^C
$ ./src/dd
./src/dd: unrecognized operand ‘./src/dd’
Try './src/dd --help' for more information.
$ ./src/yes | head -n1
./src/yes
===
Perhaps "parse_gnu_standard_options_only" should use "_getopt_long_r"
and avoid the need to reset anything?
regards,
- assaf