154359Sroberto/*
254359Sroberto * getopt - get option letter from argv
354359Sroberto *
454359Sroberto * This is a version of the public domain getopt() implementation by
554359Sroberto * Henry Spencer, changed for 4.3BSD compatibility (in addition to System V).
654359Sroberto * It allows rescanning of an option list by setting optind to 0 before
754359Sroberto * calling, which is why we use it even if the system has its own (in fact,
854359Sroberto * this one has a unique name so as not to conflict with the system's).
954359Sroberto * Thanks to Dennis Ferguson for the appropriate modifications.
1054359Sroberto *
1154359Sroberto * This file is in the Public Domain.
1254359Sroberto */
1354359Sroberto
1454359Sroberto/*LINTLIBRARY*/
1554359Sroberto
16285612Sdelphij#include <config.h>
1754359Sroberto#include <stdio.h>
1854359Sroberto
1954359Sroberto#include "ntp_stdlib.h"
2054359Sroberto
2154359Sroberto#ifdef	lint
2254359Sroberto#undef	putc
2354359Sroberto#define	putc	fputc
2454359Sroberto#endif	/* lint */
2554359Sroberto
2654359Srobertochar	*ntp_optarg;	/* Global argument pointer. */
2754359Srobertoint	ntp_optind = 0;	/* Global argv index. */
2854359Srobertoint	ntp_opterr = 1;	/* for compatibility, should error be printed? */
2954359Srobertoint	ntp_optopt;	/* for compatibility, option character checked */
3054359Sroberto
3154359Srobertostatic char	*scan = NULL;	/* Private scan pointer. */
3254359Srobertostatic const char	*prog = "amnesia";
3354359Sroberto
3454359Sroberto/*
3554359Sroberto * Print message about a bad option.
3654359Sroberto */
3754359Srobertostatic int
3854359Srobertobadopt(
3954359Sroberto	const char *mess,
4054359Sroberto	int ch
4154359Sroberto	)
4254359Sroberto{
4354359Sroberto	if (ntp_opterr) {
4454359Sroberto		fputs(prog, stderr);
4554359Sroberto		fputs(mess, stderr);
4654359Sroberto		(void) putc(ch, stderr);
4754359Sroberto		(void) putc('\n', stderr);
4854359Sroberto	}
4954359Sroberto	return ('?');
5054359Sroberto}
5154359Sroberto
5254359Srobertoint
5354359Srobertontp_getopt(
5454359Sroberto	int argc,
5554359Sroberto	char *argv[],
5654359Sroberto	const char *optstring
5754359Sroberto	)
5854359Sroberto{
5954359Sroberto	register char c;
6054359Sroberto	register const char *place;
6154359Sroberto
6254359Sroberto	prog = argv[0];
6354359Sroberto	ntp_optarg = NULL;
6454359Sroberto
6554359Sroberto	if (ntp_optind == 0) {
6654359Sroberto		scan = NULL;
6754359Sroberto		ntp_optind++;
6854359Sroberto	}
6954359Sroberto
7054359Sroberto	if (scan == NULL || *scan == '\0') {
7154359Sroberto		if (ntp_optind >= argc
7254359Sroberto		    || argv[ntp_optind][0] != '-'
7354359Sroberto		    || argv[ntp_optind][1] == '\0') {
7454359Sroberto			return (EOF);
7554359Sroberto		}
7654359Sroberto		if (argv[ntp_optind][1] == '-'
7754359Sroberto		    && argv[ntp_optind][2] == '\0') {
7854359Sroberto			ntp_optind++;
7954359Sroberto			return (EOF);
8054359Sroberto		}
8154359Sroberto
8254359Sroberto		scan = argv[ntp_optind++]+1;
8354359Sroberto	}
8454359Sroberto
8554359Sroberto	c = *scan++;
8654359Sroberto	ntp_optopt = c & 0377;
8754359Sroberto	for (place = optstring; place != NULL && *place != '\0'; ++place)
8854359Sroberto	    if (*place == c)
8954359Sroberto		break;
9054359Sroberto
9154359Sroberto	if (place == NULL || *place == '\0' || c == ':' || c == '?') {
9254359Sroberto		return (badopt(": unknown option -", c));
9354359Sroberto	}
9454359Sroberto
9554359Sroberto	place++;
9654359Sroberto	if (*place == ':') {
9754359Sroberto		if (*scan != '\0') {
9854359Sroberto			ntp_optarg = scan;
9954359Sroberto			scan = NULL;
10054359Sroberto		} else if (ntp_optind >= argc) {
10154359Sroberto			return (badopt(": option requires argument -", c));
10254359Sroberto		} else {
10354359Sroberto			ntp_optarg = argv[ntp_optind++];
10454359Sroberto		}
10554359Sroberto	}
10654359Sroberto
10754359Sroberto	return (c & 0377);
10854359Sroberto}
109