options.c revision 221011
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1991, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * This code is derived from software contributed to Berkeley by 61556Srgrimes * Kenneth Almquist. 71556Srgrimes * 81556Srgrimes * Redistribution and use in source and binary forms, with or without 91556Srgrimes * modification, are permitted provided that the following conditions 101556Srgrimes * are met: 111556Srgrimes * 1. Redistributions of source code must retain the above copyright 121556Srgrimes * notice, this list of conditions and the following disclaimer. 131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141556Srgrimes * notice, this list of conditions and the following disclaimer in the 151556Srgrimes * documentation and/or other materials provided with the distribution. 161556Srgrimes * 4. Neither the name of the University nor the names of its contributors 171556Srgrimes * may be used to endorse or promote products derived from this software 181556Srgrimes * without specific prior written permission. 191556Srgrimes * 201556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301556Srgrimes * SUCH DAMAGE. 311556Srgrimes */ 321556Srgrimes 331556Srgrimes#ifndef lint 3436150Scharnier#if 0 3536150Scharnierstatic char sccsid[] = "@(#)options.c 8.2 (Berkeley) 5/4/95"; 3636150Scharnier#endif 371556Srgrimes#endif /* not lint */ 3899110Sobrien#include <sys/cdefs.h> 3999110Sobrien__FBSDID("$FreeBSD: head/bin/sh/options.c 221011 2011-04-25 10:08:34Z jilles $"); 401556Srgrimes 4117987Speter#include <signal.h> 4217987Speter#include <unistd.h> 4317987Speter#include <stdlib.h> 4417987Speter 451556Srgrimes#include "shell.h" 461556Srgrimes#define DEFINE_OPTIONS 471556Srgrimes#include "options.h" 481556Srgrimes#undef DEFINE_OPTIONS 491556Srgrimes#include "nodes.h" /* for other header files */ 501556Srgrimes#include "eval.h" 511556Srgrimes#include "jobs.h" 521556Srgrimes#include "input.h" 531556Srgrimes#include "output.h" 541556Srgrimes#include "trap.h" 551556Srgrimes#include "var.h" 561556Srgrimes#include "memalloc.h" 571556Srgrimes#include "error.h" 581556Srgrimes#include "mystring.h" 5917987Speter#ifndef NO_HISTORY 6017987Speter#include "myhistedit.h" 6117987Speter#endif 621556Srgrimes 631556Srgrimeschar *arg0; /* value of $0 */ 641556Srgrimesstruct shparam shellparam; /* current positional parameters */ 651556Srgrimeschar **argptr; /* argument list for builtin commands */ 6659436Scracauerchar *shoptarg; /* set by nextopt (like getopt) */ 67201053Sjilleschar *nextopt_optptr; /* used by nextopt */ 681556Srgrimes 691556Srgrimeschar *minusc; /* argument to -c option */ 701556Srgrimes 711556Srgrimes 72213811Sobrienstatic void options(int); 73213811Sobrienstatic void minus_o(char *, int); 74213811Sobrienstatic void setoption(int, int); 75213811Sobrienstatic int getopts(char *, char *, char **, char ***, char **); 761556Srgrimes 771556Srgrimes 781556Srgrimes/* 791556Srgrimes * Process the shell command line arguments. 801556Srgrimes */ 811556Srgrimes 821556Srgrimesvoid 8390111Simpprocargs(int argc, char **argv) 8417987Speter{ 851556Srgrimes int i; 861556Srgrimes 871556Srgrimes argptr = argv; 881556Srgrimes if (argc > 0) 891556Srgrimes argptr++; 901556Srgrimes for (i = 0; i < NOPTS; i++) 911556Srgrimes optlist[i].val = 2; 9219240Ssteve privileged = (getuid() != geteuid() || getgid() != getegid()); 931556Srgrimes options(1); 941556Srgrimes if (*argptr == NULL && minusc == NULL) 951556Srgrimes sflag = 1; 96206182Sjilles if (iflag != 0 && sflag == 1 && isatty(0) && isatty(1)) { 971556Srgrimes iflag = 1; 98206182Sjilles if (Eflag == 2) 99206182Sjilles Eflag = 1; 100206182Sjilles } 1011556Srgrimes if (mflag == 2) 1021556Srgrimes mflag = iflag; 1031556Srgrimes for (i = 0; i < NOPTS; i++) 1041556Srgrimes if (optlist[i].val == 2) 1051556Srgrimes optlist[i].val = 0; 1061556Srgrimes arg0 = argv[0]; 1071556Srgrimes if (sflag == 0 && minusc == NULL) { 1081556Srgrimes commandname = arg0 = *argptr++; 1091556Srgrimes setinputfile(commandname, 0); 1101556Srgrimes } 11120425Ssteve /* POSIX 1003.2: first arg after -c cmd is $0, remainder $1... */ 11225227Ssteve if (argptr && minusc && *argptr) 11311111Sjoerg arg0 = *argptr++; 11420742Ssteve 1151556Srgrimes shellparam.p = argptr; 11620742Ssteve shellparam.reset = 1; 1171556Srgrimes /* assert(shellparam.malloc == 0 && shellparam.nparam == 0); */ 1181556Srgrimes while (*argptr) { 1191556Srgrimes shellparam.nparam++; 1201556Srgrimes argptr++; 1211556Srgrimes } 1221556Srgrimes optschanged(); 1231556Srgrimes} 1241556Srgrimes 1251556Srgrimes 12617987Spetervoid 12790111Simpoptschanged(void) 12817987Speter{ 1291556Srgrimes setinteractive(iflag); 13017987Speter#ifndef NO_HISTORY 1311556Srgrimes histedit(); 13217987Speter#endif 1331556Srgrimes setjobctl(mflag); 1341556Srgrimes} 1351556Srgrimes 1361556Srgrimes/* 1371556Srgrimes * Process shell options. The global variable argptr contains a pointer 1381556Srgrimes * to the argument list; we advance it past the options. 1391556Srgrimes */ 1401556Srgrimes 141213811Sobrienstatic void 14290111Simpoptions(int cmdline) 14317987Speter{ 144146255Sgad char *kp, *p; 1451556Srgrimes int val; 1461556Srgrimes int c; 1471556Srgrimes 1481556Srgrimes if (cmdline) 1491556Srgrimes minusc = NULL; 1501556Srgrimes while ((p = *argptr) != NULL) { 1511556Srgrimes argptr++; 1521556Srgrimes if ((c = *p++) == '-') { 1531556Srgrimes val = 1; 154141962Sgad /* A "-" or "--" terminates options */ 155141962Sgad if (p[0] == '\0') 156141962Sgad goto end_options1; 157141962Sgad if (p[0] == '-' && p[1] == '\0') 158141962Sgad goto end_options2; 159146255Sgad /** 160146255Sgad * For the benefit of `#!' lines in shell scripts, 161146255Sgad * treat a string of '-- *#.*' the same as '--'. 162146255Sgad * This is needed so that a script starting with: 163146255Sgad * #!/bin/sh -- # -*- perl -*- 164146255Sgad * will continue to work after a change is made to 165146255Sgad * kern/imgact_shell.c to NOT token-ize the options 166146255Sgad * specified on a '#!' line. A bit of a kludge, 167146255Sgad * but that trick is recommended in documentation 168146255Sgad * for some scripting languages, and we might as 169146255Sgad * well continue to support it. 170146255Sgad */ 171146255Sgad if (p[0] == '-') { 172146255Sgad kp = p + 1; 173146255Sgad while (*kp == ' ' || *kp == '\t') 174146255Sgad kp++; 175146255Sgad if (*kp == '#' || *kp == '\0') 176146255Sgad goto end_options2; 177146255Sgad } 1781556Srgrimes } else if (c == '+') { 1791556Srgrimes val = 0; 1801556Srgrimes } else { 1811556Srgrimes argptr--; 1821556Srgrimes break; 1831556Srgrimes } 1841556Srgrimes while ((c = *p++) != '\0') { 1851556Srgrimes if (c == 'c' && cmdline) { 1861556Srgrimes char *q; 1871556Srgrimes#ifdef NOHACK /* removing this code allows sh -ce 'foo' for compat */ 1881556Srgrimes if (*p == '\0') 1891556Srgrimes#endif 1901556Srgrimes q = *argptr++; 1911556Srgrimes if (q == NULL || minusc != NULL) 1921556Srgrimes error("Bad -c option"); 1931556Srgrimes minusc = q; 1941556Srgrimes#ifdef NOHACK 1951556Srgrimes break; 1961556Srgrimes#endif 1971556Srgrimes } else if (c == 'o') { 1981556Srgrimes minus_o(*argptr, val); 1991556Srgrimes if (*argptr) 2001556Srgrimes argptr++; 201221011Sjilles } else 2021556Srgrimes setoption(c, val); 2031556Srgrimes } 2041556Srgrimes } 205141962Sgad return; 206141962Sgad 207141962Sgad /* When processing `set', a single "-" means turn off -x and -v */ 208141962Sgadend_options1: 209141962Sgad if (!cmdline) { 210141962Sgad xflag = vflag = 0; 211141962Sgad return; 212141962Sgad } 213141962Sgad 214141962Sgad /* 215141962Sgad * When processing `set', a "--" means the remaining arguments 216141962Sgad * replace the positional parameters in the active shell. If 217141962Sgad * there are no remaining options, then all the positional 218141962Sgad * parameters are cleared (equivalent to doing ``shift $#''). 219141962Sgad */ 220141962Sgadend_options2: 221141962Sgad if (!cmdline) { 222141962Sgad if (*argptr == NULL) 223141962Sgad setparam(argptr); 224141962Sgad return; 225141962Sgad } 226141962Sgad 227141962Sgad /* 228141962Sgad * At this point we are processing options given to 'sh' on a command 229141962Sgad * line. If an end-of-options marker ("-" or "--") is followed by an 230141962Sgad * arg of "#", then skip over all remaining arguments. Some scripting 231141962Sgad * languages (e.g.: perl) document that /bin/sh will implement this 232141962Sgad * behavior, and they recommend that users take advantage of it to 233141962Sgad * solve certain issues that can come up when writing a perl script. 234141962Sgad * Yes, this feature is in /bin/sh to help users write perl scripts. 235141962Sgad */ 236141962Sgad p = *argptr; 237141962Sgad if (p != NULL && p[0] == '#' && p[1] == '\0') { 238141962Sgad while (*argptr != NULL) 239141962Sgad argptr++; 240141962Sgad /* We need to keep the final argument */ 241141962Sgad argptr--; 242141962Sgad } 2431556Srgrimes} 2441556Srgrimes 245213811Sobrienstatic void 24690111Simpminus_o(char *name, int val) 2471556Srgrimes{ 248151866Sstefanf int i; 2491556Srgrimes 2501556Srgrimes if (name == NULL) { 25197276Stjr if (val) { 25297276Stjr /* "Pretty" output. */ 25397276Stjr out1str("Current option settings\n"); 25497276Stjr for (i = 0; i < NOPTS; i++) 25597276Stjr out1fmt("%-16s%s\n", optlist[i].name, 25697276Stjr optlist[i].val ? "on" : "off"); 25797276Stjr } else { 25897276Stjr /* Output suitable for re-input to shell. */ 259215567Sjilles for (i = 0; i < NOPTS; i++) 260215567Sjilles out1fmt("%s %co %s%s", 261215567Sjilles i % 6 == 0 ? "set" : "", 262215567Sjilles optlist[i].val ? '-' : '+', 263215567Sjilles optlist[i].name, 264215567Sjilles i % 6 == 5 || i == NOPTS - 1 ? "\n" : ""); 26597276Stjr } 2661556Srgrimes } else { 2671556Srgrimes for (i = 0; i < NOPTS; i++) 2681556Srgrimes if (equal(name, optlist[i].name)) { 2691556Srgrimes setoption(optlist[i].letter, val); 2701556Srgrimes return; 2711556Srgrimes } 2721556Srgrimes error("Illegal option -o %s", name); 2731556Srgrimes } 2741556Srgrimes} 2751556Srgrimes 2768855Srgrimes 277213811Sobrienstatic void 27890111Simpsetoption(int flag, int val) 27990111Simp{ 2801556Srgrimes int i; 2811556Srgrimes 282221011Sjilles if (flag == 'p' && !val && privileged) { 283221011Sjilles (void) setuid(getuid()); 284221011Sjilles (void) setgid(getgid()); 285221011Sjilles } 2861556Srgrimes for (i = 0; i < NOPTS; i++) 2871556Srgrimes if (optlist[i].letter == flag) { 2881556Srgrimes optlist[i].val = val; 2891556Srgrimes if (val) { 2901556Srgrimes /* #%$ hack for ksh semantics */ 2911556Srgrimes if (flag == 'V') 2921556Srgrimes Eflag = 0; 2931556Srgrimes else if (flag == 'E') 2941556Srgrimes Vflag = 0; 2951556Srgrimes } 2961556Srgrimes return; 2971556Srgrimes } 2981556Srgrimes error("Illegal option -%c", flag); 2991556Srgrimes} 3001556Srgrimes 3011556Srgrimes 3021556Srgrimes/* 3031556Srgrimes * Set the shell parameters. 3041556Srgrimes */ 3051556Srgrimes 3061556Srgrimesvoid 30790111Simpsetparam(char **argv) 30890111Simp{ 3091556Srgrimes char **newparam; 3101556Srgrimes char **ap; 3111556Srgrimes int nparam; 3121556Srgrimes 3131556Srgrimes for (nparam = 0 ; argv[nparam] ; nparam++); 3141556Srgrimes ap = newparam = ckmalloc((nparam + 1) * sizeof *ap); 3151556Srgrimes while (*argv) { 3161556Srgrimes *ap++ = savestr(*argv++); 3171556Srgrimes } 3181556Srgrimes *ap = NULL; 3191556Srgrimes freeparam(&shellparam); 3201556Srgrimes shellparam.malloc = 1; 3211556Srgrimes shellparam.nparam = nparam; 3221556Srgrimes shellparam.p = newparam; 323182300Sstefanf shellparam.reset = 1; 3241556Srgrimes shellparam.optnext = NULL; 3251556Srgrimes} 3261556Srgrimes 3271556Srgrimes 3281556Srgrimes/* 3291556Srgrimes * Free the list of positional parameters. 3301556Srgrimes */ 3311556Srgrimes 3321556Srgrimesvoid 33390111Simpfreeparam(struct shparam *param) 33490111Simp{ 3351556Srgrimes char **ap; 3361556Srgrimes 3371556Srgrimes if (param->malloc) { 3381556Srgrimes for (ap = param->p ; *ap ; ap++) 3391556Srgrimes ckfree(*ap); 3401556Srgrimes ckfree(param->p); 3411556Srgrimes } 3421556Srgrimes} 3431556Srgrimes 3441556Srgrimes 3451556Srgrimes 3461556Srgrimes/* 3471556Srgrimes * The shift builtin command. 3481556Srgrimes */ 3491556Srgrimes 35017987Speterint 35190111Simpshiftcmd(int argc, char **argv) 35217987Speter{ 3531556Srgrimes int n; 3541556Srgrimes char **ap1, **ap2; 3551556Srgrimes 3561556Srgrimes n = 1; 3571556Srgrimes if (argc > 1) 3581556Srgrimes n = number(argv[1]); 3591556Srgrimes if (n > shellparam.nparam) 360157601Sstefanf return 1; 3611556Srgrimes INTOFF; 3621556Srgrimes shellparam.nparam -= n; 3631556Srgrimes for (ap1 = shellparam.p ; --n >= 0 ; ap1++) { 3641556Srgrimes if (shellparam.malloc) 3651556Srgrimes ckfree(*ap1); 3661556Srgrimes } 3671556Srgrimes ap2 = shellparam.p; 3681556Srgrimes while ((*ap2++ = *ap1++) != NULL); 369177497Sstefanf shellparam.reset = 1; 3701556Srgrimes INTON; 3711556Srgrimes return 0; 3721556Srgrimes} 3731556Srgrimes 3741556Srgrimes 3751556Srgrimes 3761556Srgrimes/* 3771556Srgrimes * The set command builtin. 3781556Srgrimes */ 3791556Srgrimes 38017987Speterint 38190111Simpsetcmd(int argc, char **argv) 38217987Speter{ 3831556Srgrimes if (argc == 1) 3841556Srgrimes return showvarscmd(argc, argv); 3851556Srgrimes INTOFF; 3861556Srgrimes options(0); 3871556Srgrimes optschanged(); 3881556Srgrimes if (*argptr != NULL) { 3891556Srgrimes setparam(argptr); 3901556Srgrimes } 3911556Srgrimes INTON; 3921556Srgrimes return 0; 3931556Srgrimes} 3941556Srgrimes 3951556Srgrimes 39620425Sstevevoid 39790111Simpgetoptsreset(const char *value) 39820425Ssteve{ 39920425Ssteve if (number(value) == 1) { 40020425Ssteve shellparam.reset = 1; 40120425Ssteve } 40220425Ssteve} 40320425Ssteve 4041556Srgrimes/* 4051556Srgrimes * The getopts builtin. Shellparam.optnext points to the next argument 4061556Srgrimes * to be processed. Shellparam.optptr points to the next character to 4071556Srgrimes * be processed in the current argument. If shellparam.optnext is NULL, 4081556Srgrimes * then it's the first time getopts has been called. 4091556Srgrimes */ 4101556Srgrimes 41117987Speterint 41290111Simpgetoptscmd(int argc, char **argv) 41317987Speter{ 41420425Ssteve char **optbase = NULL; 41520425Ssteve 41620425Ssteve if (argc < 3) 41795258Sdes error("usage: getopts optstring var [arg]"); 41820425Ssteve else if (argc == 3) 41920425Ssteve optbase = shellparam.p; 42020425Ssteve else 42120425Ssteve optbase = &argv[3]; 42220425Ssteve 42320425Ssteve if (shellparam.reset == 1) { 42420425Ssteve shellparam.optnext = optbase; 42520425Ssteve shellparam.optptr = NULL; 42620425Ssteve shellparam.reset = 0; 42720425Ssteve } 42820425Ssteve 42920425Ssteve return getopts(argv[1], argv[2], optbase, &shellparam.optnext, 43020425Ssteve &shellparam.optptr); 43120425Ssteve} 43220425Ssteve 433213811Sobrienstatic int 43490111Simpgetopts(char *optstr, char *optvar, char **optfirst, char ***optnext, 43590111Simp char **optptr) 43620425Ssteve{ 43725227Ssteve char *p, *q; 43820425Ssteve char c = '?'; 43920425Ssteve int done = 0; 44020425Ssteve int ind = 0; 44120425Ssteve int err = 0; 4421556Srgrimes char s[10]; 4431556Srgrimes 44420425Ssteve if ((p = *optptr) == NULL || *p == '\0') { 44520425Ssteve /* Current word is done, advance */ 44620425Ssteve if (*optnext == NULL) 44720425Ssteve return 1; 44820425Ssteve p = **optnext; 4491556Srgrimes if (p == NULL || *p != '-' || *++p == '\0') { 4501556Srgrimesatend: 45120742Ssteve ind = *optnext - optfirst + 1; 45220425Ssteve *optnext = NULL; 45320742Ssteve p = NULL; 45420425Ssteve done = 1; 45520425Ssteve goto out; 4561556Srgrimes } 45720425Ssteve (*optnext)++; 4581556Srgrimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 4591556Srgrimes goto atend; 4601556Srgrimes } 46120425Ssteve 4621556Srgrimes c = *p++; 46320425Ssteve for (q = optstr; *q != c; ) { 4641556Srgrimes if (*q == '\0') { 46520425Ssteve if (optstr[0] == ':') { 46620425Ssteve s[0] = c; 46720425Ssteve s[1] = '\0'; 46820425Ssteve err |= setvarsafe("OPTARG", s, 0); 46920425Ssteve } 47020425Ssteve else { 47120425Ssteve out1fmt("Illegal option -%c\n", c); 47220425Ssteve (void) unsetvar("OPTARG"); 47320425Ssteve } 4741556Srgrimes c = '?'; 47520425Ssteve goto bad; 4761556Srgrimes } 4771556Srgrimes if (*++q == ':') 4781556Srgrimes q++; 4791556Srgrimes } 48020425Ssteve 4811556Srgrimes if (*++q == ':') { 48220425Ssteve if (*p == '\0' && (p = **optnext) == NULL) { 48320425Ssteve if (optstr[0] == ':') { 48420425Ssteve s[0] = c; 48520425Ssteve s[1] = '\0'; 48620425Ssteve err |= setvarsafe("OPTARG", s, 0); 48720425Ssteve c = ':'; 48820425Ssteve } 48920425Ssteve else { 49020425Ssteve out1fmt("No arg for -%c option\n", c); 49120425Ssteve (void) unsetvar("OPTARG"); 49220425Ssteve c = '?'; 49320425Ssteve } 49420425Ssteve goto bad; 4951556Srgrimes } 49620425Ssteve 49720425Ssteve if (p == **optnext) 49820425Ssteve (*optnext)++; 49920425Ssteve setvarsafe("OPTARG", p, 0); 5001556Srgrimes p = NULL; 5011556Srgrimes } 50220425Ssteve else 50320425Ssteve setvarsafe("OPTARG", "", 0); 50420425Ssteve ind = *optnext - optfirst + 1; 50520425Ssteve goto out; 50620425Ssteve 50720425Sstevebad: 50820425Ssteve ind = 1; 50920425Ssteve *optnext = NULL; 51020425Ssteve p = NULL; 5111556Srgrimesout: 51220425Ssteve *optptr = p; 51320425Ssteve fmtstr(s, sizeof(s), "%d", ind); 51420425Ssteve err |= setvarsafe("OPTIND", s, VNOFUNC); 5151556Srgrimes s[0] = c; 5161556Srgrimes s[1] = '\0'; 51720425Ssteve err |= setvarsafe(optvar, s, 0); 51820425Ssteve if (err) { 51920425Ssteve *optnext = NULL; 52020425Ssteve *optptr = NULL; 52120425Ssteve flushall(); 52220425Ssteve exraise(EXERROR); 52320425Ssteve } 52420425Ssteve return done; 5251556Srgrimes} 5261556Srgrimes 5271556Srgrimes/* 5281556Srgrimes * XXX - should get rid of. have all builtins use getopt(3). the 5291556Srgrimes * library getopt must have the BSD extension static variable "optreset" 5301556Srgrimes * otherwise it can't be used within the shell safely. 5311556Srgrimes * 5321556Srgrimes * Standard option processing (a la getopt) for builtin routines. The 5331556Srgrimes * only argument that is passed to nextopt is the option string; the 5341556Srgrimes * other arguments are unnecessary. It return the character, or '\0' on 5351556Srgrimes * end of input. 5361556Srgrimes */ 5371556Srgrimes 5381556Srgrimesint 539200956Sjillesnextopt(const char *optstring) 54090111Simp{ 541200956Sjilles char *p; 542200956Sjilles const char *q; 5431556Srgrimes char c; 5441556Srgrimes 545201053Sjilles if ((p = nextopt_optptr) == NULL || *p == '\0') { 5461556Srgrimes p = *argptr; 5471556Srgrimes if (p == NULL || *p != '-' || *++p == '\0') 5481556Srgrimes return '\0'; 5491556Srgrimes argptr++; 5501556Srgrimes if (p[0] == '-' && p[1] == '\0') /* check for "--" */ 5511556Srgrimes return '\0'; 5521556Srgrimes } 5531556Srgrimes c = *p++; 5541556Srgrimes for (q = optstring ; *q != c ; ) { 5551556Srgrimes if (*q == '\0') 5561556Srgrimes error("Illegal option -%c", c); 5571556Srgrimes if (*++q == ':') 5581556Srgrimes q++; 5591556Srgrimes } 5601556Srgrimes if (*++q == ':') { 5611556Srgrimes if (*p == '\0' && (p = *argptr++) == NULL) 5621556Srgrimes error("No arg for -%c option", c); 56359436Scracauer shoptarg = p; 5641556Srgrimes p = NULL; 5651556Srgrimes } 566201053Sjilles nextopt_optptr = p; 5671556Srgrimes return c; 5681556Srgrimes} 569