cfopt.c revision 4bff34e37def8a90f9194d81bc345c52ba20086a
4bff34e37def8a90f9194d81bc345c52ba20086athurlow/*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Copyright (c) 2000, Boris Popov
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * All rights reserved.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * Redistribution and use in source and binary forms, with or without
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * modification, are permitted provided that the following conditions
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * are met:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 1. Redistributions of source code must retain the above copyright
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * notice, this list of conditions and the following disclaimer.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 2. Redistributions in binary form must reproduce the above copyright
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * notice, this list of conditions and the following disclaimer in the
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * documentation and/or other materials provided with the distribution.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 3. All advertising materials mentioning features or use of this software
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * must display the following acknowledgement:
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * This product includes software developed by Boris Popov.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * 4. Neither the name of the author nor the names of any co-contributors
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * may be used to endorse or promote products derived from this software
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * without specific prior written permission.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * SUCH DAMAGE.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow *
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * $Id: cfopt.c,v 1.1.1.1 2001/06/09 00:28:12 zarzycki Exp $
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#pragma ident "%Z%%M% %I% %E% SMI"
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <sys/param.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <stdio.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <string.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <libintl.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <cflib.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#include <netsmb/smb_lib.h>
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint cf_opterr = 1, /* if error message should be printed */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optind = 1, /* index into parent argv vector */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optopt, /* character checked for validity */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optreset; /* reset getopt */
4bff34e37def8a90f9194d81bc345c52ba20086athurlowconst char *cf_optarg; /* argument associated with option */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#define BADCH (int)'?'
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#define BADARG (int)':'
4bff34e37def8a90f9194d81bc345c52ba20086athurlow#define EMSG ""
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlowint
4bff34e37def8a90f9194d81bc345c52ba20086athurlowcf_getopt(nargc, nargv, ostr)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int nargc;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char * const *nargv;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow const char *ostr;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow{
4bff34e37def8a90f9194d81bc345c52ba20086athurlow static const char *place = EMSG; /* option letter processing */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow char *oli; /* option letter list index */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow int tmpind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (cf_optreset || !*place) { /* update scanning pointer */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optreset = 0;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow tmpind = cf_optind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow while (1) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (tmpind >= nargc) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow place = EMSG;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (-1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (*(place = nargv[tmpind]) != '-') {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow tmpind++;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow continue; /* lookup next option */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (place[1] && *++place == '-') { /* found "--" */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optind = ++tmpind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow place = EMSG;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (-1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optind = tmpind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow break;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow } /* option letter okay? */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if ((cf_optopt = (int)*place++) == (int)':' ||
4bff34e37def8a90f9194d81bc345c52ba20086athurlow !(oli = strchr(ostr, cf_optopt))) {
4bff34e37def8a90f9194d81bc345c52ba20086athurlow /*
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * if the user didn't specify '-' as an option,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow * assume it means -1.
4bff34e37def8a90f9194d81bc345c52ba20086athurlow */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (cf_optopt == (int)'-')
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (-1);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (!*place)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ++cf_optind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (cf_opterr && *ostr != ':')
4bff34e37def8a90f9194d81bc345c52ba20086athurlow (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow "%s: illegal option -- %c\n"),
4bff34e37def8a90f9194d81bc345c52ba20086athurlow __progname, cf_optopt);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (BADCH);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (*++oli != ':') { /* don't need argument */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optarg = NULL;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (!*place)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ++cf_optind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow } else { /* need an argument */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (*place) /* no white space */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optarg = place;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow else if (nargc <= ++cf_optind) { /* no arg */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow place = EMSG;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (*ostr == ':')
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (BADARG);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow if (cf_opterr)
4bff34e37def8a90f9194d81bc345c52ba20086athurlow (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4bff34e37def8a90f9194d81bc345c52ba20086athurlow "%s: option requires an argument -- %c\n"),
4bff34e37def8a90f9194d81bc345c52ba20086athurlow __progname, cf_optopt);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (BADCH);
4bff34e37def8a90f9194d81bc345c52ba20086athurlow } else /* white space */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow cf_optarg = nargv[cf_optind];
4bff34e37def8a90f9194d81bc345c52ba20086athurlow place = EMSG;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow ++cf_optind;
4bff34e37def8a90f9194d81bc345c52ba20086athurlow }
4bff34e37def8a90f9194d81bc345c52ba20086athurlow return (cf_optopt); /* dump back option letter */
4bff34e37def8a90f9194d81bc345c52ba20086athurlow}