Portablelize new getopt.c so it should work where err.h is missing

This commit is contained in:
tbullock@comlore.com 2011-02-17 03:24:54 +00:00
parent 7f66ab61bf
commit 6f6e523bf9

View File

@ -25,7 +25,7 @@
* by Dieter Baron and Thomas Klausner. * by Dieter Baron and Thomas Klausner.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * MODIFICATION, are permitted provided that the following conditions
* are met: * are met:
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
@ -46,9 +46,9 @@
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <err.h>
#include <errno.h> #include <errno.h>
#include "getopt.h" #include "getopt.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -195,7 +195,8 @@ parse_long_options(char * const *nargv, const char *options,
else { else {
/* ambiguous abbreviation */ /* ambiguous abbreviation */
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(ambig, (int)current_argv_len, fprintf(stderr, "ambiguous option -- %.*s\n"
, (int)current_argv_len,
current_argv); current_argv);
optopt = 0; optopt = 0;
return (BADCH); return (BADCH);
@ -205,7 +206,9 @@ parse_long_options(char * const *nargv, const char *options,
if (long_options[match].has_arg == no_argument if (long_options[match].has_arg == no_argument
&& has_equal) { && has_equal) {
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(noarg, (int)current_argv_len, fprintf(stderr, "option doesn't take an "
"argument -- %.*s\n",
(int)current_argv_len,
current_argv); current_argv);
/* /*
* XXX: GNU sets optopt to val regardless of flag * XXX: GNU sets optopt to val regardless of flag
@ -235,7 +238,8 @@ parse_long_options(char * const *nargv, const char *options,
* should be generated. * should be generated.
*/ */
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(recargstring, fprintf(stderr, "option requires an argument "
"-- %s\n",
current_argv); current_argv);
/* /*
* XXX: GNU sets optopt to val regardless of flag * XXX: GNU sets optopt to val regardless of flag
@ -253,7 +257,7 @@ parse_long_options(char * const *nargv, const char *options,
return (-1); return (-1);
} }
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(illoptstring, current_argv); fprintf(stderr, "unknown option -- %s\n", current_argv);
optopt = 0; optopt = 0;
return (BADCH); return (BADCH);
} }
@ -415,7 +419,7 @@ start:
if (!*place) if (!*place)
++optind; ++optind;
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(illoptchar, optchar); fprintf(stderr, "unknown option -- %c\n", optchar);
optopt = optchar; optopt = optchar;
return (BADCH); return (BADCH);
} }
@ -426,7 +430,8 @@ start:
else if (++optind >= nargc) { /* no arg */ else if (++optind >= nargc) { /* no arg */
place = EMSG; place = EMSG;
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(recargchar, optchar); fprintf(stderr, "option requires an argument"
" -- %c\n", optchar);
optopt = optchar; optopt = optchar;
return (BADARG); return (BADARG);
} else /* white space */ } else /* white space */
@ -447,7 +452,8 @@ start:
if (++optind >= nargc) { /* no arg */ if (++optind >= nargc) { /* no arg */
place = EMSG; place = EMSG;
if (PRINT_ERROR) if (PRINT_ERROR)
warnx(recargchar, optchar); fprintf(stderr, "option requires an "
"argument -- %c\n", optchar);
optopt = optchar; optopt = optchar;
return (BADARG); return (BADARG);
} else } else