17c478bd9Sstevel@tonic-gate /* getopt_long and getopt_long_only entry points for GNU getopt.
27c478bd9Sstevel@tonic-gate    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
37c478bd9Sstevel@tonic-gate      Free Software Foundation, Inc.
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate    NOTE: The canonical source of this file is maintained with the GNU C Library.
67c478bd9Sstevel@tonic-gate    Bugs can be reported to bug-glibc@gnu.org.
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate    This program is free software; you can redistribute it and/or modify it
97c478bd9Sstevel@tonic-gate    under the terms of the GNU General Public License as published by the
107c478bd9Sstevel@tonic-gate    Free Software Foundation; either version 2, or (at your option) any
117c478bd9Sstevel@tonic-gate    later version.
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate    This program is distributed in the hope that it will be useful,
147c478bd9Sstevel@tonic-gate    but WITHOUT ANY WARRANTY; without even the implied warranty of
157c478bd9Sstevel@tonic-gate    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
167c478bd9Sstevel@tonic-gate    GNU General Public License for more details.
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate    You should have received a copy of the GNU General Public License
197c478bd9Sstevel@tonic-gate    along with this program; if not, write to the Free Software
207c478bd9Sstevel@tonic-gate    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
217c478bd9Sstevel@tonic-gate    USA.  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #ifdef HAVE_CONFIG_H
247c478bd9Sstevel@tonic-gate #include <config.h>
257c478bd9Sstevel@tonic-gate #endif
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include "getopt.h"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #if !defined __STDC__ || !__STDC__
307c478bd9Sstevel@tonic-gate /* This is a separate conditional since some stdc systems
317c478bd9Sstevel@tonic-gate    reject `defined (const)'.  */
327c478bd9Sstevel@tonic-gate #ifndef const
337c478bd9Sstevel@tonic-gate #define const
347c478bd9Sstevel@tonic-gate #endif
357c478bd9Sstevel@tonic-gate #endif
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /* Comment out all this code if we are using the GNU C Library, and are not
407c478bd9Sstevel@tonic-gate    actually compiling the library itself.  This code is part of the GNU C
417c478bd9Sstevel@tonic-gate    Library, but also included in many other GNU distributions.  Compiling
427c478bd9Sstevel@tonic-gate    and linking in this code is a waste when using the GNU C library
437c478bd9Sstevel@tonic-gate    (especially if it is a shared library).  Rather than having every GNU
447c478bd9Sstevel@tonic-gate    program understand `configure --with-gnu-libc' and omit the object files,
457c478bd9Sstevel@tonic-gate    it is simpler to just do this in the source for each such file.  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #define GETOPT_INTERFACE_VERSION 2
487c478bd9Sstevel@tonic-gate #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
497c478bd9Sstevel@tonic-gate #include <gnu-versions.h>
507c478bd9Sstevel@tonic-gate #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
517c478bd9Sstevel@tonic-gate #define ELIDE_CODE
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #ifndef ELIDE_CODE
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /* This needs to come after some library #include
597c478bd9Sstevel@tonic-gate    to get __GNU_LIBRARY__ defined.  */
607c478bd9Sstevel@tonic-gate #ifdef __GNU_LIBRARY__
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #endif
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifndef	NULL
657c478bd9Sstevel@tonic-gate #define NULL 0
667c478bd9Sstevel@tonic-gate #endif
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate int
getopt_long(argc,argv,options,long_options,opt_index)697c478bd9Sstevel@tonic-gate getopt_long (argc, argv, options, long_options, opt_index)
707c478bd9Sstevel@tonic-gate      int argc;
717c478bd9Sstevel@tonic-gate      char *const *argv;
727c478bd9Sstevel@tonic-gate      const char *options;
737c478bd9Sstevel@tonic-gate      const struct option *long_options;
747c478bd9Sstevel@tonic-gate      int *opt_index;
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* Like getopt_long, but '-' as well as '--' can indicate a long option.
807c478bd9Sstevel@tonic-gate    If an option that starts with '-' (not '--') doesn't match a long option,
817c478bd9Sstevel@tonic-gate    but does match a short option, it is parsed as a short option
827c478bd9Sstevel@tonic-gate    instead.  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate int
getopt_long_only(argc,argv,options,long_options,opt_index)857c478bd9Sstevel@tonic-gate getopt_long_only (argc, argv, options, long_options, opt_index)
867c478bd9Sstevel@tonic-gate      int argc;
877c478bd9Sstevel@tonic-gate      char *const *argv;
887c478bd9Sstevel@tonic-gate      const char *options;
897c478bd9Sstevel@tonic-gate      const struct option *long_options;
907c478bd9Sstevel@tonic-gate      int *opt_index;
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate #endif	/* Not ELIDE_CODE.  */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate #ifdef TEST
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #include <stdio.h>
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate int
main(argc,argv)1037c478bd9Sstevel@tonic-gate main (argc, argv)
1047c478bd9Sstevel@tonic-gate      int argc;
1057c478bd9Sstevel@tonic-gate      char **argv;
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate   int c;
1087c478bd9Sstevel@tonic-gate   int digit_optind = 0;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate   while (1)
1117c478bd9Sstevel@tonic-gate     {
1127c478bd9Sstevel@tonic-gate       int this_option_optind = optind ? optind : 1;
1137c478bd9Sstevel@tonic-gate       int option_index = 0;
1147c478bd9Sstevel@tonic-gate       static struct option long_options[] =
1157c478bd9Sstevel@tonic-gate       {
1167c478bd9Sstevel@tonic-gate 	{"add", 1, 0, 0},
1177c478bd9Sstevel@tonic-gate 	{"append", 0, 0, 0},
1187c478bd9Sstevel@tonic-gate 	{"delete", 1, 0, 0},
1197c478bd9Sstevel@tonic-gate 	{"verbose", 0, 0, 0},
1207c478bd9Sstevel@tonic-gate 	{"create", 0, 0, 0},
1217c478bd9Sstevel@tonic-gate 	{"file", 1, 0, 0},
1227c478bd9Sstevel@tonic-gate 	{0, 0, 0, 0}
1237c478bd9Sstevel@tonic-gate       };
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate       c = getopt_long (argc, argv, "abc:d:0123456789",
1267c478bd9Sstevel@tonic-gate 		       long_options, &option_index);
1277c478bd9Sstevel@tonic-gate       if (c == -1)
1287c478bd9Sstevel@tonic-gate 	break;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate       switch (c)
1317c478bd9Sstevel@tonic-gate 	{
1327c478bd9Sstevel@tonic-gate 	case 0:
1337c478bd9Sstevel@tonic-gate 	  printf ("option %s", long_options[option_index].name);
1347c478bd9Sstevel@tonic-gate 	  if (optarg)
1357c478bd9Sstevel@tonic-gate 	    printf (" with arg %s", optarg);
1367c478bd9Sstevel@tonic-gate 	  printf ("\n");
1377c478bd9Sstevel@tonic-gate 	  break;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	case '0':
1407c478bd9Sstevel@tonic-gate 	case '1':
1417c478bd9Sstevel@tonic-gate 	case '2':
1427c478bd9Sstevel@tonic-gate 	case '3':
1437c478bd9Sstevel@tonic-gate 	case '4':
1447c478bd9Sstevel@tonic-gate 	case '5':
1457c478bd9Sstevel@tonic-gate 	case '6':
1467c478bd9Sstevel@tonic-gate 	case '7':
1477c478bd9Sstevel@tonic-gate 	case '8':
1487c478bd9Sstevel@tonic-gate 	case '9':
1497c478bd9Sstevel@tonic-gate 	  if (digit_optind != 0 && digit_optind != this_option_optind)
1507c478bd9Sstevel@tonic-gate 	    printf ("digits occur in two different argv-elements.\n");
1517c478bd9Sstevel@tonic-gate 	  digit_optind = this_option_optind;
1527c478bd9Sstevel@tonic-gate 	  printf ("option %c\n", c);
1537c478bd9Sstevel@tonic-gate 	  break;
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	case 'a':
1567c478bd9Sstevel@tonic-gate 	  printf ("option a\n");
1577c478bd9Sstevel@tonic-gate 	  break;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	case 'b':
1607c478bd9Sstevel@tonic-gate 	  printf ("option b\n");
1617c478bd9Sstevel@tonic-gate 	  break;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	case 'c':
1647c478bd9Sstevel@tonic-gate 	  printf ("option c with value `%s'\n", optarg);
1657c478bd9Sstevel@tonic-gate 	  break;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	case 'd':
1687c478bd9Sstevel@tonic-gate 	  printf ("option d with value `%s'\n", optarg);
1697c478bd9Sstevel@tonic-gate 	  break;
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	case '?':
1727c478bd9Sstevel@tonic-gate 	  break;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	default:
1757c478bd9Sstevel@tonic-gate 	  printf ("?? getopt returned character code 0%o ??\n", c);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate     }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate   if (optind < argc)
1807c478bd9Sstevel@tonic-gate     {
1817c478bd9Sstevel@tonic-gate       printf ("non-option ARGV-elements: ");
1827c478bd9Sstevel@tonic-gate       while (optind < argc)
1837c478bd9Sstevel@tonic-gate 	printf ("%s ", argv[optind++]);
1847c478bd9Sstevel@tonic-gate       printf ("\n");
1857c478bd9Sstevel@tonic-gate     }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate   exit (0);
1887c478bd9Sstevel@tonic-gate }
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate #endif /* TEST */
191