1240116Smarcel/* 2240116Smarcel * Copyright (c) 1987, 1993, 1994 3240116Smarcel * The Regents of the University of California. All rights reserved. 4240116Smarcel * 5240116Smarcel * Redistribution and use in source and binary forms, with or without 6240116Smarcel * modification, are permitted provided that the following conditions 7240116Smarcel * are met: 8240116Smarcel * 1. Redistributions of source code must retain the above copyright 9240116Smarcel * notice, this list of conditions and the following disclaimer. 10240116Smarcel * 2. Redistributions in binary form must reproduce the above copyright 11240116Smarcel * notice, this list of conditions and the following disclaimer in the 12240116Smarcel * documentation and/or other materials provided with the distribution. 13240116Smarcel * 3. All advertising materials mentioning features or use of this software 14240116Smarcel * must display the following acknowledgement: 15240116Smarcel * This product includes software developed by the University of 16240116Smarcel * California, Berkeley and its contributors. 17240116Smarcel * 4. Neither the name of the University nor the names of its contributors 18240116Smarcel * may be used to endorse or promote products derived from this software 19240116Smarcel * without specific prior written permission. 20240116Smarcel * 21240116Smarcel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22240116Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23240116Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24240116Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25240116Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26273929Sjmmv * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27273929Sjmmv * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28240116Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29273929Sjmmv * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30240116Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31240116Smarcel * SUCH DAMAGE. 32240116Smarcel */ 33240116Smarcel 34240116Smarcel#ifdef HAVE_CONFIG_H 35240116Smarcel# include "config.h" 36240116Smarcel#endif 37240116Smarcel#if !defined(HAVE_GETOPT) || defined(WANT_GETOPT_LONG) || defined(BROKEN_GETOPT) 38240116Smarcel 39240116Smarcel#if defined(LIBC_SCCS) && !defined(lint) 40240116Smarcel/* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */ 41240116Smarcelstatic char *rcsid = "$Id: getopt.c,v 1.3 1999/01/08 02:14:18 sjg Exp $"; 42240116Smarcel#endif /* LIBC_SCCS and not lint */ 43240116Smarcel 44240116Smarcel#include <stdio.h> 45240116Smarcel#include <stdlib.h> 46240116Smarcel#include <string.h> 47240116Smarcel 48273929Sjmmv 49240116Smarcel#define BADCH (int)'?' 50240116Smarcel#define BADARG (int)':' 51273929Sjmmv#define EMSG "" 52273929Sjmmv 53273929Sjmmvint opterr = 1, /* if error message should be printed */ 54273929Sjmmv optind = 1, /* index into parent argv vector */ 55273929Sjmmv optopt = BADCH, /* character checked for validity */ 56273929Sjmmv optreset; /* reset getopt */ 57240116Smarcelchar *optarg; /* argument associated with option */ 58240116Smarcel 59240116Smarcel/* 60240116Smarcel * getopt -- 61240116Smarcel * Parse argc/argv argument vector. 62240116Smarcel */ 63240116Smarcelint 64240116Smarcelgetopt(nargc, nargv, ostr) 65240116Smarcel int nargc; 66240116Smarcel char * const *nargv; 67240116Smarcel const char *ostr; 68240116Smarcel{ 69240116Smarcel extern char *__progname; 70240116Smarcel static char *place = EMSG; /* option letter processing */ 71240116Smarcel char *oli; /* option letter list index */ 72240116Smarcel 73240116Smarcel#ifndef BSD4_4 74240116Smarcel if (!__progname) { 75240116Smarcel if (__progname = strrchr(nargv[0], '/')) 76240116Smarcel ++__progname; 77240116Smarcel else 78240116Smarcel __progname = nargv[0]; 79240116Smarcel } 80240116Smarcel#endif 81240116Smarcel 82240116Smarcel if (optreset || !*place) { /* update scanning pointer */ 83240116Smarcel optreset = 0; 84240116Smarcel if (optind >= nargc || *(place = nargv[optind]) != '-') { 85240116Smarcel place = EMSG; 86240116Smarcel return (-1); 87240116Smarcel } 88240116Smarcel if (place[1] && *++place == '-' /* found "--" */ 89240116Smarcel && !place[1]) { /* and not "--foo" */ 90240116Smarcel ++optind; 91240116Smarcel place = EMSG; 92240116Smarcel return (-1); 93240116Smarcel } 94240116Smarcel } /* option letter okay? */ 95240116Smarcel if ((optopt = (int)*place++) == (int)':' || 96240116Smarcel !(oli = strchr(ostr, optopt))) { 97240116Smarcel /* 98240116Smarcel * if the user didn't specify '-' as an option, 99240116Smarcel * assume it means -1. 100240116Smarcel */ 101240116Smarcel if (optopt == (int)'-') 102240116Smarcel return (-1); 103240116Smarcel if (!*place) 104240116Smarcel ++optind; 105240116Smarcel if (opterr && *ostr != ':') 106240116Smarcel (void)fprintf(stderr, 107240116Smarcel "%s: illegal option -- %c\n", __progname, optopt); 108240116Smarcel return (BADCH); 109240116Smarcel } 110240116Smarcel if (*++oli != ':') { /* don't need argument */ 111240116Smarcel optarg = NULL; 112240116Smarcel if (!*place) 113240116Smarcel ++optind; 114240116Smarcel } 115240116Smarcel else { /* need an argument */ 116240116Smarcel if (*place) /* no white space */ 117240116Smarcel optarg = place; 118240116Smarcel else if (nargc <= ++optind) { /* no arg */ 119240116Smarcel place = EMSG; 120240116Smarcel if (*ostr == ':') 121240116Smarcel return (BADARG); 122240116Smarcel if (opterr) 123240116Smarcel (void)fprintf(stderr, 124240116Smarcel "%s: option requires an argument -- %c\n", 125240116Smarcel __progname, optopt); 126240116Smarcel return (BADCH); 127240116Smarcel } 128240116Smarcel else /* white space */ 129240116Smarcel optarg = nargv[optind]; 130240116Smarcel place = EMSG; 131240116Smarcel ++optind; 132240116Smarcel } 133240116Smarcel return (optopt); /* dump back option letter */ 134240116Smarcel} 135240116Smarcel#endif 136240116Smarcel#ifdef MAIN 137240116Smarcel#ifndef BSD4_4 138240116Smarcelchar *__progname; 139240116Smarcel#endif 140240116Smarcel 141240116Smarcelint 142240116Smarcelmain(argc, argv) 143240116Smarcel int argc; 144240116Smarcel char *argv[]; 145240116Smarcel{ 146240116Smarcel int c; 147240116Smarcel char *opts = argv[1]; 148240116Smarcel 149240116Smarcel --argc; 150240116Smarcel ++argv; 151240116Smarcel 152240116Smarcel while ((c = getopt(argc, argv, opts)) != EOF) { 153240116Smarcel switch (c) { 154240116Smarcel case '-': 155240116Smarcel if (optarg) 156240116Smarcel printf("--%s ", optarg); 157240116Smarcel break; 158240116Smarcel case '?': 159240116Smarcel exit(1); 160240116Smarcel break; 161240116Smarcel default: 162240116Smarcel if (optarg) 163240116Smarcel printf("-%c %s ", c, optarg); 164240116Smarcel else 165240116Smarcel printf("-%c ", c); 166240116Smarcel break; 167240116Smarcel } 168240116Smarcel } 169240116Smarcel 170240116Smarcel if (optind < argc) { 171240116Smarcel printf("-- "); 172240116Smarcel for (; optind < argc; ++optind) { 173240116Smarcel printf("%s ", argv[optind]); 174240116Smarcel } 175240116Smarcel } 176240116Smarcel printf("\n"); 177240116Smarcel exit(0); 178240116Smarcel} 179240116Smarcel#endif 180240116Smarcel