Header And Logo

PostgreSQL
| The world's most advanced open source database.

getopt.c

Go to the documentation of this file.
00001 /* src/port/getopt.c */
00002 
00003 /* This is used by psql under Win32 */
00004 
00005 /*
00006  * Copyright (c) 1987, 1993, 1994
00007  *  The Regents of the University of California.  All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  * 3. Neither the name of the University nor the names of its contributors
00018  *    may be used to endorse or promote products derived from this software
00019  *    without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  */
00032 
00033 #include "c.h"
00034 
00035 #if defined(LIBC_SCCS) && !defined(lint)
00036 static char sccsid[] = "@(#)getopt.c    8.3 (Berkeley) 4/27/95";
00037 #endif   /* LIBC_SCCS and not lint */
00038 
00039 
00040 /*
00041  * On some versions of Solaris, opterr and friends are defined in core libc
00042  * rather than in a separate getopt module.  Define these variables only
00043  * if configure found they aren't there by default.  (We assume that testing
00044  * opterr is sufficient for all of these.)
00045  */
00046 #ifndef HAVE_INT_OPTERR
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 char       *optarg;             /* argument associated with option */
00052 #else
00053 
00054 extern int  opterr;
00055 extern int  optind;
00056 extern int  optopt;
00057 extern char *optarg;
00058 #endif
00059 
00060 #define BADCH   (int)'?'
00061 #define BADARG  (int)':'
00062 #define EMSG    ""
00063 
00064 int         getopt(int nargc, char *const * nargv, const char *ostr);
00065 
00066 /*
00067  * getopt
00068  *  Parse argc/argv argument vector.
00069  *
00070  * This implementation does not use optreset.  Instead, we guarantee that
00071  * it can be restarted on a new argv array after a previous call returned -1,
00072  * if the caller resets optind to 1 before the first call of the new series.
00073  * (Internally, this means we must be sure to reset "place" to EMSG before
00074  * returning -1.)
00075  */
00076 int
00077 getopt(int nargc, char *const * nargv, const char *ostr)
00078 {
00079     static char *place = EMSG;  /* option letter processing */
00080     char       *oli;            /* option letter list index */
00081 
00082     if (!*place)
00083     {                           /* update scanning pointer */
00084         if (optind >= nargc || *(place = nargv[optind]) != '-')
00085         {
00086             place = EMSG;
00087             return -1;
00088         }
00089         if (place[1] && *++place == '-' && place[1] == '\0')
00090         {                       /* found "--" */
00091             ++optind;
00092             place = EMSG;
00093             return -1;
00094         }
00095     }                           /* option letter okay? */
00096     if ((optopt = (int) *place++) == (int) ':' ||
00097         !(oli = strchr(ostr, optopt)))
00098     {
00099         /*
00100          * if the user didn't specify '-' as an option, assume it means -1.
00101          */
00102         if (optopt == (int) '-')
00103         {
00104             place = EMSG;
00105             return -1;
00106         }
00107         if (!*place)
00108             ++optind;
00109         if (opterr && *ostr != ':')
00110             (void) fprintf(stderr,
00111                            "illegal option -- %c\n", optopt);
00112         return BADCH;
00113     }
00114     if (*++oli != ':')
00115     {                           /* don't need argument */
00116         optarg = NULL;
00117         if (!*place)
00118             ++optind;
00119     }
00120     else
00121     {                           /* need an argument */
00122         if (*place)             /* no white space */
00123             optarg = place;
00124         else if (nargc <= ++optind)
00125         {                       /* no arg */
00126             place = EMSG;
00127             if (*ostr == ':')
00128                 return BADARG;
00129             if (opterr)
00130                 (void) fprintf(stderr,
00131                                "option requires an argument -- %c\n",
00132                                optopt);
00133             return BADCH;
00134         }
00135         else
00136             /* white space */
00137             optarg = nargv[optind];
00138         place = EMSG;
00139         ++optind;
00140     }
00141     return optopt;              /* dump back option letter */
00142 }