16007Sthurlow /*
26007Sthurlow * Copyright (c) 2000, Boris Popov
36007Sthurlow * All rights reserved.
46007Sthurlow *
56007Sthurlow * Redistribution and use in source and binary forms, with or without
66007Sthurlow * modification, are permitted provided that the following conditions
76007Sthurlow * are met:
86007Sthurlow * 1. Redistributions of source code must retain the above copyright
96007Sthurlow * notice, this list of conditions and the following disclaimer.
106007Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
116007Sthurlow * notice, this list of conditions and the following disclaimer in the
126007Sthurlow * documentation and/or other materials provided with the distribution.
136007Sthurlow * 3. All advertising materials mentioning features or use of this software
146007Sthurlow * must display the following acknowledgement:
156007Sthurlow * This product includes software developed by Boris Popov.
166007Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
176007Sthurlow * may be used to endorse or promote products derived from this software
186007Sthurlow * without specific prior written permission.
196007Sthurlow *
206007Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
216007Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226007Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236007Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
246007Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
256007Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
266007Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
276007Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
286007Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
296007Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
306007Sthurlow * SUCH DAMAGE.
316007Sthurlow *
326007Sthurlow * $Id: cfopt.c,v 1.1.1.1 2001/06/09 00:28:12 zarzycki Exp $
336007Sthurlow */
346007Sthurlow
35*10023SGordon.Ross@Sun.COM #include <sys/types.h>
366007Sthurlow
376007Sthurlow #include <stdio.h>
386007Sthurlow #include <string.h>
39*10023SGordon.Ross@Sun.COM #include <synch.h>
406007Sthurlow #include <libintl.h>
416007Sthurlow
426007Sthurlow #include <cflib.h>
436007Sthurlow #include <netsmb/smb_lib.h>
44*10023SGordon.Ross@Sun.COM #include <assert.h>
45*10023SGordon.Ross@Sun.COM
46*10023SGordon.Ross@Sun.COM /* lock for the variables below */
47*10023SGordon.Ross@Sun.COM mutex_t cf_opt_mutex = DEFAULTMUTEX;
486007Sthurlow
496007Sthurlow int cf_opterr = 1, /* if error message should be printed */
506007Sthurlow cf_optind = 1, /* index into parent argv vector */
516007Sthurlow cf_optopt, /* character checked for validity */
526007Sthurlow cf_optreset; /* reset getopt */
536007Sthurlow const char *cf_optarg; /* argument associated with option */
546007Sthurlow
556007Sthurlow #define BADCH (int)'?'
566007Sthurlow #define BADARG (int)':'
576007Sthurlow #define EMSG ""
586007Sthurlow
59*10023SGordon.Ross@Sun.COM void
cf_opt_lock(void)60*10023SGordon.Ross@Sun.COM cf_opt_lock(void)
61*10023SGordon.Ross@Sun.COM {
62*10023SGordon.Ross@Sun.COM mutex_lock(&cf_opt_mutex);
63*10023SGordon.Ross@Sun.COM }
64*10023SGordon.Ross@Sun.COM
65*10023SGordon.Ross@Sun.COM void
cf_opt_unlock(void)66*10023SGordon.Ross@Sun.COM cf_opt_unlock(void)
67*10023SGordon.Ross@Sun.COM {
68*10023SGordon.Ross@Sun.COM mutex_unlock(&cf_opt_mutex);
69*10023SGordon.Ross@Sun.COM }
70*10023SGordon.Ross@Sun.COM
716007Sthurlow int
cf_getopt(nargc,nargv,ostr)726007Sthurlow cf_getopt(nargc, nargv, ostr)
736007Sthurlow int nargc;
746007Sthurlow char * const *nargv;
756007Sthurlow const char *ostr;
766007Sthurlow {
776007Sthurlow static const char *place = EMSG; /* option letter processing */
786007Sthurlow char *oli; /* option letter list index */
796007Sthurlow int tmpind;
806007Sthurlow
81*10023SGordon.Ross@Sun.COM assert(MUTEX_HELD(&cf_opt_mutex));
82*10023SGordon.Ross@Sun.COM
836007Sthurlow if (cf_optreset || !*place) { /* update scanning pointer */
846007Sthurlow cf_optreset = 0;
856007Sthurlow tmpind = cf_optind;
86*10023SGordon.Ross@Sun.COM for (;;) {
876007Sthurlow if (tmpind >= nargc) {
886007Sthurlow place = EMSG;
896007Sthurlow return (-1);
906007Sthurlow }
916007Sthurlow if (*(place = nargv[tmpind]) != '-') {
926007Sthurlow tmpind++;
936007Sthurlow continue; /* lookup next option */
946007Sthurlow }
956007Sthurlow if (place[1] && *++place == '-') { /* found "--" */
966007Sthurlow cf_optind = ++tmpind;
976007Sthurlow place = EMSG;
986007Sthurlow return (-1);
996007Sthurlow }
1006007Sthurlow cf_optind = tmpind;
1016007Sthurlow break;
1026007Sthurlow }
1036007Sthurlow } /* option letter okay? */
1046007Sthurlow if ((cf_optopt = (int)*place++) == (int)':' ||
1056007Sthurlow !(oli = strchr(ostr, cf_optopt))) {
1066007Sthurlow /*
1076007Sthurlow * if the user didn't specify '-' as an option,
1086007Sthurlow * assume it means -1.
1096007Sthurlow */
1106007Sthurlow if (cf_optopt == (int)'-')
1116007Sthurlow return (-1);
1126007Sthurlow if (!*place)
1136007Sthurlow ++cf_optind;
1146007Sthurlow if (cf_opterr && *ostr != ':')
1156007Sthurlow (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1166007Sthurlow "%s: illegal option -- %c\n"),
1176007Sthurlow __progname, cf_optopt);
1186007Sthurlow return (BADCH);
1196007Sthurlow }
1206007Sthurlow if (*++oli != ':') { /* don't need argument */
1216007Sthurlow cf_optarg = NULL;
1226007Sthurlow if (!*place)
1236007Sthurlow ++cf_optind;
1246007Sthurlow } else { /* need an argument */
1256007Sthurlow if (*place) /* no white space */
1266007Sthurlow cf_optarg = place;
1276007Sthurlow else if (nargc <= ++cf_optind) { /* no arg */
1286007Sthurlow place = EMSG;
1296007Sthurlow if (*ostr == ':')
1306007Sthurlow return (BADARG);
1316007Sthurlow if (cf_opterr)
1326007Sthurlow (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1336007Sthurlow "%s: option requires an argument -- %c\n"),
1346007Sthurlow __progname, cf_optopt);
1356007Sthurlow return (BADCH);
1366007Sthurlow } else /* white space */
1376007Sthurlow cf_optarg = nargv[cf_optind];
1386007Sthurlow place = EMSG;
1396007Sthurlow ++cf_optind;
1406007Sthurlow }
1416007Sthurlow return (cf_optopt); /* dump back option letter */
1426007Sthurlow }
143