Per the Linux man page, "By default, getopt() permutes the contents of argv as it scans, so that eventually all the nonoptions are at the end." This behavior, however, is not implemented in the NuttX getopt() logic.
The Linux man page requires that the getopt_long() and getopt_long_only() functions accept arguments to options in a form like:
--option=argument
This PR adds that missing functionality.
This change effects only getopt_long() and getopt_long_only()
Tested on a simulator NSH configuration with a modified version of the getopt() test in apps/testing/ostest.
If an unrecognized long option is encountered, we must skip over that argv[] entry or getopt_long() will seriously misbehave.
Affects getopt_long() and getopt_long_only()
Problem found and fix verified with an updated version of the OS test.
Found a place in getopt_common() where the option string is used before it is checked if it is NULL. This can happen because the short option string is optional for getopt_long() and getopt_long_only()
If optstring is NULL, that would be an ERROR for getopt(), but not for the getopt_long() versions.
Should effect only the getopt() APIs
Tested on the simulator using apps/testing/ostest.
1. Null pointer dereference:
- for (ndx = 0; longopts[ndx].name[0] != '\0'; ndx++)
+ for (ndx = 0; longopts[ndx].name != NULL; ndx++)
2. Handle single character long options. An option like -x could be either a short option or a long option (under getopt_long_only()). This case was not being handled correctly.
3. Add missing support for optional arguments to short options (indicated with two "::"
This effects all members of the getopt() family of APIs.
Tested on the simulator using extensions to apps/testing/ostest.
Add implementations of getopt_long() and getopt_long_only()
There could be impacts to getop() since that implementation is shared withe new getopts_long() and getops_long_only() implementation.
Tested using a modified version of sim:nsh