getopt: Make code more portable.
This commit is contained in:
parent
6d670f6c0f
commit
f9bb5323eb
|
@ -5,6 +5,8 @@ if not env['msvc']:
|
||||||
|
|
||||||
env = env.Clone()
|
env = env.Clone()
|
||||||
|
|
||||||
|
env.Prepend(CPPPATH = ['.'])
|
||||||
|
|
||||||
getopt = env.ConvenienceLibrary(
|
getopt = env.ConvenienceLibrary(
|
||||||
target = 'getopt',
|
target = 'getopt',
|
||||||
source = ['getopt_long.c'],
|
source = ['getopt_long.c'],
|
||||||
|
|
|
@ -33,8 +33,6 @@
|
||||||
#ifndef _GETOPT_H_
|
#ifndef _GETOPT_H_
|
||||||
#define _GETOPT_H_
|
#define _GETOPT_H_
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions
|
* GNU-like getopt_long() and 4.4BSD getsubopt()/optreset extensions
|
||||||
*/
|
*/
|
||||||
|
@ -42,6 +40,10 @@
|
||||||
#define required_argument 1
|
#define required_argument 1
|
||||||
#define optional_argument 2
|
#define optional_argument 2
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
struct option {
|
struct option {
|
||||||
/* name of long option */
|
/* name of long option */
|
||||||
const char *name;
|
const char *name;
|
||||||
|
@ -56,7 +58,6 @@ struct option {
|
||||||
int val;
|
int val;
|
||||||
};
|
};
|
||||||
|
|
||||||
__BEGIN_DECLS
|
|
||||||
int getopt_long(int, char * const *, const char *,
|
int getopt_long(int, char * const *, const char *,
|
||||||
const struct option *, int *);
|
const struct option *, int *);
|
||||||
int getopt_long_only(int, char * const *, const char *,
|
int getopt_long_only(int, char * const *, const char *,
|
||||||
|
@ -73,6 +74,9 @@ extern int optopt;
|
||||||
extern int optreset;
|
extern int optreset;
|
||||||
extern char *suboptarg; /* getsubopt(3) external variable */
|
extern char *suboptarg; /* getsubopt(3) external variable */
|
||||||
#endif
|
#endif
|
||||||
__END_DECLS
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* !_GETOPT_H_ */
|
#endif /* !_GETOPT_H_ */
|
||||||
|
|
|
@ -49,9 +49,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>
|
||||||
|
|
||||||
|
@ -198,7 +198,7 @@ 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, ambig, (int)current_argv_len,
|
||||||
current_argv);
|
current_argv);
|
||||||
optopt = 0;
|
optopt = 0;
|
||||||
return (BADCH);
|
return (BADCH);
|
||||||
|
@ -208,7 +208,7 @@ 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, noarg, (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
|
||||||
|
@ -238,7 +238,7 @@ 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, recargstring,
|
||||||
current_argv);
|
current_argv);
|
||||||
/*
|
/*
|
||||||
* XXX: GNU sets optopt to val regardless of flag
|
* XXX: GNU sets optopt to val regardless of flag
|
||||||
|
@ -256,7 +256,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, illoptstring, current_argv);
|
||||||
optopt = 0;
|
optopt = 0;
|
||||||
return (BADCH);
|
return (BADCH);
|
||||||
}
|
}
|
||||||
|
@ -418,7 +418,7 @@ start:
|
||||||
if (!*place)
|
if (!*place)
|
||||||
++optind;
|
++optind;
|
||||||
if (PRINT_ERROR)
|
if (PRINT_ERROR)
|
||||||
warnx(illoptchar, optchar);
|
fprintf(stderr, illoptchar, optchar);
|
||||||
optopt = optchar;
|
optopt = optchar;
|
||||||
return (BADCH);
|
return (BADCH);
|
||||||
}
|
}
|
||||||
|
@ -429,7 +429,7 @@ 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, recargchar, optchar);
|
||||||
optopt = optchar;
|
optopt = optchar;
|
||||||
return (BADARG);
|
return (BADARG);
|
||||||
} else /* white space */
|
} else /* white space */
|
||||||
|
@ -450,7 +450,7 @@ 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, recargchar, optchar);
|
||||||
optopt = optchar;
|
optopt = optchar;
|
||||||
return (BADARG);
|
return (BADARG);
|
||||||
} else
|
} else
|
||||||
|
|
Loading…
Reference in New Issue