net-snmp 5.7
|
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$"; 00036 #endif /* LIBC_SCCS and not lint */ 00037 00038 #include <net-snmp/net-snmp-config.h> 00039 00040 #include <stdio.h> 00041 #include <stdlib.h> 00042 #include <string.h> 00043 00044 #include <net-snmp/library/getopt.h> 00045 00046 #ifdef _BSD 00047 extern char *__progname; 00048 #else 00049 #define __progname "getopt" 00050 #endif 00051 00052 int opterr = 1, /* if error message should be printed */ 00053 optind = 1, /* index into parent argv vector */ 00054 optopt, /* character checked for validity */ 00055 optreset; /* reset getopt */ 00056 char *optarg; /* argument associated with option */ 00057 char EMSG[] = ""; 00058 00059 #define BADCH (int)'?' 00060 #define BADARG (int)':' 00061 00062 /* 00063 * getopt -- 00064 * Parse argc/argv argument vector. 00065 */ 00066 int 00067 getopt(int nargc, char *const *nargv, const char *ostr) 00068 { 00069 static char *place = EMSG; /* option letter processing */ 00070 char *oli; /* option letter list index */ 00071 00072 if (optreset || !*place) { /* update scanning pointer */ 00073 optreset = 0; 00074 if (optind >= nargc || *(place = nargv[optind]) != '-') { 00075 place = EMSG; 00076 return (-1); 00077 } 00078 if (place[1] && *++place == '-') { /* found "--" */ 00079 ++optind; 00080 place = EMSG; 00081 return (-1); 00082 } 00083 } /* option letter okay? */ 00084 if ((optopt = (int) *place++) == (int) ':' || 00085 !(oli = strchr(ostr, optopt))) { 00086 /* 00087 * if the user didn't specify '-' as an option, 00088 * assume it means -1. 00089 */ 00090 if (optopt == (int) '-') 00091 return (-1); 00092 if (!*place) 00093 ++optind; 00094 if (opterr && *ostr != ':') 00095 (void) fprintf(stderr, 00096 "%s: illegal option -- %c\n", __progname, 00097 optopt); 00098 return (BADCH); 00099 } 00100 if (*++oli != ':') { /* don't need argument */ 00101 optarg = NULL; 00102 if (!*place) 00103 ++optind; 00104 } else { /* need an argument */ 00105 if (*place) /* no white space */ 00106 optarg = place; 00107 else if (nargc <= ++optind) { /* no arg */ 00108 place = EMSG; 00109 if (*ostr == ':') 00110 return (BADARG); 00111 if (opterr) 00112 (void) fprintf(stderr, 00113 "%s: option requires an argument -- %c\n", 00114 __progname, optopt); 00115 return (BADCH); 00116 } else /* white space */ 00117 optarg = nargv[optind]; 00118 place = EMSG; 00119 ++optind; 00120 } 00121 return (optopt); /* dump back option letter */ 00122 }