121397Smckusick /* 2*32677Sbostic * Copyright (c) 1987 Regents of the University of California. 3*32677Sbostic * All rights reserved. 4*32677Sbostic * 5*32677Sbostic * Redistribution and use in source and binary forms are permitted 6*32677Sbostic * provided that this notice is preserved and that due credit is given 7*32677Sbostic * to the University of California at Berkeley. The name of the University 8*32677Sbostic * may not be used to endorse or promote products derived from this 9*32677Sbostic * software without specific written prior permission. This software 10*32677Sbostic * is provided ``as is'' without express or implied warranty. 1121397Smckusick */ 1221397Smckusick 1326524Sdonn #if defined(LIBC_SCCS) && !defined(lint) 14*32677Sbostic static char sccsid[] = "@(#)getopt.c 4.5 (Berkeley) 11/24/87"; 1526524Sdonn #endif LIBC_SCCS and not lint 1621397Smckusick 1721397Smckusick #include <stdio.h> 1821397Smckusick 1921397Smckusick /* 2021397Smckusick * get option letter from argument vector 2121397Smckusick */ 2225264Smckusick int opterr = 1, /* if error message should be printed */ 2321397Smckusick optind = 1, /* index into parent argv vector */ 2421397Smckusick optopt; /* character checked for validity */ 2521397Smckusick char *optarg; /* argument associated with option */ 2621397Smckusick 2721397Smckusick #define BADCH (int)'?' 2821397Smckusick #define EMSG "" 2932111Sbostic #define tell(s) { \ 3032111Sbostic if (opterr) { \ 3132111Sbostic fputs(*nargv, stderr); \ 3232111Sbostic fputs(s, stderr); \ 3332111Sbostic fputc(optopt, stderr); \ 3432111Sbostic fputc((int)'\n', stderr); \ 3532111Sbostic } \ 3632111Sbostic return(BADCH); \ 3732111Sbostic } 3821397Smckusick 3932111Sbostic getopt(nargc, nargv, ostr) 4032111Sbostic int nargc; 4132111Sbostic char **nargv, *ostr; 4221397Smckusick { 4332111Sbostic static char *place = EMSG; /* option letter processing */ 4432111Sbostic register char *oli; /* option letter list index */ 4521397Smckusick char *index(); 4621397Smckusick 4732111Sbostic if (!*place) { /* update scanning pointer */ 4832111Sbostic if (optind >= nargc || *(place = nargv[optind]) != '-' || 4932111Sbostic !*++place) 5032111Sbostic return(EOF); 5132111Sbostic if (*place == '-') { /* found "--" */ 5221397Smckusick ++optind; 5321397Smckusick return(EOF); 5421397Smckusick } 5532111Sbostic } /* option letter okay? */ 5632111Sbostic if ((optopt = (int)*place++) == (int)':' || 5732111Sbostic !(oli = index(ostr, optopt))) { 5832111Sbostic if (!*place) 5932111Sbostic ++optind; 6021397Smckusick tell(": illegal option -- "); 6121397Smckusick } 6232111Sbostic if (*++oli != ':') { /* don't need argument */ 6321397Smckusick optarg = NULL; 6432111Sbostic if (!*place) 6532111Sbostic ++optind; 6621397Smckusick } 6732111Sbostic else { /* need an argument */ 6832111Sbostic if (*place) /* no white space */ 6932111Sbostic optarg = place; 7021397Smckusick else if (nargc <= ++optind) { /* no arg */ 7121397Smckusick place = EMSG; 7221397Smckusick tell(": option requires an argument -- "); 7321397Smckusick } 7432111Sbostic else /* white space */ 7532111Sbostic optarg = nargv[optind]; 7621397Smckusick place = EMSG; 7721397Smckusick ++optind; 7821397Smckusick } 7932111Sbostic return(optopt); /* dump back option letter */ 8021397Smckusick } 81