xref: /onnv-gate/usr/src/lib/libast/common/comp/getopt.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                   Phong Vo <kpv@research.att.com>                    *
204887Schin *                                                                      *
214887Schin ***********************************************************************/
224887Schin #pragma prototyped
234887Schin 
244887Schin #include <ast.h>
254887Schin 
264887Schin #undef	_lib_getopt	/* we can satisfy the api */
274887Schin 
284887Schin #if _lib_getopt
294887Schin 
304887Schin NoN(getopt)
314887Schin 
324887Schin #else
334887Schin 
344887Schin #undef	_BLD_ast	/* enable ast imports since we're user static */
354887Schin 
364887Schin #include <error.h>
374887Schin #include <option.h>
384887Schin 
394887Schin int		opterr = 1;
404887Schin int		optind = 1;
414887Schin int		optopt = 0;
424887Schin char*		optarg = 0;
434887Schin 
444887Schin static int	lastoptind;
454887Schin 
464887Schin extern int
474887Schin getopt(int argc, char* const* argv, const char* optstring)
484887Schin {
494887Schin 	int	n;
504887Schin 
514887Schin 	NoP(argc);
524887Schin 	opt_info.index = (optind > 1 || optind == lastoptind) ? optind : 0;
534887Schin 	if (opt_info.index >= argc)
544887Schin 		return -1;
554887Schin 	switch (n = optget((char**)argv, optstring))
564887Schin 	{
574887Schin 	case ':':
584887Schin 		n = '?';
594887Schin 		/*FALLTHROUGH*/
604887Schin 	case '?':
614887Schin 		if (opterr && (!optstring || *optstring != ':'))
624887Schin 		{
634887Schin 			if (!error_info.id)
644887Schin 				error_info.id = argv[0];
654887Schin 			errormsg(NiL, 2, opt_info.arg);
664887Schin 		}
674887Schin 		optopt = opt_info.option[1];
684887Schin 		break;
694887Schin 	case 0:
704887Schin 		n = -1;
714887Schin 		break;
724887Schin 	}
734887Schin 	optarg = opt_info.arg;
744887Schin 	lastoptind = optind = opt_info.index;
754887Schin 	return n;
764887Schin }
774887Schin 
784887Schin #endif
79