michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /* michael@0: * Copyright (c) 1987 Regents of the University of California. michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms are permitted michael@0: * provided that: (1) source distributions retain this entire copyright michael@0: * notice and comment, and (2) distributions including binaries display michael@0: * the following acknowledgement: ``This product includes software michael@0: * developed by the University of California, Berkeley and its contributors'' michael@0: * in the documentation or other materials provided with the distribution michael@0: * and in all advertising materials mentioning features or use of this michael@0: * software. Neither the name of the University nor the names of its michael@0: * contributors may be used to endorse or promote products derived michael@0: * from this software without specific prior written permission. michael@0: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED michael@0: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. michael@0: */ michael@0: michael@0: #if defined(LIBC_SCCS) && !defined(lint) michael@0: static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 6/1/90"; michael@0: #endif /* LIBC_SCCS and not lint */ michael@0: michael@0: #include michael@0: #include michael@0: #define index strchr michael@0: #define rindex strrchr michael@0: michael@0: /* michael@0: * get option letter from argument vector michael@0: */ michael@0: int opterr = 1, /* if error message should be printed */ michael@0: optind = 1, /* index into parent argv vector */ michael@0: optopt; /* character checked for validity */ michael@0: char *optarg; /* argument associated with option */ michael@0: michael@0: #define BADCH (int)'?' michael@0: #define EMSG "" michael@0: michael@0: int getopt(int nargc, char **nargv, char *ostr) michael@0: { michael@0: static char *place = EMSG; /* option letter processing */ michael@0: register char *oli; /* option letter list index */ michael@0: char *p; michael@0: michael@0: if (!*place) { /* update scanning pointer */ michael@0: if (optind >= nargc || *(place = nargv[optind]) != '-') { michael@0: place = EMSG; michael@0: return(EOF); michael@0: } michael@0: if (place[1] && *++place == '-') { /* found "--" */ michael@0: ++optind; michael@0: place = EMSG; michael@0: return(EOF); michael@0: } michael@0: } /* option letter okay? */ michael@0: if ((optopt = (int)*place++) == (int)':' || michael@0: !(oli = index(ostr, optopt))) { michael@0: /* michael@0: * if the user didn't specify '-' as an option, michael@0: * assume it means EOF. michael@0: */ michael@0: if (optopt == (int)'-') michael@0: return(EOF); michael@0: if (!*place) michael@0: ++optind; michael@0: if (opterr) { michael@0: if (!(p = rindex(*nargv, '/'))) michael@0: p = *nargv; michael@0: else michael@0: ++p; michael@0: (void)fprintf(stderr, "%s: illegal option -- %c\n", michael@0: p, optopt); michael@0: } michael@0: return(BADCH); michael@0: } michael@0: if (*++oli != ':') { /* don't need argument */ michael@0: optarg = NULL; michael@0: if (!*place) michael@0: ++optind; michael@0: } michael@0: else { /* need an argument */ michael@0: if (*place) /* no white space */ michael@0: optarg = place; michael@0: else if (nargc <= ++optind) { /* no arg */ michael@0: place = EMSG; michael@0: if (!(p = rindex(*nargv, '/'))) michael@0: p = *nargv; michael@0: else michael@0: ++p; michael@0: if (opterr) michael@0: (void)fprintf(stderr, michael@0: "%s: option requires an argument -- %c\n", michael@0: p, optopt); michael@0: return(BADCH); michael@0: } michael@0: else /* white space */ michael@0: optarg = nargv[optind]; michael@0: place = EMSG; michael@0: ++optind; michael@0: } michael@0: return(optopt); /* dump back option letter */ michael@0: }