提交 acccc93e 编写于 作者: F Felix Fietkau 提交者: Rich Felker

getopt: fix optional argument processing

Processing an option character with optional argument fails if the
option is last on the command line. This happens because the
if (optind >= argc) check runs first before testing for optional
argument.
上级 941644e9
......@@ -55,7 +55,8 @@ int getopt(int argc, char * const argv[], const char *optstring)
return '?';
}
if (optstring[i+1] == ':') {
if (optind >= argc) {
if (optstring[i+2] == ':') optarg = 0;
else if (optind >= argc) {
if (optstring[0] == ':') return ':';
if (opterr) {
write(2, argv[0], strlen(argv[0]));
......@@ -65,7 +66,6 @@ int getopt(int argc, char * const argv[], const char *optstring)
}
return '?';
}
if (optstring[i+2] == ':') optarg = 0;
if (optstring[i+2] != ':' || optpos) {
optarg = argv[optind++] + optpos;
optpos = 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册