1273806Snp/* 2273806Snp * getopt - get option letter from argv 3273806Snp * 4273806Snp * This is a version of the public domain getopt() implementation by 5273806Snp * Henry Spencer, changed for 4.3BSD compatibility (in addition to System V). 6273806Snp * It allows rescanning of an option list by setting optind to 0 before 7273806Snp * calling, which is why we use it even if the system has its own (in fact, 8273806Snp * this one has a unique name so as not to conflict with the system's). 9273806Snp * Thanks to Dennis Ferguson for the appropriate modifications. 10273806Snp * 11273806Snp * This file is in the Public Domain. 12273806Snp */ 13273806Snp 14273806Snp/*LINTLIBRARY*/ 15273806Snp 16273806Snp#include <config.h> 17273806Snp#include <stdio.h> 18273806Snp 19273806Snp#include "ntp_stdlib.h" 20273806Snp 21273806Snp#ifdef lint 22273806Snp#undef putc 23273806Snp#define putc fputc 24273806Snp#endif /* lint */ 25273806Snp 26273806Snpchar *ntp_optarg; /* Global argument pointer. */ 27273806Snpint ntp_optind = 0; /* Global argv index. */ 28273806Snpint ntp_opterr = 1; /* for compatibility, should error be printed? */ 29273806Snpint ntp_optopt; /* for compatibility, option character checked */ 30273806Snp 31273806Snpstatic char *scan = NULL; /* Private scan pointer. */ 32273806Snpstatic const char *prog = "amnesia"; 33273806Snp 34273806Snp/* 35273806Snp * Print message about a bad option. 36273806Snp */ 37273806Snpstatic int 38273806Snpbadopt( 39273806Snp const char *mess, 40273806Snp int ch 41273806Snp ) 42273806Snp{ 43273806Snp if (ntp_opterr) { 44273806Snp fputs(prog, stderr); 45273806Snp fputs(mess, stderr); 46273806Snp (void) putc(ch, stderr); 47273806Snp (void) putc('\n', stderr); 48273806Snp } 49273806Snp return ('?'); 50273806Snp} 51273806Snp 52273806Snpint 53273806Snpntp_getopt( 54273806Snp int argc, 55273806Snp char *argv[], 56273806Snp const char *optstring 57273806Snp ) 58273806Snp{ 59273806Snp register char c; 60273806Snp register const char *place; 61273806Snp 62273806Snp prog = argv[0]; 63273806Snp ntp_optarg = NULL; 64273806Snp 65273806Snp if (ntp_optind == 0) { 66273806Snp scan = NULL; 67273806Snp ntp_optind++; 68273806Snp } 69273806Snp 70273806Snp if (scan == NULL || *scan == '\0') { 71273806Snp if (ntp_optind >= argc 72273806Snp || argv[ntp_optind][0] != '-' 73273806Snp || argv[ntp_optind][1] == '\0') { 74273806Snp return (EOF); 75273806Snp } 76273806Snp if (argv[ntp_optind][1] == '-' 77273806Snp && argv[ntp_optind][2] == '\0') { 78273806Snp ntp_optind++; 79273806Snp return (EOF); 80273806Snp } 81273806Snp 82273806Snp scan = argv[ntp_optind++]+1; 83273806Snp } 84273806Snp 85273806Snp c = *scan++; 86273806Snp ntp_optopt = c & 0377; 87273806Snp for (place = optstring; place != NULL && *place != '\0'; ++place) 88273806Snp if (*place == c) 89273806Snp break; 90273806Snp 91273806Snp if (place == NULL || *place == '\0' || c == ':' || c == '?') { 92273806Snp return (badopt(": unknown option -", c)); 93273806Snp } 94273806Snp 95273806Snp place++; 96273806Snp if (*place == ':') { 97273806Snp if (*scan != '\0') { 98273806Snp ntp_optarg = scan; 99273806Snp scan = NULL; 100273806Snp } else if (ntp_optind >= argc) { 101273806Snp return (badopt(": option requires argument -", c)); 102273806Snp } else { 103273806Snp ntp_optarg = argv[ntp_optind++]; 104273806Snp } 105318799Snp } 106273806Snp 107273806Snp return (c & 0377); 108273806Snp} 109273806Snp