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*36960Sbostic static char sccsid[] = "@(#)getopt.c 4.8 (Berkeley) 03/05/89"; 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 */ 4134017Sbostic char *index(); 4221397Smckusick 4332111Sbostic if (!*place) { /* update scanning pointer */ 4434017Sbostic if (optind >= nargc || *(place = nargv[optind]) != '-') 4532111Sbostic return(EOF); 4634017Sbostic if (place[1] && *++place == '-') { /* found "--" */ 4721397Smckusick ++optind; 4821397Smckusick return(EOF); 4921397Smckusick } 5032111Sbostic } /* option letter okay? */ 5132111Sbostic if ((optopt = (int)*place++) == (int)':' || 5232111Sbostic !(oli = index(ostr, optopt))) { 5332111Sbostic if (!*place) 5432111Sbostic ++optind; 55*36960Sbostic if (opterr) 56*36960Sbostic (void)fprintf(stderr, "%s: illegal option -- %c\n", 57*36960Sbostic *nargv, optopt); 58*36960Sbostic return(BADCH); 5921397Smckusick } 6032111Sbostic if (*++oli != ':') { /* don't need argument */ 6121397Smckusick optarg = NULL; 6232111Sbostic if (!*place) 6332111Sbostic ++optind; 6421397Smckusick } 6532111Sbostic else { /* need an argument */ 6632111Sbostic if (*place) /* no white space */ 6732111Sbostic optarg = place; 6821397Smckusick else if (nargc <= ++optind) { /* no arg */ 6921397Smckusick place = EMSG; 70*36960Sbostic if (opterr) 71*36960Sbostic (void)fprintf(stderr, 72*36960Sbostic "%s: option requires an argument -- %c\n", 73*36960Sbostic *nargv, optopt); 74*36960Sbostic return(BADCH); 7521397Smckusick } 7632111Sbostic else /* white space */ 7732111Sbostic optarg = nargv[optind]; 7821397Smckusick place = EMSG; 7921397Smckusick ++optind; 8021397Smckusick } 8132111Sbostic return(optopt); /* dump back option letter */ 8221397Smckusick } 83