X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmisc%2Fgetopt.c;h=7d7b116782300afce97dcfd8c24115b87849f4fc;hb=ebb6afde680fc420f85a124d548831d59650b611;hp=e921a60ed9997ac0634f1a4c819ce55c7b18714d;hpb=b123f2395266a44176e49cee251fb776e97f26e1;p=musl diff --git a/src/misc/getopt.c b/src/misc/getopt.c index e921a60e..7d7b1167 100644 --- a/src/misc/getopt.c +++ b/src/misc/getopt.c @@ -5,6 +5,7 @@ #include #include "libc.h" #include "locale_impl.h" +#include "stdio_impl.h" char *optarg; int optind=1, opterr=1, optopt, __optpos, __optreset=0; @@ -16,12 +17,12 @@ void __getopt_msg(const char *a, const char *b, const char *c, size_t l) { FILE *f = stderr; b = __lctrans_cur(b); - flockfile(f); + FLOCK(f); fputs(a, f)>=0 && fwrite(b, strlen(b), 1, f) && fwrite(c, 1, l, f)==l && putc('\n', f); - funlockfile(f); + FUNLOCK(f); } int getopt(int argc, char * const argv[], const char *optstring) @@ -84,8 +85,12 @@ int getopt(int argc, char * const argv[], const char *optstring) return '?'; } if (optstring[i] == ':') { - if (optstring[i+1] == ':') optarg = 0; - else if (optind >= argc) { + optarg = 0; + if (optstring[i+1] != ':' || optpos) { + optarg = argv[optind++] + optpos; + optpos = 0; + } + if (optind > argc) { optopt = c; if (optstring[0] == ':') return ':'; if (opterr) __getopt_msg(argv[0], @@ -93,10 +98,6 @@ int getopt(int argc, char * const argv[], const char *optstring) optchar, k); return '?'; } - if (optstring[i+1] != ':' || optpos) { - optarg = argv[optind++] + optpos; - optpos = 0; - } } return c; }