1 #pragma ident "%Z%%M% %I% %E% SMI" 2 /* from 4.3BSD-tahoe 4.9 6/23/89 */ 3 4 /* 5 * Copyright (c) 1989 Sun Microsystems, Inc. 6 */ 7 /* 8 * Copyright (c) 1987 Regents of the University of California. 9 * All rights reserved. 10 * 11 * Redistribution and use in source and binary forms are permitted 12 * provided that the above copyright notice and this paragraph are 13 * duplicated in all such forms and that any documentation, 14 * advertising materials, and other materials related to such 15 * distribution and use acknowledge that the software was developed 16 * by the University of California, Berkeley. The name of the 17 * University may not be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 21 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 /* LINTLIBRARY */ 25 26 #include <stdio.h> 27 #include <string.h> 28 29 /* 30 * get option letter from argument vector 31 */ 32 /* See lib/libc/gen/common/optind.c for next 3 definitions. */ 33 extern char *optarg; /* argument associated with option */ 34 extern int opterr; /* if error message should be printed */ 35 extern int optind; /* index into parent argv vector */ 36 int optopt; /* character checked for validity */ 37 38 39 #define BADCH (int)'?' 40 #define EMSG "" 41 42 getopt(nargc, nargv, ostr) 43 int nargc; 44 char **nargv, *ostr; 45 { 46 static char *place = EMSG; /* option letter processing */ 47 register char *oli; /* option letter list index */ 48 char *p; 49 50 if (!*place) { /* update scanning pointer */ 51 if (optind >= nargc || *(place = nargv[optind]) != '-') { 52 place = EMSG; 53 return (EOF); 54 } 55 if (place[1] && *++place == '-') { /* found "--" */ 56 ++optind; 57 place = EMSG; 58 return (EOF); 59 } 60 } /* option letter okay? */ 61 if ((optopt = (int)*place++) == (int)':' || 62 !(oli = strchr(ostr, optopt))) { 63 64 /* 65 * For backwards compatibility: don't treat '-' as an 66 * option letter unless caller explicitly asked for it. 67 */ 68 if (optopt == (int)'-') 69 return (EOF); 70 if (!*place) 71 ++optind; 72 if (opterr) { 73 if (!(p = strrchr(*nargv, '/'))) 74 p = *nargv; 75 else 76 ++p; 77 (void)fprintf(stderr, "%s: illegal option -- %c\n", 78 p, optopt); 79 } 80 return (BADCH); 81 } 82 if (*++oli != ':') { /* don't need argument */ 83 optarg = NULL; 84 if (!*place) 85 ++optind; 86 } else { /* need an argument */ 87 if (*place) /* no white space */ 88 optarg = place; 89 else if (nargc <= ++optind) { /* no arg */ 90 place = EMSG; 91 if (!(p = strrchr(*nargv, '/'))) 92 p = *nargv; 93 else 94 ++p; 95 if (opterr) 96 (void)fprintf(stderr, 97 "%s: option requires an argument -- %c\n", 98 p, optopt); 99 return (BADCH); 100 } else /* white space */ 101 optarg = nargv[optind]; 102 place = EMSG; 103 ++optind; 104 } 105 return (optopt); /* dump back option letter */ 106 } 107