summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/libntp/getopt.c
blob: 7b344f0dc6f4cbd14cd4b5e526c5b1825fba9c8d (plain)
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
97
98
99
100
101
102
103
104
105
106
107
/*
 * 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
 * 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.
 *
 * This file is in the Public Domain.
 */

/*LINTLIBRARY*/

#include <stdio.h>

#include "ntp_stdlib.h"

#ifdef	lint
#undef	putc
#define	putc	fputc
#endif	/* lint */

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 */

static char	*scan = NULL;	/* Private scan pointer. */
static const char	*prog = "amnesia";

/*
 * Print message about a bad option.
 */
static int
badopt(
	const char *mess,
	int ch
	)
{
	if (ntp_opterr) {
		fputs(prog, stderr);
		fputs(mess, stderr);
		(void) putc(ch, stderr);
		(void) putc('\n', stderr);
	}
	return ('?');
}

int
ntp_getopt(
	int argc,
	char *argv[],
	const char *optstring
	)
{
	register char c;
	register const char *place;

	prog = argv[0];
	ntp_optarg = NULL;

	if (ntp_optind == 0) {
		scan = NULL;
		ntp_optind++;
	}
	
	if (scan == NULL || *scan == '\0') {
		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);
		}
	
		scan = argv[ntp_optind++]+1;
	}

	c = *scan++;
	ntp_optopt = c & 0377;
	for (place = optstring; place != NULL && *place != '\0'; ++place)
	    if (*place == c)
		break;

	if (place == NULL || *place == '\0' || c == ':' || c == '?') {
		return (badopt(": unknown option -", c));
	}

	place++;
	if (*place == ':') {
		if (*scan != '\0') {
			ntp_optarg = scan;
			scan = NULL;
		} else if (ntp_optind >= argc) {
			return (badopt(": option requires argument -", c));
		} else {
			ntp_optarg = argv[ntp_optind++];
		}
	}

	return (c & 0377);
}
OpenPOWER on IntegriCloud