"Fossies" - the Fresh Open Source Software Archive

Member "libzip-1.5.2/src/getopt.c" (12 Mar 2019, 3667 Bytes) of package /linux/misc/libzip-1.5.2.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "getopt.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.4.0_vs_1.5.0.

    1 /*
    2  * getopt.c --
    3  *
    4  *      Standard UNIX getopt function.  Code is from BSD.
    5  *
    6  * Copyright (c) 1987-2002 The Regents of the University of California.
    7  * All rights reserved.
    8  *
    9  * Redistribution and use in source and binary forms, with or without
   10  * modification, are permitted provided that the following conditions are met:
   11  *
   12  * A. Redistributions of source code must retain the above copyright notice,
   13  *    this list of conditions and the following disclaimer.
   14  * B. Redistributions in binary form must reproduce the above copyright notice,
   15  *    this list of conditions and the following disclaimer in the documentation
   16  *    and/or other materials provided with the distribution.
   17  * C. Neither the names of the copyright holders nor the names of its
   18  *    contributors may be used to endorse or promote products derived from this
   19  *    software without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
   22  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
   23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
   25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   31  * POSSIBILITY OF SUCH DAMAGE.
   32  */
   33 
   34 /* #if !defined(lint)
   35  * static char sccsid[] = "@(#)getopt.c 8.2 (Berkeley) 4/2/94";
   36  * #endif
   37  */
   38 #include <stdio.h>
   39 #include <stdlib.h>
   40 #include <string.h>
   41 
   42 #include "getopt.h"
   43 
   44 int opterr = 1, /* if error message should be printed */
   45     optind = 1, /* index into parent argv vector */
   46     optopt,     /* character checked for validity */
   47     optreset;   /* reset getopt */
   48 char *optarg;   /* argument associated with option */
   49 
   50 #define BADCH (int)'?'
   51 #define BADARG (int)':'
   52 #define EMSG ""
   53 
   54 /*
   55  * getopt --
   56  *      Parse argc/argv argument vector.
   57  */
   58 int
   59 getopt(int nargc, char *const *nargv, const char *ostr) {
   60     static char *place = EMSG; /* option letter processing */
   61     char *oli;                 /* option letter list index */
   62 
   63     if (optreset || !*place) { /* update scanning pointer */
   64     optreset = 0;
   65     if (optind >= nargc || *(place = nargv[optind]) != '-') {
   66         place = EMSG;
   67         return (EOF);
   68     }
   69     if (place[1] && *++place == '-') { /* found "--" */
   70         ++optind;
   71         place = EMSG;
   72         return (EOF);
   73     }
   74     } /* option letter okay? */
   75     if ((optopt = (int)*place++) == (int)':' || !(oli = (char *)strchr(ostr, optopt))) {
   76     /*
   77      * if the user didn't specify '-' as an option,
   78      * assume it means EOF.
   79      */
   80     if (optopt == (int)'-')
   81         return (EOF);
   82     if (!*place)
   83         ++optind;
   84     if (opterr && *ostr != ':')
   85         (void)fprintf(stderr, "illegal option -- %c\n", optopt);
   86     return (BADCH);
   87     }
   88     if (*++oli != ':') { /* don't need argument */
   89     optarg = NULL;
   90     if (!*place)
   91         ++optind;
   92     }
   93     else {          /* need an argument */
   94     if (*place) /* no white space */
   95         optarg = place;
   96     else if (nargc <= ++optind) { /* no arg */
   97         place = EMSG;
   98         if (*ostr == ':')
   99         return (BADARG);
  100         if (opterr)
  101         (void)fprintf(stderr, "option requires an argument -- %c\n", optopt);
  102         return (BADCH);
  103     }
  104     else /* white space */
  105         optarg = nargv[optind];
  106     place = EMSG;
  107     ++optind;
  108     }
  109     return (optopt); /* dump back option letter */
  110 }