|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1987 Regents of the University of California. ! 3: * All rights reserved. ! 4: * ! 5: * Redistribution and use in source and binary forms are permitted ! 6: * provided that the above copyright notice and this paragraph are ! 7: * duplicated in all such forms and that any documentation, ! 8: * advertising materials, and other materials related to such ! 9: * distribution and use acknowledge that the software was developed ! 10: * by the University of California, Berkeley. The name of the ! 11: * University may not be used to endorse or promote products derived ! 12: * from this software without specific prior written permission. ! 13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 16: */ ! 17: ! 18: #if defined(LIBC_SCCS) && !defined(lint) ! 19: static char sccsid[] = "@(#)getopt.c 4.7 (Berkeley) 6/27/88"; ! 20: #endif /* LIBC_SCCS and not lint */ ! 21: ! 22: #include <stdio.h> ! 23: ! 24: /* ! 25: * get option letter from argument vector ! 26: */ ! 27: int opterr = 1, /* if error message should be printed */ ! 28: optind = 1, /* index into parent argv vector */ ! 29: optopt; /* character checked for validity */ ! 30: char *optarg; /* argument associated with option */ ! 31: ! 32: #define BADCH (int)'?' ! 33: #define EMSG "" ! 34: #define tell(s) { \ ! 35: if (opterr) { \ ! 36: fputs(*nargv, stderr); \ ! 37: fputs(s, stderr); \ ! 38: fputc(optopt, stderr); \ ! 39: fputc((int)'\n', stderr); \ ! 40: } \ ! 41: return(BADCH); \ ! 42: } ! 43: ! 44: getopt(nargc, nargv, ostr) ! 45: int nargc; ! 46: char **nargv, *ostr; ! 47: { ! 48: static char *place = EMSG; /* option letter processing */ ! 49: register char *oli; /* option letter list index */ ! 50: char *index(); ! 51: ! 52: if (!*place) { /* update scanning pointer */ ! 53: if (optind >= nargc || *(place = nargv[optind]) != '-') ! 54: return(EOF); ! 55: if (place[1] && *++place == '-') { /* found "--" */ ! 56: ++optind; ! 57: return(EOF); ! 58: } ! 59: } /* option letter okay? */ ! 60: if ((optopt = (int)*place++) == (int)':' || ! 61: !(oli = index(ostr, optopt))) { ! 62: if (!*place) ! 63: ++optind; ! 64: tell(": illegal option -- "); ! 65: } ! 66: if (*++oli != ':') { /* don't need argument */ ! 67: optarg = NULL; ! 68: if (!*place) ! 69: ++optind; ! 70: } ! 71: else { /* need an argument */ ! 72: if (*place) /* no white space */ ! 73: optarg = place; ! 74: else if (nargc <= ++optind) { /* no arg */ ! 75: place = EMSG; ! 76: tell(": option requires an argument -- "); ! 77: } ! 78: else /* white space */ ! 79: optarg = nargv[optind]; ! 80: place = EMSG; ! 81: ++optind; ! 82: } ! 83: return(optopt); /* dump back option letter */ ! 84: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.