121397Smckusick /* 232677Sbostic * Copyright (c) 1987 Regents of the University of California. 332677Sbostic * All rights reserved. 432677Sbostic * 532677Sbostic * Redistribution and use in source and binary forms are permitted 634831Sbostic * provided that the above copyright notice and this paragraph are 734831Sbostic * duplicated in all such forms and that any documentation, 834831Sbostic * advertising materials, and other materials related to such 934831Sbostic * distribution and use acknowledge that the software was developed 1034831Sbostic * by the University of California, Berkeley. The name of the 1134831Sbostic * University may not be used to endorse or promote products derived 1234831Sbostic * from this software without specific prior written permission. 1334831Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434831Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534831Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621397Smckusick */ 1721397Smckusick 1826524Sdonn #if defined(LIBC_SCCS) && !defined(lint) 19*40166Sbostic static char sccsid[] = "@(#)getopt.c 4.11 (Berkeley) 02/20/90"; 2034017Sbostic #endif /* LIBC_SCCS and not lint */ 2121397Smckusick 2221397Smckusick #include <stdio.h> 2321397Smckusick 2421397Smckusick /* 2521397Smckusick * get option letter from argument vector 2621397Smckusick */ 2725264Smckusick int opterr = 1, /* if error message should be printed */ 2821397Smckusick optind = 1, /* index into parent argv vector */ 2921397Smckusick optopt; /* character checked for validity */ 3021397Smckusick char *optarg; /* argument associated with option */ 3121397Smckusick 3234017Sbostic #define BADCH (int)'?' 3334017Sbostic #define EMSG "" 3421397Smckusick 3532111Sbostic getopt(nargc, nargv, ostr) 3634017Sbostic int nargc; 3734017Sbostic char **nargv, *ostr; 3821397Smckusick { 3934017Sbostic static char *place = EMSG; /* option letter processing */ 4034017Sbostic register char *oli; /* option letter list index */ 4138284Sbostic char *p, *index(), *rindex(); 4221397Smckusick 4332111Sbostic if (!*place) { /* update scanning pointer */ 44*40166Sbostic if (optind >= nargc || *(place = nargv[optind]) != '-') { 45*40166Sbostic place = EMSG; 4632111Sbostic return(EOF); 47*40166Sbostic } 4834017Sbostic if (place[1] && *++place == '-') { /* found "--" */ 4921397Smckusick ++optind; 50*40166Sbostic place = EMSG; 5121397Smckusick return(EOF); 5221397Smckusick } 5332111Sbostic } /* option letter okay? */ 5432111Sbostic if ((optopt = (int)*place++) == (int)':' || 5532111Sbostic !(oli = index(ostr, optopt))) { 5639712Sbostic /* 5739712Sbostic * if the user didn't specify '-' as an option, 5839712Sbostic * assume it means EOF. 5939712Sbostic */ 6039712Sbostic if (optopt == (int)'-') 6139712Sbostic return(EOF); 6232111Sbostic if (!*place) 6332111Sbostic ++optind; 6438284Sbostic if (opterr) { 6538284Sbostic if (!(p = rindex(*nargv, '/'))) 6638284Sbostic p = *nargv; 6738284Sbostic else 6838284Sbostic ++p; 6936960Sbostic (void)fprintf(stderr, "%s: illegal option -- %c\n", 7038284Sbostic p, optopt); 7138284Sbostic } 7236960Sbostic return(BADCH); 7321397Smckusick } 7432111Sbostic if (*++oli != ':') { /* don't need argument */ 7521397Smckusick optarg = NULL; 7632111Sbostic if (!*place) 7732111Sbostic ++optind; 7821397Smckusick } 7932111Sbostic else { /* need an argument */ 8032111Sbostic if (*place) /* no white space */ 8132111Sbostic optarg = place; 8221397Smckusick else if (nargc <= ++optind) { /* no arg */ 8321397Smckusick place = EMSG; 8438284Sbostic if (!(p = rindex(*nargv, '/'))) 8538284Sbostic p = *nargv; 8638284Sbostic else 8738284Sbostic ++p; 8836960Sbostic if (opterr) 8936960Sbostic (void)fprintf(stderr, 9036960Sbostic "%s: option requires an argument -- %c\n", 9138284Sbostic p, optopt); 9236960Sbostic return(BADCH); 9321397Smckusick } 9432111Sbostic else /* white space */ 9532111Sbostic optarg = nargv[optind]; 9621397Smckusick place = EMSG; 9721397Smckusick ++optind; 9821397Smckusick } 9932111Sbostic return(optopt); /* dump back option letter */ 10021397Smckusick } 101