00001 /* 00002 * Copyright (c) 1987, 1993, 1994 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the University nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 */ 00029 00030 #if defined(LIBC_SCCS) && !defined(lint) 00031 /* 00032 * static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; 00033 */ 00034 static char *rcsid = 00035 "$Id: getopt.c 13265 2005-10-27 09:43:13Z dts12 $"; 00036 #endif /* LIBC_SCCS and not lint */ 00037 00038 #include <stdio.h> 00039 #include <stdlib.h> 00040 #include <string.h> 00041 00042 #ifdef _BSD 00043 extern char *__progname; 00044 #else 00045 #define __progname "getopt" 00046 #endif 00047 00048 int opterr = 1, /* if error message should be printed */ 00049 optind = 1, /* index into parent argv vector */ 00050 optopt, /* character checked for validity */ 00051 optreset; /* reset getopt */ 00052 char *optarg; /* argument associated with option */ 00053 char EMSG[] = ""; 00054 00055 #define BADCH (int)'?' 00056 #define BADARG (int)':' 00057 00058 /* 00059 * getopt -- 00060 * Parse argc/argv argument vector. 00061 */ 00062 int 00063 getopt(int nargc, char *const *nargv, const char *ostr) 00064 { 00065 static char *place = EMSG; /* option letter processing */ 00066 char *oli; /* option letter list index */ 00067 00068 if (optreset || !*place) { /* update scanning pointer */ 00069 optreset = 0; 00070 if (optind >= nargc || *(place = nargv[optind]) != '-') { 00071 place = EMSG; 00072 return (-1); 00073 } 00074 if (place[1] && *++place == '-') { /* found "--" */ 00075 ++optind; 00076 place = EMSG; 00077 return (-1); 00078 } 00079 } /* option letter okay? */ 00080 if ((optopt = (int) *place++) == (int) ':' || 00081 !(oli = strchr(ostr, optopt))) { 00082 /* 00083 * if the user didn't specify '-' as an option, 00084 * assume it means -1. 00085 */ 00086 if (optopt == (int) '-') 00087 return (-1); 00088 if (!*place) 00089 ++optind; 00090 if (opterr && *ostr != ':') 00091 (void) fprintf(stderr, 00092 "%s: illegal option -- %c\n", __progname, 00093 optopt); 00094 return (BADCH); 00095 } 00096 if (*++oli != ':') { /* don't need argument */ 00097 optarg = NULL; 00098 if (!*place) 00099 ++optind; 00100 } else { /* need an argument */ 00101 if (*place) /* no white space */ 00102 optarg = place; 00103 else if (nargc <= ++optind) { /* no arg */ 00104 place = EMSG; 00105 if (*ostr == ':') 00106 return (BADARG); 00107 if (opterr) 00108 (void) fprintf(stderr, 00109 "%s: option requires an argument -- %c\n", 00110 __progname, optopt); 00111 return (BADCH); 00112 } else /* white space */ 00113 optarg = nargv[optind]; 00114 place = EMSG; 00115 ++optind; 00116 } 00117 return (optopt); /* dump back option letter */ 00118 }