14bff34e3Sthurlow /* 24bff34e3Sthurlow * Copyright (c) 2000, Boris Popov 34bff34e3Sthurlow * All rights reserved. 44bff34e3Sthurlow * 54bff34e3Sthurlow * Redistribution and use in source and binary forms, with or without 64bff34e3Sthurlow * modification, are permitted provided that the following conditions 74bff34e3Sthurlow * are met: 84bff34e3Sthurlow * 1. Redistributions of source code must retain the above copyright 94bff34e3Sthurlow * notice, this list of conditions and the following disclaimer. 104bff34e3Sthurlow * 2. Redistributions in binary form must reproduce the above copyright 114bff34e3Sthurlow * notice, this list of conditions and the following disclaimer in the 124bff34e3Sthurlow * documentation and/or other materials provided with the distribution. 134bff34e3Sthurlow * 3. All advertising materials mentioning features or use of this software 144bff34e3Sthurlow * must display the following acknowledgement: 154bff34e3Sthurlow * This product includes software developed by Boris Popov. 164bff34e3Sthurlow * 4. Neither the name of the author nor the names of any co-contributors 174bff34e3Sthurlow * may be used to endorse or promote products derived from this software 184bff34e3Sthurlow * without specific prior written permission. 194bff34e3Sthurlow * 204bff34e3Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 214bff34e3Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 224bff34e3Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 234bff34e3Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 244bff34e3Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 254bff34e3Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 264bff34e3Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 274bff34e3Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 284bff34e3Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 294bff34e3Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 304bff34e3Sthurlow * SUCH DAMAGE. 314bff34e3Sthurlow * 324bff34e3Sthurlow * $Id: cfopt.c,v 1.1.1.1 2001/06/09 00:28:12 zarzycki Exp $ 334bff34e3Sthurlow */ 344bff34e3Sthurlow 35*613a2f6bSGordon Ross #include <sys/types.h> 364bff34e3Sthurlow 374bff34e3Sthurlow #include <stdio.h> 384bff34e3Sthurlow #include <string.h> 39*613a2f6bSGordon Ross #include <synch.h> 404bff34e3Sthurlow #include <libintl.h> 414bff34e3Sthurlow 424bff34e3Sthurlow #include <cflib.h> 434bff34e3Sthurlow #include <netsmb/smb_lib.h> 44*613a2f6bSGordon Ross #include <assert.h> 45*613a2f6bSGordon Ross 46*613a2f6bSGordon Ross /* lock for the variables below */ 47*613a2f6bSGordon Ross mutex_t cf_opt_mutex = DEFAULTMUTEX; 484bff34e3Sthurlow 494bff34e3Sthurlow int cf_opterr = 1, /* if error message should be printed */ 504bff34e3Sthurlow cf_optind = 1, /* index into parent argv vector */ 514bff34e3Sthurlow cf_optopt, /* character checked for validity */ 524bff34e3Sthurlow cf_optreset; /* reset getopt */ 534bff34e3Sthurlow const char *cf_optarg; /* argument associated with option */ 544bff34e3Sthurlow 554bff34e3Sthurlow #define BADCH (int)'?' 564bff34e3Sthurlow #define BADARG (int)':' 574bff34e3Sthurlow #define EMSG "" 584bff34e3Sthurlow 59*613a2f6bSGordon Ross void 60*613a2f6bSGordon Ross cf_opt_lock(void) 61*613a2f6bSGordon Ross { 62*613a2f6bSGordon Ross mutex_lock(&cf_opt_mutex); 63*613a2f6bSGordon Ross } 64*613a2f6bSGordon Ross 65*613a2f6bSGordon Ross void 66*613a2f6bSGordon Ross cf_opt_unlock(void) 67*613a2f6bSGordon Ross { 68*613a2f6bSGordon Ross mutex_unlock(&cf_opt_mutex); 69*613a2f6bSGordon Ross } 70*613a2f6bSGordon Ross 714bff34e3Sthurlow int 724bff34e3Sthurlow cf_getopt(nargc, nargv, ostr) 734bff34e3Sthurlow int nargc; 744bff34e3Sthurlow char * const *nargv; 754bff34e3Sthurlow const char *ostr; 764bff34e3Sthurlow { 774bff34e3Sthurlow static const char *place = EMSG; /* option letter processing */ 784bff34e3Sthurlow char *oli; /* option letter list index */ 794bff34e3Sthurlow int tmpind; 804bff34e3Sthurlow 81*613a2f6bSGordon Ross assert(MUTEX_HELD(&cf_opt_mutex)); 82*613a2f6bSGordon Ross 834bff34e3Sthurlow if (cf_optreset || !*place) { /* update scanning pointer */ 844bff34e3Sthurlow cf_optreset = 0; 854bff34e3Sthurlow tmpind = cf_optind; 86*613a2f6bSGordon Ross for (;;) { 874bff34e3Sthurlow if (tmpind >= nargc) { 884bff34e3Sthurlow place = EMSG; 894bff34e3Sthurlow return (-1); 904bff34e3Sthurlow } 914bff34e3Sthurlow if (*(place = nargv[tmpind]) != '-') { 924bff34e3Sthurlow tmpind++; 934bff34e3Sthurlow continue; /* lookup next option */ 944bff34e3Sthurlow } 954bff34e3Sthurlow if (place[1] && *++place == '-') { /* found "--" */ 964bff34e3Sthurlow cf_optind = ++tmpind; 974bff34e3Sthurlow place = EMSG; 984bff34e3Sthurlow return (-1); 994bff34e3Sthurlow } 1004bff34e3Sthurlow cf_optind = tmpind; 1014bff34e3Sthurlow break; 1024bff34e3Sthurlow } 1034bff34e3Sthurlow } /* option letter okay? */ 1044bff34e3Sthurlow if ((cf_optopt = (int)*place++) == (int)':' || 1054bff34e3Sthurlow !(oli = strchr(ostr, cf_optopt))) { 1064bff34e3Sthurlow /* 1074bff34e3Sthurlow * if the user didn't specify '-' as an option, 1084bff34e3Sthurlow * assume it means -1. 1094bff34e3Sthurlow */ 1104bff34e3Sthurlow if (cf_optopt == (int)'-') 1114bff34e3Sthurlow return (-1); 1124bff34e3Sthurlow if (!*place) 1134bff34e3Sthurlow ++cf_optind; 1144bff34e3Sthurlow if (cf_opterr && *ostr != ':') 1154bff34e3Sthurlow (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1164bff34e3Sthurlow "%s: illegal option -- %c\n"), 1174bff34e3Sthurlow __progname, cf_optopt); 1184bff34e3Sthurlow return (BADCH); 1194bff34e3Sthurlow } 1204bff34e3Sthurlow if (*++oli != ':') { /* don't need argument */ 1214bff34e3Sthurlow cf_optarg = NULL; 1224bff34e3Sthurlow if (!*place) 1234bff34e3Sthurlow ++cf_optind; 1244bff34e3Sthurlow } else { /* need an argument */ 1254bff34e3Sthurlow if (*place) /* no white space */ 1264bff34e3Sthurlow cf_optarg = place; 1274bff34e3Sthurlow else if (nargc <= ++cf_optind) { /* no arg */ 1284bff34e3Sthurlow place = EMSG; 1294bff34e3Sthurlow if (*ostr == ':') 1304bff34e3Sthurlow return (BADARG); 1314bff34e3Sthurlow if (cf_opterr) 1324bff34e3Sthurlow (void) fprintf(stderr, dgettext(TEXT_DOMAIN, 1334bff34e3Sthurlow "%s: option requires an argument -- %c\n"), 1344bff34e3Sthurlow __progname, cf_optopt); 1354bff34e3Sthurlow return (BADCH); 1364bff34e3Sthurlow } else /* white space */ 1374bff34e3Sthurlow cf_optarg = nargv[cf_optind]; 1384bff34e3Sthurlow place = EMSG; 1394bff34e3Sthurlow ++cf_optind; 1404bff34e3Sthurlow } 1414bff34e3Sthurlow return (cf_optopt); /* dump back option letter */ 1424bff34e3Sthurlow } 143