getopt.c revision 81746
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 311573Srgrimes * SUCH DAMAGE. 321573Srgrimes */ 331573Srgrimes 341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3554713Sjkh#if 0 3623658Speterstatic char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; 3754713Sjkh#endif 3854713Sjkhstatic const char rcsid[] = "$FreeBSD: head/lib/libc/stdlib/getopt.c 81746 2001-08-16 03:27:03Z jkoshy $"; 391573Srgrimes#endif /* LIBC_SCCS and not lint */ 401573Srgrimes 411573Srgrimes#include <stdio.h> 421573Srgrimes#include <stdlib.h> 431573Srgrimes#include <string.h> 441573Srgrimes 451573Srgrimesint opterr = 1, /* if error message should be printed */ 461573Srgrimes optind = 1, /* index into parent argv vector */ 471573Srgrimes optopt, /* character checked for validity */ 481573Srgrimes optreset; /* reset getopt */ 491573Srgrimeschar *optarg; /* argument associated with option */ 501573Srgrimes 511573Srgrimes#define BADCH (int)'?' 521573Srgrimes#define BADARG (int)':' 531573Srgrimes#define EMSG "" 541573Srgrimes 551573Srgrimes/* 561573Srgrimes * getopt -- 571573Srgrimes * Parse argc/argv argument vector. 581573Srgrimes */ 591573Srgrimesint 601573Srgrimesgetopt(nargc, nargv, ostr) 611573Srgrimes int nargc; 621573Srgrimes char * const *nargv; 631573Srgrimes const char *ostr; 641573Srgrimes{ 651573Srgrimes extern char *__progname; 661573Srgrimes static char *place = EMSG; /* option letter processing */ 671573Srgrimes char *oli; /* option letter list index */ 681573Srgrimes 691573Srgrimes if (optreset || !*place) { /* update scanning pointer */ 701573Srgrimes optreset = 0; 711573Srgrimes if (optind >= nargc || *(place = nargv[optind]) != '-') { 721573Srgrimes place = EMSG; 7323658Speter return (-1); 741573Srgrimes } 751573Srgrimes if (place[1] && *++place == '-') { /* found "--" */ 761573Srgrimes ++optind; 771573Srgrimes place = EMSG; 7823658Speter return (-1); 791573Srgrimes } 801573Srgrimes } /* option letter okay? */ 811573Srgrimes if ((optopt = (int)*place++) == (int)':' || 821573Srgrimes !(oli = strchr(ostr, optopt))) { 831573Srgrimes /* 841573Srgrimes * if the user didn't specify '-' as an option, 8523658Speter * assume it means -1. 861573Srgrimes */ 871573Srgrimes if (optopt == (int)'-') 8823658Speter return (-1); 891573Srgrimes if (!*place) 901573Srgrimes ++optind; 9165421Simp if (opterr && *ostr != ':' && optopt != BADCH) 921573Srgrimes (void)fprintf(stderr, 931573Srgrimes "%s: illegal option -- %c\n", __progname, optopt); 941573Srgrimes return (BADCH); 951573Srgrimes } 961573Srgrimes if (*++oli != ':') { /* don't need argument */ 971573Srgrimes optarg = NULL; 981573Srgrimes if (!*place) 991573Srgrimes ++optind; 1001573Srgrimes } 1011573Srgrimes else { /* need an argument */ 1021573Srgrimes if (*place) /* no white space */ 1031573Srgrimes optarg = place; 1041573Srgrimes else if (nargc <= ++optind) { /* no arg */ 1051573Srgrimes place = EMSG; 1061573Srgrimes if (*ostr == ':') 10781746Sjkoshy return (BADARG); 1081573Srgrimes if (opterr) 1091573Srgrimes (void)fprintf(stderr, 1101573Srgrimes "%s: option requires an argument -- %c\n", 1111573Srgrimes __progname, optopt); 11281746Sjkoshy return (BADCH); 1131573Srgrimes } 1141573Srgrimes else /* white space */ 1151573Srgrimes optarg = nargv[optind]; 1161573Srgrimes place = EMSG; 1171573Srgrimes ++optind; 1181573Srgrimes } 1191573Srgrimes return (optopt); /* dump back option letter */ 1201573Srgrimes} 121