-
Notifications
You must be signed in to change notification settings - Fork 7
Description
One cannot expect optind to approach argc. On GNU/Linux:
The following will hang because getopt will, by default, permute argv such that options are in front and nonoptions are at the end of argv.
$ ../../PSOC_programmer/bin/mergehex -nm ../../PSOC_compiler/../PSOC_programmer/config/nm.hex -c demo_tmp.hex > demo.hex
Setting POSIXLY_CORRECT functions as a workaround and mergehex will run just fine.
$ POSIXLY_CORRECT=1 ../../PSOC_programmer/bin/mergehex -nm ../../PSOC_compiler/../PSOC_programmer/config/nm.hex -c demo_tmp.hex > demo.hex
Relevant paragraph from man 3 getopt
By default, getopt() permutes the contents of argv as it scans, so
that eventually all the nonoptions are at the end. Two other modes
are also implemented. If the first character of optstring is '+' or
the environment variable POSIXLY_CORRECT is set, then option process‐
ing stops as soon as a nonoption argument is encountered. If the
first character of optstring is '-', then each nonoption argv-element
is handled as if it were the argument of an option with character code
- (This is used by programs that were written to expect options and
other argv-elements in any order and that care about the ordering of
the two.) The special argument "--" forces an end of option-scanning
regardless of the scanning mode.