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*38284Sbostic static char sccsid[] = "@(#)getopt.c 4.9 (Berkeley) 06/23/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 */ 41*38284Sbostic char *p, *index(), *rindex(); 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*38284Sbostic if (opterr) { 56*38284Sbostic if (!(p = rindex(*nargv, '/'))) 57*38284Sbostic p = *nargv; 58*38284Sbostic else 59*38284Sbostic ++p; 6036960Sbostic (void)fprintf(stderr, "%s: illegal option -- %c\n", 61*38284Sbostic p, optopt); 62*38284Sbostic } 6336960Sbostic return(BADCH); 6421397Smckusick } 6532111Sbostic if (*++oli != ':') { /* don't need argument */ 6621397Smckusick optarg = NULL; 6732111Sbostic if (!*place) 6832111Sbostic ++optind; 6921397Smckusick } 7032111Sbostic else { /* need an argument */ 7132111Sbostic if (*place) /* no white space */ 7232111Sbostic optarg = place; 7321397Smckusick else if (nargc <= ++optind) { /* no arg */ 7421397Smckusick place = EMSG; 75*38284Sbostic if (!(p = rindex(*nargv, '/'))) 76*38284Sbostic p = *nargv; 77*38284Sbostic else 78*38284Sbostic ++p; 7936960Sbostic if (opterr) 8036960Sbostic (void)fprintf(stderr, 8136960Sbostic "%s: option requires an argument -- %c\n", 82*38284Sbostic p, optopt); 8336960Sbostic return(BADCH); 8421397Smckusick } 8532111Sbostic else /* white space */ 8632111Sbostic optarg = nargv[optind]; 8721397Smckusick place = EMSG; 8821397Smckusick ++optind; 8921397Smckusick } 9032111Sbostic return(optopt); /* dump back option letter */ 9121397Smckusick } 92