support optional-argument extension to getopt via double-colon
authorRich Felker <dalias@aerifal.cx>
Thu, 12 Jun 2014 03:38:44 +0000 (23:38 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 12 Jun 2014 03:38:44 +0000 (23:38 -0400)
this extension is not incompatible with the standard behavior of the
function, not expensive, and avoids requiring a replacement getopt
with full GNU extensions for a few important apps including busybox's
sed with the -i option.

src/misc/getopt.c

index f1a1639c56da1ae9140cde36d486a928daf872fd..8a2e4d50d891e63281411b997e57822745e9bb79 100644 (file)
@@ -65,8 +65,11 @@ int getopt(int argc, char * const argv[], const char *optstring)
                        }
                        return '?';
                }
-               optarg = argv[optind++] + optpos;
-               optpos = 0;
+               if (optstring[i+2] == ':') optarg = 0;
+               if (optstring[i+2] != ':' || optpos) {
+                       optarg = argv[optind++] + optpos;
+                       optpos = 0;
+               }
        }
        return c;
 }