freebsd-dev/usr.sbin/xntpd/lib/getopt.c

106 lines
2.2 KiB
C
Raw Normal View History

1994-09-29 23:04:24 +00:00
/*
1993-12-21 18:36:48 +00:00
* 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 0 before
1994-02-03 22:09:07 +00:00
* calling, which is why we use it even if the system has its own (in fact,
* this one has a unique name so as not to conflict with the system's).
* Thanks to Dennis Ferguson for the appropriate modifications.
1993-12-21 18:36:48 +00:00
*
* This file is in the Public Domain.
*/
/*LINTLIBRARY*/
#include <stdio.h>
#include "ntp_stdlib.h"
#ifdef lint
#undef putc
#define putc fputc
#endif /* lint */
1994-02-03 22:09:07 +00:00
char *ntp_optarg; /* Global argument pointer. */
int ntp_optind = 0; /* Global argv index. */
int ntp_opterr = 1; /* for compatibility, should error be printed? */
int ntp_optopt; /* for compatibility, option character checked */
1993-12-21 18:36:48 +00:00
static char *scan = NULL; /* Private scan pointer. */
1994-02-03 22:09:07 +00:00
static char *prog = "amnesia";
1993-12-21 18:36:48 +00:00
/*
1994-02-03 22:09:07 +00:00
* Print message about a bad option.
1993-12-21 18:36:48 +00:00
*/
1994-02-03 22:09:07 +00:00
static int
badopt(mess, ch)
char *mess;
int ch;
{
if (ntp_opterr) {
fputs(prog, stderr);
fputs(mess, stderr);
(void) putc(ch, stderr);
(void) putc('\n', stderr);
}
return ('?');
}
1993-12-21 18:36:48 +00:00
int
1994-02-03 22:09:07 +00:00
ntp_getopt(argc, argv, optstring)
1993-12-21 18:36:48 +00:00
int argc;
char *argv[];
char *optstring;
{
register char c;
register char *place;
1994-02-03 22:09:07 +00:00
prog = argv[0];
ntp_optarg = NULL;
1993-12-21 18:36:48 +00:00
1994-02-03 22:09:07 +00:00
if (ntp_optind == 0) {
1993-12-21 18:36:48 +00:00
scan = NULL;
1994-02-03 22:09:07 +00:00
ntp_optind++;
1993-12-21 18:36:48 +00:00
}
if (scan == NULL || *scan == '\0') {
1994-02-03 22:09:07 +00:00
if (ntp_optind >= argc
|| argv[ntp_optind][0] != '-'
|| argv[ntp_optind][1] == '\0') {
return (EOF);
}
if (argv[ntp_optind][1] == '-'
&& argv[ntp_optind][2] == '\0') {
ntp_optind++;
return (EOF);
1993-12-21 18:36:48 +00:00
}
1994-02-03 22:09:07 +00:00
scan = argv[ntp_optind++]+1;
1993-12-21 18:36:48 +00:00
}
c = *scan++;
1994-02-03 22:09:07 +00:00
ntp_optopt = c & 0377;
1993-12-21 18:36:48 +00:00
for (place = optstring; place != NULL && *place != '\0'; ++place)
if (*place == c)
break;
if (place == NULL || *place == '\0' || c == ':' || c == '?') {
1994-02-03 22:09:07 +00:00
return (badopt(": unknown option -", c));
1993-12-21 18:36:48 +00:00
}
place++;
if (*place == ':') {
if (*scan != '\0') {
1994-02-03 22:09:07 +00:00
ntp_optarg = scan;
1993-12-21 18:36:48 +00:00
scan = NULL;
1994-02-03 22:09:07 +00:00
} else if (ntp_optind >= argc) {
return (badopt(": option requires argument -", c));
1993-12-21 18:36:48 +00:00
} else {
1994-02-03 22:09:07 +00:00
ntp_optarg = argv[ntp_optind++];
1993-12-21 18:36:48 +00:00
}
}
1994-02-03 22:09:07 +00:00
return (c & 0377);
1993-12-21 18:36:48 +00:00
}