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