|
roentgen |
b75cab |
/* $Id: getopt.c,v 1.3 2009-01-22 20:53:07 fwarmerdam Exp $ */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* Copyright (c) 1987, 1993, 1994
|
|
roentgen |
b75cab |
* The Regents of the University of California. All rights reserved.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* Redistribution and use in source and binary forms, with or without
|
|
roentgen |
b75cab |
* modification, are permitted provided that the following conditions
|
|
roentgen |
b75cab |
* are met:
|
|
roentgen |
b75cab |
* 1. Redistributions of source code must retain the above copyright
|
|
roentgen |
b75cab |
* notice, this list of conditions and the following disclaimer.
|
|
roentgen |
b75cab |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
roentgen |
b75cab |
* notice, this list of conditions and the following disclaimer in the
|
|
roentgen |
b75cab |
* documentation and/or other materials provided with the distribution.
|
|
roentgen |
b75cab |
* 3. Neither the name of the University nor the names of its contributors
|
|
roentgen |
b75cab |
* may be used to endorse or promote products derived from this software
|
|
roentgen |
b75cab |
* without specific prior written permission.
|
|
roentgen |
b75cab |
*
|
|
roentgen |
b75cab |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
roentgen |
b75cab |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
roentgen |
b75cab |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
roentgen |
b75cab |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
roentgen |
b75cab |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
roentgen |
b75cab |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
roentgen |
b75cab |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
roentgen |
b75cab |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
roentgen |
b75cab |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
roentgen |
b75cab |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
roentgen |
b75cab |
* SUCH DAMAGE.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#if 0
|
|
roentgen |
b75cab |
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
|
|
roentgen |
b75cab |
__RCSID("$NetBSD: getopt.c,v 1.26 2003/08/07 16:43:40 agc Exp $");
|
|
roentgen |
b75cab |
#endif
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#include <stdio.h></stdio.h>
|
|
roentgen |
b75cab |
#include <string.h></string.h>
|
|
roentgen |
b75cab |
#include "libport.h"
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
int opterr = 1, /* if error message should be printed */
|
|
roentgen |
b75cab |
optind = 1, /* index into parent argv vector */
|
|
roentgen |
b75cab |
optopt, /* character checked for validity */
|
|
roentgen |
b75cab |
optreset; /* reset getopt */
|
|
roentgen |
b75cab |
char *optarg; /* argument associated with option */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
#define BADCH (int)'?'
|
|
roentgen |
b75cab |
#define BADARG (int)':'
|
|
roentgen |
b75cab |
#define EMSG ""
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/*
|
|
roentgen |
b75cab |
* getopt --
|
|
roentgen |
b75cab |
* Parse argc/argv argument vector.
|
|
roentgen |
b75cab |
*/
|
|
roentgen |
b75cab |
int
|
|
roentgen |
b75cab |
getopt(int argc, char * const argv[], const char *optstring)
|
|
roentgen |
b75cab |
{
|
|
roentgen |
b75cab |
static char *place = EMSG; /* option letter processing */
|
|
roentgen |
b75cab |
char *oli; /* option letter list index */
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
if (optreset || *place == 0) { /* update scanning pointer */
|
|
roentgen |
b75cab |
optreset = 0;
|
|
roentgen |
b75cab |
place = argv[optind];
|
|
roentgen |
b75cab |
if (optind >= argc || *place++ != '-') {
|
|
roentgen |
b75cab |
/* Argument is absent or is not an option */
|
|
roentgen |
b75cab |
place = EMSG;
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
optopt = *place++;
|
|
roentgen |
b75cab |
if (optopt == '-' && *place == 0) {
|
|
roentgen |
b75cab |
/* "--" => end of options */
|
|
roentgen |
b75cab |
++optind;
|
|
roentgen |
b75cab |
place = EMSG;
|
|
roentgen |
b75cab |
return (-1);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
if (optopt == 0) {
|
|
roentgen |
b75cab |
/* Solitary '-', treat as a '-' option
|
|
roentgen |
b75cab |
if the program (eg su) is looking for it. */
|
|
roentgen |
b75cab |
place = EMSG;
|
|
roentgen |
b75cab |
if (strchr(optstring, '-') == NULL)
|
|
roentgen |
b75cab |
return -1;
|
|
roentgen |
b75cab |
optopt = '-';
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
} else
|
|
roentgen |
b75cab |
optopt = *place++;
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/* See if option letter is one the caller wanted... */
|
|
roentgen |
b75cab |
if (optopt == ':' || (oli = strchr(optstring, optopt)) == NULL) {
|
|
roentgen |
b75cab |
if (*place == 0)
|
|
roentgen |
b75cab |
++optind;
|
|
roentgen |
b75cab |
if (opterr && *optstring != ':')
|
|
roentgen |
b75cab |
(void)fprintf(stderr,
|
|
roentgen |
b75cab |
"unknown option -- %c\n", optopt);
|
|
roentgen |
b75cab |
return (BADCH);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
|
|
roentgen |
b75cab |
/* Does this option need an argument? */
|
|
roentgen |
b75cab |
if (oli[1] != ':') {
|
|
roentgen |
b75cab |
/* don't need argument */
|
|
roentgen |
b75cab |
optarg = NULL;
|
|
roentgen |
b75cab |
if (*place == 0)
|
|
roentgen |
b75cab |
++optind;
|
|
roentgen |
b75cab |
} else {
|
|
roentgen |
b75cab |
/* Option-argument is either the rest of this argument or the
|
|
roentgen |
b75cab |
entire next argument. */
|
|
roentgen |
b75cab |
if (*place)
|
|
roentgen |
b75cab |
optarg = place;
|
|
roentgen |
b75cab |
else if (argc > ++optind)
|
|
roentgen |
b75cab |
optarg = argv[optind];
|
|
roentgen |
b75cab |
else {
|
|
roentgen |
b75cab |
/* option-argument absent */
|
|
roentgen |
b75cab |
place = EMSG;
|
|
roentgen |
b75cab |
if (*optstring == ':')
|
|
roentgen |
b75cab |
return (BADARG);
|
|
roentgen |
b75cab |
if (opterr)
|
|
roentgen |
b75cab |
(void)fprintf(stderr,
|
|
roentgen |
b75cab |
"option requires an argument -- %c\n",
|
|
roentgen |
b75cab |
optopt);
|
|
roentgen |
b75cab |
return (BADCH);
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
place = EMSG;
|
|
roentgen |
b75cab |
++optind;
|
|
roentgen |
b75cab |
}
|
|
roentgen |
b75cab |
return (optopt); /* return option letter */
|
|
roentgen |
b75cab |
}
|