1*cdfa2a7eSchristos /* $NetBSD: getopt.c,v 1.5 2020/05/25 20:47:24 christos Exp $ */
2abb0f93cSkardel
3abb0f93cSkardel /*
4abb0f93cSkardel * getopt - get option letter from argv
5abb0f93cSkardel *
6abb0f93cSkardel * This is a version of the public domain getopt() implementation by
7abb0f93cSkardel * Henry Spencer, changed for 4.3BSD compatibility (in addition to System V).
8abb0f93cSkardel * It allows rescanning of an option list by setting optind to 0 before
9abb0f93cSkardel * calling, which is why we use it even if the system has its own (in fact,
10abb0f93cSkardel * this one has a unique name so as not to conflict with the system's).
11abb0f93cSkardel * Thanks to Dennis Ferguson for the appropriate modifications.
12abb0f93cSkardel *
13abb0f93cSkardel * This file is in the Public Domain.
14abb0f93cSkardel */
15abb0f93cSkardel
16abb0f93cSkardel /*LINTLIBRARY*/
17abb0f93cSkardel
188585484eSchristos #include <config.h>
19abb0f93cSkardel #include <stdio.h>
20abb0f93cSkardel
21abb0f93cSkardel #include "ntp_stdlib.h"
22abb0f93cSkardel
23abb0f93cSkardel #ifdef lint
24abb0f93cSkardel #undef putc
25abb0f93cSkardel #define putc fputc
26abb0f93cSkardel #endif /* lint */
27abb0f93cSkardel
28abb0f93cSkardel char *ntp_optarg; /* Global argument pointer. */
29abb0f93cSkardel int ntp_optind = 0; /* Global argv index. */
30abb0f93cSkardel int ntp_opterr = 1; /* for compatibility, should error be printed? */
31abb0f93cSkardel int ntp_optopt; /* for compatibility, option character checked */
32abb0f93cSkardel
33abb0f93cSkardel static char *scan = NULL; /* Private scan pointer. */
34abb0f93cSkardel static const char *prog = "amnesia";
35abb0f93cSkardel
36abb0f93cSkardel /*
37abb0f93cSkardel * Print message about a bad option.
38abb0f93cSkardel */
39abb0f93cSkardel static int
badopt(const char * mess,int ch)40abb0f93cSkardel badopt(
41abb0f93cSkardel const char *mess,
42abb0f93cSkardel int ch
43abb0f93cSkardel )
44abb0f93cSkardel {
45abb0f93cSkardel if (ntp_opterr) {
46abb0f93cSkardel fputs(prog, stderr);
47abb0f93cSkardel fputs(mess, stderr);
48abb0f93cSkardel (void) putc(ch, stderr);
49abb0f93cSkardel (void) putc('\n', stderr);
50abb0f93cSkardel }
51abb0f93cSkardel return ('?');
52abb0f93cSkardel }
53abb0f93cSkardel
54abb0f93cSkardel int
ntp_getopt(int argc,char * argv[],const char * optstring)55abb0f93cSkardel ntp_getopt(
56abb0f93cSkardel int argc,
57abb0f93cSkardel char *argv[],
58abb0f93cSkardel const char *optstring
59abb0f93cSkardel )
60abb0f93cSkardel {
61abb0f93cSkardel register char c;
62abb0f93cSkardel register const char *place;
63abb0f93cSkardel
64abb0f93cSkardel prog = argv[0];
65abb0f93cSkardel ntp_optarg = NULL;
66abb0f93cSkardel
67abb0f93cSkardel if (ntp_optind == 0) {
68abb0f93cSkardel scan = NULL;
69abb0f93cSkardel ntp_optind++;
70abb0f93cSkardel }
71abb0f93cSkardel
72abb0f93cSkardel if (scan == NULL || *scan == '\0') {
73abb0f93cSkardel if (ntp_optind >= argc
74abb0f93cSkardel || argv[ntp_optind][0] != '-'
75abb0f93cSkardel || argv[ntp_optind][1] == '\0') {
76abb0f93cSkardel return (EOF);
77abb0f93cSkardel }
78abb0f93cSkardel if (argv[ntp_optind][1] == '-'
79abb0f93cSkardel && argv[ntp_optind][2] == '\0') {
80abb0f93cSkardel ntp_optind++;
81abb0f93cSkardel return (EOF);
82abb0f93cSkardel }
83abb0f93cSkardel
84abb0f93cSkardel scan = argv[ntp_optind++]+1;
85abb0f93cSkardel }
86abb0f93cSkardel
87abb0f93cSkardel c = *scan++;
88abb0f93cSkardel ntp_optopt = c & 0377;
89abb0f93cSkardel for (place = optstring; place != NULL && *place != '\0'; ++place)
90abb0f93cSkardel if (*place == c)
91abb0f93cSkardel break;
92abb0f93cSkardel
93abb0f93cSkardel if (place == NULL || *place == '\0' || c == ':' || c == '?') {
94abb0f93cSkardel return (badopt(": unknown option -", c));
95abb0f93cSkardel }
96abb0f93cSkardel
97abb0f93cSkardel place++;
98abb0f93cSkardel if (*place == ':') {
99abb0f93cSkardel if (*scan != '\0') {
100abb0f93cSkardel ntp_optarg = scan;
101abb0f93cSkardel scan = NULL;
102abb0f93cSkardel } else if (ntp_optind >= argc) {
103abb0f93cSkardel return (badopt(": option requires argument -", c));
104abb0f93cSkardel } else {
105abb0f93cSkardel ntp_optarg = argv[ntp_optind++];
106abb0f93cSkardel }
107abb0f93cSkardel }
108abb0f93cSkardel
109abb0f93cSkardel return (c & 0377);
110abb0f93cSkardel }
111