Blame snmplib/getopt.c

Packit fcad23
/*
Packit fcad23
 * Copyright (c) 1987, 1993, 1994
Packit fcad23
 *      The Regents of the University of California.  All rights reserved.
Packit fcad23
 *
Packit fcad23
 * Redistribution and use in source and binary forms, with or without
Packit fcad23
 * modification, are permitted provided that the following conditions
Packit fcad23
 * are met:
Packit fcad23
 * 1. Redistributions of source code must retain the above copyright
Packit fcad23
 *    notice, this list of conditions and the following disclaimer.
Packit fcad23
 * 2. Redistributions in binary form must reproduce the above copyright
Packit fcad23
 *    notice, this list of conditions and the following disclaimer in the
Packit fcad23
 *    documentation and/or other materials provided with the distribution.
Packit fcad23
 * 3. Neither the name of the University nor the names of its contributors
Packit fcad23
 *    may be used to endorse or promote products derived from this software
Packit fcad23
 *    without specific prior written permission.
Packit fcad23
 *
Packit fcad23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
Packit fcad23
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit fcad23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit fcad23
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
Packit fcad23
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit fcad23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
Packit fcad23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
Packit fcad23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
Packit fcad23
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
Packit fcad23
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
Packit fcad23
 * SUCH DAMAGE.
Packit fcad23
 */
Packit fcad23
Packit fcad23
#if defined(LIBC_SCCS) && !defined(lint)
Packit fcad23
/*
Packit fcad23
 * static char sccsid[] = "from: @(#)getopt.c   8.2 (Berkeley) 4/2/94"; 
Packit fcad23
 */
Packit fcad23
static char    *rcsid =
Packit fcad23
    "$Id$";
Packit fcad23
#endif                          /* LIBC_SCCS and not lint */
Packit fcad23
Packit fcad23
#include <net-snmp/net-snmp-config.h>
Packit fcad23
Packit fcad23
#include <stdio.h>
Packit fcad23
#include <stdlib.h>
Packit fcad23
#include <string.h>
Packit fcad23
Packit fcad23
#include <net-snmp/library/getopt.h>
Packit fcad23
Packit fcad23
#ifdef _BSD
Packit fcad23
extern char    *__progname;
Packit fcad23
#else
Packit fcad23
#define __progname "getopt"
Packit fcad23
#endif
Packit fcad23
Packit fcad23
int             opterr = 1,     /* if error message should be printed */
Packit fcad23
                optind = 1,     /* index into parent argv vector */
Packit fcad23
                optopt,         /* character checked for validity */
Packit fcad23
                optreset;       /* reset getopt */
Packit fcad23
char           *optarg;         /* argument associated with option */
Packit fcad23
char            EMSG[] = "";
Packit fcad23
Packit fcad23
#define	BADCH	(int)'?'
Packit fcad23
#define	BADARG	(int)':'
Packit fcad23
Packit fcad23
/*
Packit fcad23
 * getopt --
Packit fcad23
 *      Parse argc/argv argument vector.
Packit fcad23
 */
Packit fcad23
int
Packit fcad23
getopt(int nargc, char *const *nargv, const char *ostr)
Packit fcad23
{
Packit fcad23
    static char    *place = EMSG;       /* option letter processing */
Packit fcad23
    char           *oli;        /* option letter list index */
Packit fcad23
Packit fcad23
    if (optreset || !*place) {  /* update scanning pointer */
Packit fcad23
        optreset = 0;
Packit fcad23
        if (optind >= nargc || *(place = nargv[optind]) != '-') {
Packit fcad23
            place = EMSG;
Packit fcad23
            return (-1);
Packit fcad23
        }
Packit fcad23
        if (place[1] && *++place == '-') {      /* found "--" */
Packit fcad23
            ++optind;
Packit fcad23
            place = EMSG;
Packit fcad23
            return (-1);
Packit fcad23
        }
Packit fcad23
    }                           /* option letter okay? */
Packit fcad23
    if ((optopt = (int) *place++) == (int) ':' ||
Packit fcad23
        !(oli = strchr(ostr, optopt))) {
Packit fcad23
        /*
Packit fcad23
         * if the user didn't specify '-' as an option,
Packit fcad23
         * assume it means -1.
Packit fcad23
         */
Packit fcad23
        if (optopt == (int) '-')
Packit fcad23
            return (-1);
Packit fcad23
        if (!*place)
Packit fcad23
            ++optind;
Packit fcad23
        if (opterr && *ostr != ':')
Packit fcad23
            (void) fprintf(stderr,
Packit fcad23
                           "%s: illegal option -- %c\n", __progname,
Packit fcad23
                           optopt);
Packit fcad23
        return (BADCH);
Packit fcad23
    }
Packit fcad23
    if (*++oli != ':') {        /* don't need argument */
Packit fcad23
        optarg = NULL;
Packit fcad23
        if (!*place)
Packit fcad23
            ++optind;
Packit fcad23
    } else {                    /* need an argument */
Packit fcad23
        if (*place)             /* no white space */
Packit fcad23
            optarg = place;
Packit fcad23
        else if (nargc <= ++optind) {   /* no arg */
Packit fcad23
            place = EMSG;
Packit fcad23
            if (*ostr == ':')
Packit fcad23
                return (BADARG);
Packit fcad23
            if (opterr)
Packit fcad23
                (void) fprintf(stderr,
Packit fcad23
                               "%s: option requires an argument -- %c\n",
Packit fcad23
                               __progname, optopt);
Packit fcad23
            return (BADCH);
Packit fcad23
        } else                  /* white space */
Packit fcad23
            optarg = nargv[optind];
Packit fcad23
        place = EMSG;
Packit fcad23
        ++optind;
Packit fcad23
    }
Packit fcad23
    return (optopt);            /* dump back option letter */
Packit fcad23
}