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