getopt.c revision 92986
11573Srgrimes/* 21573Srgrimes * Copyright (c) 1987, 1993, 1994 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 3. All advertising materials mentioning features or use of this software 141573Srgrimes * must display the following acknowledgement: 151573Srgrimes * This product includes software developed by the University of 161573Srgrimes * California, Berkeley and its contributors. 171573Srgrimes * 4. Neither the name of the University nor the names of its contributors 181573Srgrimes * may be used to endorse or promote products derived from this software 191573Srgrimes * without specific prior written permission. 201573Srgrimes * 211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3150476Speter * SUCH DAMAGE. 321573Srgrimes */ 33127227Stjr 341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 351573Srgrimesstatic char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; 361573Srgrimes#endif /* LIBC_SCCS and not lint */ 371573Srgrimes#include <sys/cdefs.h> 381573Srgrimes__FBSDID("$FreeBSD: head/lib/libc/stdlib/getopt.c 92986 2002-03-22 21:53:29Z obrien $"); 391573Srgrimes 401573Srgrimes#include <stdio.h> 4159460Sphantom#include <stdlib.h> 4259460Sphantom#include <string.h> 431573Srgrimes 4484306Sruint opterr = 1, /* if error message should be printed */ 451573Srgrimes optind = 1, /* index into parent argv vector */ 4624879Sbde optopt, /* character checked for validity */ 471573Srgrimes optreset; /* reset getopt */ 48131539Sruchar *optarg; /* argument associated with option */ 491573Srgrimes 50131539Sru#define BADCH (int)'?' 511573Srgrimes#define BADARG (int)':' 521573Srgrimes#define EMSG "" 531573Srgrimes 541573Srgrimes/* 551573Srgrimes * getopt -- 561573Srgrimes * Parse argc/argv argument vector. 5779754Sdd */ 581573Srgrimesint 591573Srgrimesgetopt(nargc, nargv, ostr) 601573Srgrimes int nargc; 611573Srgrimes char * const *nargv; 621573Srgrimes const char *ostr; 631573Srgrimes{ 641573Srgrimes extern char *__progname; 651573Srgrimes static char *place = EMSG; /* option letter processing */ 661573Srgrimes char *oli; /* option letter list index */ 671573Srgrimes 681573Srgrimes if (optreset || !*place) { /* update scanning pointer */ 691573Srgrimes optreset = 0; 701573Srgrimes if (optind >= nargc || *(place = nargv[optind]) != '-') { 711573Srgrimes place = EMSG; 7279754Sdd return (-1); 731573Srgrimes } 741573Srgrimes if (place[1] && *++place == '-') { /* found "--" */ 751573Srgrimes ++optind; 761573Srgrimes place = EMSG; 771573Srgrimes return (-1); 781573Srgrimes } 791573Srgrimes } /* option letter okay? */ 801573Srgrimes if ((optopt = (int)*place++) == (int)':' || 811573Srgrimes !(oli = strchr(ostr, optopt))) { 821573Srgrimes /* 831573Srgrimes * if the user didn't specify '-' as an option, 8482810Sache * assume it means -1. 851573Srgrimes */ 861573Srgrimes if (optopt == (int)'-') 871573Srgrimes return (-1); 881573Srgrimes if (!*place) 8979754Sdd ++optind; 901573Srgrimes if (opterr && *ostr != ':' && optopt != BADCH) 911573Srgrimes (void)fprintf(stderr, 921573Srgrimes "%s: illegal option -- %c\n", __progname, optopt); 931573Srgrimes return (BADCH); 941573Srgrimes } 951573Srgrimes if (*++oli != ':') { /* don't need argument */ 961573Srgrimes optarg = NULL; 971573Srgrimes if (!*place) 981573Srgrimes ++optind; 991573Srgrimes } 1001573Srgrimes else { /* need an argument */ 1011573Srgrimes if (*place) /* no white space */ 1021573Srgrimes optarg = place; 1031573Srgrimes else if (nargc <= ++optind) { /* no arg */ 1041573Srgrimes place = EMSG; 1051573Srgrimes if (*ostr == ':') 1061573Srgrimes return (BADARG); 1071573Srgrimes if (opterr) 1081573Srgrimes (void)fprintf(stderr, 1091573Srgrimes "%s: option requires an argument -- %c\n", 1101573Srgrimes __progname, optopt); 11179754Sdd return (BADCH); 1121573Srgrimes } 1131573Srgrimes else /* white space */ 1141573Srgrimes optarg = nargv[optind]; 1151573Srgrimes place = EMSG; 116119893Sru ++optind; 1171573Srgrimes } 1181573Srgrimes return (optopt); /* dump back option letter */ 1191573Srgrimes} 1201573Srgrimes