diff --git a/src/misc/getopt.c b/src/misc/getopt.c index e77e460..e2a309a 100644 --- a/src/misc/getopt.c +++ b/src/misc/getopt.c @@ -4,6 +4,7 @@ #include #include #include "libc.h" +#include "stdio_impl.h" char *optarg; int optind=1, opterr=1, optopt, __optpos, __optreset=0; @@ -11,6 +12,21 @@ int optind=1, opterr=1, optopt, __optpos, __optreset=0; #define optpos __optpos weak_alias(__optreset, optreset); +#if 0 +static void errmsg(const char *a, const char *b, int l, const char *c) +{ + FILE *f = stderr; + FLOCK(f); + size_t al = strlen(a), bl = strlen(b); + __fwritex((void *)a, al, f) == al && + __fwritex((void *)b, bl, f) == bl && + __fwritex((void *)c, l, f) == l && + __fwritex((void *)"\n", 1, f); + FUNLOCK(f); +} +#define fprintf(fi, fo, a, b, l, c) errmsg(a, b, l, c) +#endif + int getopt(int argc, char * const argv[], const char *optstring) { int i; @@ -66,24 +82,18 @@ int getopt(int argc, char * const argv[], const char *optstring) } while (l && d != c); if (d != c) { - if (optstring[0] != ':' && opterr) { - write(2, argv[0], strlen(argv[0])); - write(2, ": illegal option: ", 18); - write(2, optchar, k); - write(2, "\n", 1); - } + if (optstring[0] != ':' && opterr) + fprintf(stderr, "%s%s%.*s\n", argv[0], + ": illegal option: ", k, optchar); return '?'; } if (optstring[i] == ':') { if (optstring[i+1] == ':') optarg = 0; else if (optind >= argc) { if (optstring[0] == ':') return ':'; - if (opterr) { - write(2, argv[0], strlen(argv[0])); - write(2, ": option requires an argument: ", 31); - write(2, optchar, k); - write(2, "\n", 1); - } + if (opterr) + fprintf(stderr, "%s%s%.*s\n", argv[0], + ": option requires an argument: ", k, optchar); return '?'; } if (optstring[i+1] != ':' || optpos) {