Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

getopt.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  */
00007 /*
00008  * Copyright (c) 1987, 1993, 1994
00009  *      The Regents of the University of California.  All rights reserved.
00010  *
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions
00013  * are met:
00014  * 1. Redistributions of source code must retain the above copyright
00015  *    notice, this list of conditions and the following disclaimer.
00016  * 2. Redistributions in binary form must reproduce the above copyright
00017  *    notice, this list of conditions and the following disclaimer in the
00018  *    documentation and/or other materials provided with the distribution.
00019  * 3. Neither the name of the University nor the names of its contributors
00020  *    may be used to endorse or promote products derived from this software
00021  *    without specific prior written permission.
00022  *
00023  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00024  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00027  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00028  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00029  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00032  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00033  * SUCH DAMAGE.
00034  *
00035  * $Id: getopt.c,v 12.1 2005/06/16 20:20:48 bostic Exp $
00036  */
00037 
00038 #include "db_config.h"
00039 
00040 #ifndef NO_SYSTEM_INCLUDES
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044 #endif
00045 
00046 #include "db_int.h"
00047 
00048 int     __db_getopt_reset;      /* global reset for VxWorks. */
00049 
00050 int     opterr = 1,             /* if error message should be printed */
00051         optind = 1,             /* index into parent argv vector */
00052         optopt,                 /* character checked for validity */
00053         optreset;               /* reset getopt */
00054 char    *optarg;                /* argument associated with option */
00055 
00056 #undef  BADCH
00057 #define BADCH   (int)'?'
00058 #undef  BADARG
00059 #define BADARG  (int)':'
00060 #undef  EMSG
00061 #define EMSG    ""
00062 
00063 /*
00064  * getopt --
00065  *      Parse argc/argv argument vector.
00066  *
00067  * PUBLIC: #ifndef HAVE_GETOPT
00068  * PUBLIC: int getopt __P((int, char * const *, const char *));
00069  * PUBLIC: #endif
00070  */
00071 int
00072 getopt(nargc, nargv, ostr)
00073         int nargc;
00074         char * const *nargv;
00075         const char *ostr;
00076 {
00077         static char *progname;
00078         static char *place = EMSG;              /* option letter processing */
00079         char *oli;                              /* option letter list index */
00080 
00081         /*
00082          * VxWorks needs to be able to repeatedly call getopt from multiple
00083          * programs within its global name space.
00084          */
00085         if (__db_getopt_reset) {
00086                 __db_getopt_reset = 0;
00087 
00088                 opterr = optind = 1;
00089                 optopt = optreset = 0;
00090                 optarg = NULL;
00091                 progname = NULL;
00092                 place = EMSG;
00093         }
00094         if (!progname) {
00095                 if ((progname = __db_rpath(*nargv)) == NULL)
00096                         progname = *nargv;
00097                 else
00098                         ++progname;
00099         }
00100 
00101         if (optreset || !*place) {              /* update scanning pointer */
00102                 optreset = 0;
00103                 if (optind >= nargc || *(place = nargv[optind]) != '-') {
00104                         place = EMSG;
00105                         return (EOF);
00106                 }
00107                 if (place[1] && *++place == '-') {      /* found "--" */
00108                         ++optind;
00109                         place = EMSG;
00110                         return (EOF);
00111                 }
00112         }                                       /* option letter okay? */
00113         if ((optopt = (int)*place++) == (int)':' ||
00114             !(oli = strchr(ostr, optopt))) {
00115                 /*
00116                  * if the user didn't specify '-' as an option,
00117                  * assume it means EOF.
00118                  */
00119                 if (optopt == (int)'-')
00120                         return (EOF);
00121                 if (!*place)
00122                         ++optind;
00123                 if (opterr && *ostr != ':')
00124                         (void)fprintf(stderr,
00125                             "%s: illegal option -- %c\n", progname, optopt);
00126                 return (BADCH);
00127         }
00128         if (*++oli != ':') {                    /* don't need argument */
00129                 optarg = NULL;
00130                 if (!*place)
00131                         ++optind;
00132         }
00133         else {                                  /* need an argument */
00134                 if (*place)                     /* no white space */
00135                         optarg = place;
00136                 else if (nargc <= ++optind) {   /* no arg */
00137                         place = EMSG;
00138                         if (*ostr == ':')
00139                                 return (BADARG);
00140                         if (opterr)
00141                                 (void)fprintf(stderr,
00142                                     "%s: option requires an argument -- %c\n",
00143                                     progname, optopt);
00144                         return (BADCH);
00145                 }
00146                 else                            /* white space */
00147                         optarg = nargv[optind];
00148                 place = EMSG;
00149                 ++optind;
00150         }
00151         return (optopt);                        /* dump back option letter */
00152 }

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2