11305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood/* 21305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * Copyright (c) 1987, 1993, 1994 31305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * The Regents of the University of California. All rights reserved. 41305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 51305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * Redistribution and use in source and binary forms, with or without 61305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * modification, are permitted provided that the following conditions 71305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * are met: 81305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 1. Redistributions of source code must retain the above copyright 91305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * notice, this list of conditions and the following disclaimer. 101305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 2. Redistributions in binary form must reproduce the above copyright 111305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * notice, this list of conditions and the following disclaimer in the 121305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * documentation and/or other materials provided with the distribution. 131305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 3. Neither the name of the University nor the names of its contributors 141305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * may be used to endorse or promote products derived from this software 151305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * without specific prior written permission. 161305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * 171305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * SUCH DAMAGE. 281305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood */ 291305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 301305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */ 311305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 321305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include "includes.h" 331305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET) 341305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 351305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#if defined(LIBC_SCCS) && !defined(lint) 361305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwoodstatic char *rcsid = "$OpenBSD: getopt.c,v 1.5 2003/06/02 20:18:37 millert Exp $"; 371305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#endif /* LIBC_SCCS and not lint */ 381305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 391305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include <stdio.h> 401305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include <stdlib.h> 411305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#include <string.h> 421305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 431305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwoodint BSDopterr = 1, /* if error message should be printed */ 441305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptind = 1, /* index into parent argv vector */ 451305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptopt, /* character checked for validity */ 461305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptreset; /* reset getopt */ 471305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwoodchar *BSDoptarg; /* argument associated with option */ 481305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 491305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#define BADCH (int)'?' 501305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#define BADARG (int)':' 511305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#define EMSG "" 521305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 531305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood/* 541305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * getopt -- 551305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * Parse argc/argv argument vector. 561305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood */ 571305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwoodint 581305e95ba6ff9fa202d0818caf10405df4b0f648Mike LockwoodBSDgetopt(nargc, nargv, ostr) 591305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood int nargc; 601305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood char * const *nargv; 611305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood const char *ostr; 621305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood{ 631305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood extern char *__progname; 641305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood static char *place = EMSG; /* option letter processing */ 651305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood char *oli; /* option letter list index */ 661305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 671305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (ostr == NULL) 681305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (-1); 691305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 701305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (BSDoptreset || !*place) { /* update scanning pointer */ 711305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptreset = 0; 721305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') { 731305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood place = EMSG; 741305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (-1); 751305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 761305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (place[1] && *++place == '-') { /* found "--" */ 771305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ++BSDoptind; 781305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood place = EMSG; 791305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (-1); 801305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 811305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } /* option letter okay? */ 821305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if ((BSDoptopt = (int)*place++) == (int)':' || 831305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood !(oli = strchr(ostr, BSDoptopt))) { 841305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood /* 851305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * if the user didn't specify '-' as an option, 861305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood * assume it means -1. 871305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood */ 881305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (BSDoptopt == (int)'-') 891305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (-1); 901305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (!*place) 911305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ++BSDoptind; 921305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (BSDopterr && *ostr != ':') 931305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood (void)fprintf(stderr, 941305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood "%s: illegal option -- %c\n", __progname, BSDoptopt); 951305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (BADCH); 961305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 971305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (*++oli != ':') { /* don't need argument */ 981305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptarg = NULL; 991305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (!*place) 1001305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ++BSDoptind; 1011305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 1021305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood else { /* need an argument */ 1031305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (*place) /* no white space */ 1041305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptarg = place; 1051305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood else if (nargc <= ++BSDoptind) { /* no arg */ 1061305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood place = EMSG; 1071305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (*ostr == ':') 1081305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (BADARG); 1091305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood if (BSDopterr) 1101305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood (void)fprintf(stderr, 1111305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood "%s: option requires an argument -- %c\n", 1121305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood __progname, BSDoptopt); 1131305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (BADCH); 1141305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 1151305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood else /* white space */ 1161305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood BSDoptarg = nargv[BSDoptind]; 1171305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood place = EMSG; 1181305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood ++BSDoptind; 1191305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood } 1201305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood return (BSDoptopt); /* dump back option letter */ 1211305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood} 1221305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood 1231305e95ba6ff9fa202d0818caf10405df4b0f648Mike Lockwood#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */ 124