1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/*
* getopt - get option letter from argv
*
* This is a version of the public domain getopt() implementation by
* Henry Spencer, changed for 4.3BSD compatibility (in addition to System V).
* It allows rescanning of an option list by setting optind to 1 before
* calling. Thanks to Dennis Ferguson for the appropriate modifications.
*
* This file is in the Public Domain.
*/
/*LINTLIBRARY*/
#include "hostenv.h"
#include <stdio.h>
#ifdef lint
#undef putc
#define putc fputc
#endif /* lint */
char *optarg; /* Global argument pointer. */
int optind = 1; /* Global argv index. */
/*
* N.B. use following at own risk
*/
int opterr = 1; /* for compatibility, should error be printed? */
int optopt; /* for compatibility, option character checked */
static char *scan = NULL; /* Private scan pointer. */
/*
* Print message about a bad option. Watch this definition, it's
* not a single statement.
*/
#define BADOPT(mess, ch) if (opterr) { \
extern int fputs(), fputc(); \
(void) fputs(argv[0], stderr); \
(void) fputs(mess, stderr); \
(void) fputc(ch, stderr); \
(void) fputc('\n', stderr); \
} \
return('?')
int
getopt(argc, argv, optstring)
int argc;
char *const argv[];
const char *optstring;
{
register char c;
register const char *place;
optarg = NULL;
if (optind == 1)
scan = NULL;
if (scan == NULL || *scan == '\0') {
if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
return EOF;
if (argv[optind][1] == '-' && argv[optind][2] == '\0') {
optind++;
return EOF;
}
scan = argv[optind]+1;
optind++;
}
c = *scan++;
optopt = c & 0377;
for (place = optstring; place != NULL && *place != '\0'; ++place)
if (*place == c)
break;
if (place == NULL || *place == '\0' || c == ':' || c == '?') {
BADOPT(": unknown option -", c);
}
place++;
if (*place == ':') {
if (*scan != '\0') {
optarg = scan;
scan = NULL;
} else if (optind >= argc) {
BADOPT(": option requires argument -", c);
} else {
optarg = argv[optind];
optind++;
}
}
return c&0377;
}
|