getopt.c revision 54713
1169092Sdeischen/* 2169092Sdeischen * Copyright (c) 1987, 1993, 1994 3169092Sdeischen * The Regents of the University of California. All rights reserved. 4174618Sdas * 5174618Sdas * Redistribution and use in source and binary forms, with or without 6157196Sdeischen * modification, are permitted provided that the following conditions 7157196Sdeischen * are met: 8157196Sdeischen * 1. Redistributions of source code must retain the above copyright 9157196Sdeischen * notice, this list of conditions and the following disclaimer. 10157196Sdeischen * 2. Redistributions in binary form must reproduce the above copyright 11157196Sdeischen * notice, this list of conditions and the following disclaimer in the 12157196Sdeischen * documentation and/or other materials provided with the distribution. 13157196Sdeischen * 3. All advertising materials mentioning features or use of this software 14157196Sdeischen * must display the following acknowledgement: 15157196Sdeischen * This product includes software developed by the University of 16157196Sdeischen * California, Berkeley and its contributors. 17157196Sdeischen * 4. Neither the name of the University nor the names of its contributors 18157196Sdeischen * may be used to endorse or promote products derived from this software 19157196Sdeischen * without specific prior written permission. 20157196Sdeischen * 21157196Sdeischen * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22157196Sdeischen * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23157196Sdeischen * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24157196Sdeischen * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25157196Sdeischen * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26157196Sdeischen * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27157196Sdeischen * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28157196Sdeischen * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29157196Sdeischen * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30157196Sdeischen * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31157196Sdeischen * SUCH DAMAGE. 32157196Sdeischen */ 33157196Sdeischen 34157196Sdeischen#if defined(LIBC_SCCS) && !defined(lint) 35157196Sdeischen#if 0 36157196Sdeischenstatic char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95"; 37157196Sdeischen#endif 38157196Sdeischenstatic const char rcsid[] = "$FreeBSD: head/lib/libc/stdlib/getopt.c 54713 1999-12-17 01:52:15Z jkh $"; 39157196Sdeischen#endif /* LIBC_SCCS and not lint */ 40157196Sdeischen 41157196Sdeischen#include <stdio.h> 42157196Sdeischen#include <stdlib.h> 43157196Sdeischen#include <string.h> 44157196Sdeischen 45157196Sdeischenint opterr = 1, /* if error message should be printed */ 46157196Sdeischen optind = 1, /* index into parent argv vector */ 47157196Sdeischen optopt, /* character checked for validity */ 48157196Sdeischen optreset; /* reset getopt */ 49157196Sdeischenchar *optarg; /* argument associated with option */ 50157196Sdeischen 51157196Sdeischen#define BADCH (int)'?' 52157196Sdeischen#define BADARG (int)':' 53157196Sdeischen#define EMSG "" 54157196Sdeischen 55157196Sdeischen/* 56157196Sdeischen * getopt -- 57157196Sdeischen * Parse argc/argv argument vector. 58157196Sdeischen */ 59157196Sdeischenint 60157196Sdeischengetopt(nargc, nargv, ostr) 61157196Sdeischen int nargc; 62157196Sdeischen char * const *nargv; 63157196Sdeischen const char *ostr; 64157196Sdeischen{ 65157196Sdeischen extern char *__progname; 66157196Sdeischen static char *place = EMSG; /* option letter processing */ 67157196Sdeischen char *oli; /* option letter list index */ 68157196Sdeischen int ret; 69157196Sdeischen 70157196Sdeischen if (optreset || !*place) { /* update scanning pointer */ 71157196Sdeischen optreset = 0; 72157196Sdeischen if (optind >= nargc || *(place = nargv[optind]) != '-') { 73157196Sdeischen place = EMSG; 74157196Sdeischen return (-1); 75157196Sdeischen } 76157196Sdeischen if (place[1] && *++place == '-') { /* found "--" */ 77157196Sdeischen ++optind; 78157196Sdeischen place = EMSG; 79157196Sdeischen return (-1); 80157196Sdeischen } 81157196Sdeischen } /* option letter okay? */ 82157196Sdeischen if ((optopt = (int)*place++) == (int)':' || 83157196Sdeischen !(oli = strchr(ostr, optopt))) { 84157196Sdeischen /* 85157196Sdeischen * if the user didn't specify '-' as an option, 86157196Sdeischen * assume it means -1. 87157196Sdeischen */ 88157196Sdeischen if (optopt == (int)'-') 89157196Sdeischen return (-1); 90157196Sdeischen if (!*place) 91157196Sdeischen ++optind; 92157196Sdeischen if (opterr && *ostr != ':') 93157196Sdeischen (void)fprintf(stderr, 94157196Sdeischen "%s: illegal option -- %c\n", __progname, optopt); 95157196Sdeischen return (BADCH); 96157196Sdeischen } 97157196Sdeischen if (*++oli != ':') { /* don't need argument */ 98157196Sdeischen optarg = NULL; 99157196Sdeischen if (!*place) 100157196Sdeischen ++optind; 101157196Sdeischen } 102157196Sdeischen else { /* need an argument */ 103157196Sdeischen if (*place) /* no white space */ 104157196Sdeischen optarg = place; 105157196Sdeischen else if (nargc <= ++optind) { /* no arg */ 106157196Sdeischen place = EMSG; 107157196Sdeischen if (*ostr == ':') 108157196Sdeischen ret = BADARG; 109157196Sdeischen else 110157196Sdeischen ret = BADCH; 111157196Sdeischen if (opterr) 112157196Sdeischen (void)fprintf(stderr, 113157196Sdeischen "%s: option requires an argument -- %c\n", 114157196Sdeischen __progname, optopt); 115157196Sdeischen return (ret); 116157196Sdeischen } 117157196Sdeischen else /* white space */ 118157196Sdeischen optarg = nargv[optind]; 119157196Sdeischen place = EMSG; 120157196Sdeischen ++optind; 121157196Sdeischen } 122157196Sdeischen return (optopt); /* dump back option letter */ 123157196Sdeischen} 124157196Sdeischen