xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/getopt.c (revision 722:636b850d4ee9)
1*722Smuffin /*
2*722Smuffin  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
3*722Smuffin  * Use is subject to license terms.
4*722Smuffin  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1987 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
110Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
120Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
130Sstevel@tonic-gate  * advertising materials, and other materials related to such
140Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
150Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
160Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
170Sstevel@tonic-gate  * from this software without specific prior written permission.
180Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
190Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
200Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate 
23*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
24*722Smuffin 
25*722Smuffin 
260Sstevel@tonic-gate /* LINTLIBRARY */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /*
320Sstevel@tonic-gate  * get option letter from argument vector
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate /* See lib/libc/gen/common/optind.c for next 3 definitions. */
350Sstevel@tonic-gate extern char	*optarg;	/* argument associated with option */
360Sstevel@tonic-gate extern int	opterr;		/* if error message should be printed */
370Sstevel@tonic-gate extern int	optind;		/* index into parent argv vector */
380Sstevel@tonic-gate int		optopt;		/* character checked for validity */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	BADCH	(int)'?'
420Sstevel@tonic-gate #define	EMSG	""
430Sstevel@tonic-gate 
44*722Smuffin int
getopt(int nargc,char ** nargv,char * ostr)45*722Smuffin getopt(int nargc, char **nargv, char *ostr)
460Sstevel@tonic-gate {
470Sstevel@tonic-gate 	static char *place = EMSG;		/* option letter processing */
48*722Smuffin 	char *oli;			/* option letter list index */
490Sstevel@tonic-gate 	char *p;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	if (!*place) {				/* update scanning pointer */
520Sstevel@tonic-gate 		if (optind >= nargc || *(place = nargv[optind]) != '-') {
530Sstevel@tonic-gate 			place = EMSG;
540Sstevel@tonic-gate 			return (EOF);
550Sstevel@tonic-gate 		}
560Sstevel@tonic-gate 		if (place[1] && *++place == '-') {	/* found "--" */
570Sstevel@tonic-gate 			++optind;
580Sstevel@tonic-gate 			place = EMSG;
590Sstevel@tonic-gate 			return (EOF);
600Sstevel@tonic-gate 		}
610Sstevel@tonic-gate 	}					/* option letter okay? */
620Sstevel@tonic-gate 	if ((optopt = (int)*place++) == (int)':' ||
630Sstevel@tonic-gate 	    !(oli = strchr(ostr, optopt))) {
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 		/*
660Sstevel@tonic-gate 		 * For backwards compatibility: don't treat '-' as an
670Sstevel@tonic-gate 		 * option letter unless caller explicitly asked for it.
680Sstevel@tonic-gate 		 */
690Sstevel@tonic-gate 		if (optopt == (int)'-')
700Sstevel@tonic-gate 			return (EOF);
710Sstevel@tonic-gate 		if (!*place)
720Sstevel@tonic-gate 			++optind;
730Sstevel@tonic-gate 		if (opterr) {
740Sstevel@tonic-gate 			if (!(p = strrchr(*nargv, '/')))
750Sstevel@tonic-gate 				p = *nargv;
760Sstevel@tonic-gate 			else
770Sstevel@tonic-gate 				++p;
780Sstevel@tonic-gate 			(void)fprintf(stderr, "%s: illegal option -- %c\n",
790Sstevel@tonic-gate 			    p, optopt);
800Sstevel@tonic-gate 		}
810Sstevel@tonic-gate 		return (BADCH);
820Sstevel@tonic-gate 	}
830Sstevel@tonic-gate 	if (*++oli != ':') {			/* don't need argument */
840Sstevel@tonic-gate 		optarg = NULL;
850Sstevel@tonic-gate 		if (!*place)
860Sstevel@tonic-gate 			++optind;
870Sstevel@tonic-gate 	} else {				/* need an argument */
880Sstevel@tonic-gate 		if (*place)			/* no white space */
890Sstevel@tonic-gate 			optarg = place;
900Sstevel@tonic-gate 		else if (nargc <= ++optind) {	/* no arg */
910Sstevel@tonic-gate 			place = EMSG;
920Sstevel@tonic-gate 			if (!(p = strrchr(*nargv, '/')))
930Sstevel@tonic-gate 				p = *nargv;
940Sstevel@tonic-gate 			else
950Sstevel@tonic-gate 				++p;
960Sstevel@tonic-gate 			if (opterr)
970Sstevel@tonic-gate 				(void)fprintf(stderr,
980Sstevel@tonic-gate 				    "%s: option requires an argument -- %c\n",
990Sstevel@tonic-gate 				    p, optopt);
1000Sstevel@tonic-gate 			return (BADCH);
1010Sstevel@tonic-gate 		} else				/* white space */
1020Sstevel@tonic-gate 			optarg = nargv[optind];
1030Sstevel@tonic-gate 		place = EMSG;
1040Sstevel@tonic-gate 		++optind;
1050Sstevel@tonic-gate 	}
1060Sstevel@tonic-gate 	return (optopt);			/* dump back option letter */
1070Sstevel@tonic-gate }
108