121397Smckusick /* 232677Sbostic * Copyright (c) 1987 Regents of the University of California. 332677Sbostic * All rights reserved. 432677Sbostic * 542635Sbostic * %sccs.include.redist.c% 621397Smckusick */ 721397Smckusick 826524Sdonn #if defined(LIBC_SCCS) && !defined(lint) 9*54707Smarc static char sccsid[] = "@(#)getopt.c 5.1 (Berkeley) 07/06/92"; 1034017Sbostic #endif /* LIBC_SCCS and not lint */ 1121397Smckusick 1221397Smckusick #include <stdio.h> 1346574Sdonn #include <stdlib.h> 1446574Sdonn #include <string.h> 1521397Smckusick 1621397Smckusick /* 1721397Smckusick * get option letter from argument vector 1821397Smckusick */ 1925264Smckusick int opterr = 1, /* if error message should be printed */ 2021397Smckusick optind = 1, /* index into parent argv vector */ 21*54707Smarc optopt, /* character checked for validity */ 22*54707Smarc optreset; /* reset getopt */ 2321397Smckusick char *optarg; /* argument associated with option */ 2421397Smckusick 2534017Sbostic #define BADCH (int)'?' 26*54707Smarc #define BADARG (int)':' 2734017Sbostic #define EMSG "" 2821397Smckusick 2946574Sdonn int 3032111Sbostic getopt(nargc, nargv, ostr) 3134017Sbostic int nargc; 3246574Sdonn char * const *nargv; 3346574Sdonn const char *ostr; 3421397Smckusick { 3534017Sbostic static char *place = EMSG; /* option letter processing */ 3634017Sbostic register char *oli; /* option letter list index */ 3746574Sdonn char *p; 3821397Smckusick 39*54707Smarc if (optreset || !*place) { /* update scanning pointer */ 40*54707Smarc optreset = 0; 4140166Sbostic if (optind >= nargc || *(place = nargv[optind]) != '-') { 4240166Sbostic place = EMSG; 4332111Sbostic return(EOF); 4440166Sbostic } 4534017Sbostic if (place[1] && *++place == '-') { /* found "--" */ 4621397Smckusick ++optind; 4740166Sbostic place = EMSG; 4821397Smckusick return(EOF); 4921397Smckusick } 5032111Sbostic } /* option letter okay? */ 5132111Sbostic if ((optopt = (int)*place++) == (int)':' || 5232111Sbostic !(oli = index(ostr, optopt))) { 5339712Sbostic /* 5439712Sbostic * if the user didn't specify '-' as an option, 5539712Sbostic * assume it means EOF. 5639712Sbostic */ 5739712Sbostic if (optopt == (int)'-') 5839712Sbostic return(EOF); 5932111Sbostic if (!*place) 6032111Sbostic ++optind; 61*54707Smarc if (opterr && *ostr != ':') { 6238284Sbostic if (!(p = rindex(*nargv, '/'))) 6338284Sbostic p = *nargv; 6438284Sbostic else 6538284Sbostic ++p; 6636960Sbostic (void)fprintf(stderr, "%s: illegal option -- %c\n", 6738284Sbostic p, optopt); 6838284Sbostic } 6936960Sbostic return(BADCH); 7021397Smckusick } 7132111Sbostic if (*++oli != ':') { /* don't need argument */ 7221397Smckusick optarg = NULL; 7332111Sbostic if (!*place) 7432111Sbostic ++optind; 7521397Smckusick } 7632111Sbostic else { /* need an argument */ 7732111Sbostic if (*place) /* no white space */ 7832111Sbostic optarg = place; 7921397Smckusick else if (nargc <= ++optind) { /* no arg */ 8021397Smckusick place = EMSG; 8138284Sbostic if (!(p = rindex(*nargv, '/'))) 8238284Sbostic p = *nargv; 8338284Sbostic else 8438284Sbostic ++p; 85*54707Smarc if (*ostr == ':') 86*54707Smarc return(BADARG); 8736960Sbostic if (opterr) 8836960Sbostic (void)fprintf(stderr, 8936960Sbostic "%s: option requires an argument -- %c\n", 9038284Sbostic p, optopt); 9136960Sbostic return(BADCH); 9221397Smckusick } 9332111Sbostic else /* white space */ 9432111Sbostic optarg = nargv[optind]; 9521397Smckusick place = EMSG; 9621397Smckusick ++optind; 9721397Smckusick } 9832111Sbostic return(optopt); /* dump back option letter */ 9921397Smckusick } 100