Home | History | Annotate | Line # | Download | only in lib
getopt1.c revision 1.1.1.1.2.2
      1  1.1.1.1.2.2  jym /* getopt_long and getopt_long_only entry points for GNU getopt.
      2  1.1.1.1.2.2  jym    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
      3  1.1.1.1.2.2  jym      Free Software Foundation, Inc.
      4  1.1.1.1.2.2  jym    This file is part of the GNU C Library.
      5  1.1.1.1.2.2  jym 
      6  1.1.1.1.2.2  jym    This program is free software; you can redistribute it and/or modify
      7  1.1.1.1.2.2  jym    it under the terms of the GNU General Public License as published by
      8  1.1.1.1.2.2  jym    the Free Software Foundation; either version 2, or (at your option)
      9  1.1.1.1.2.2  jym    any later version.
     10  1.1.1.1.2.2  jym 
     11  1.1.1.1.2.2  jym    This program is distributed in the hope that it will be useful,
     12  1.1.1.1.2.2  jym    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  1.1.1.1.2.2  jym    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14  1.1.1.1.2.2  jym    GNU General Public License for more details.
     15  1.1.1.1.2.2  jym 
     16  1.1.1.1.2.2  jym    You should have received a copy of the GNU General Public License along
     17  1.1.1.1.2.2  jym    with this program; if not, write to the Free Software Foundation,
     18  1.1.1.1.2.2  jym    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
     19  1.1.1.1.2.2  jym 
     20  1.1.1.1.2.2  jym #ifdef HAVE_CONFIG_H
     22  1.1.1.1.2.2  jym # include <config.h>
     23  1.1.1.1.2.2  jym #endif
     24  1.1.1.1.2.2  jym 
     25  1.1.1.1.2.2  jym #ifdef _LIBC
     26  1.1.1.1.2.2  jym # include <getopt.h>
     27  1.1.1.1.2.2  jym #else
     28  1.1.1.1.2.2  jym # include "getopt.h"
     29  1.1.1.1.2.2  jym #endif
     30  1.1.1.1.2.2  jym #include "getopt_int.h"
     31  1.1.1.1.2.2  jym 
     32  1.1.1.1.2.2  jym #include <stdio.h>
     33  1.1.1.1.2.2  jym 
     34  1.1.1.1.2.2  jym /* This needs to come after some library #include
     35  1.1.1.1.2.2  jym    to get __GNU_LIBRARY__ defined.  */
     36  1.1.1.1.2.2  jym #ifdef __GNU_LIBRARY__
     37  1.1.1.1.2.2  jym #include <stdlib.h>
     38  1.1.1.1.2.2  jym #endif
     39  1.1.1.1.2.2  jym 
     40  1.1.1.1.2.2  jym #ifndef	NULL
     41  1.1.1.1.2.2  jym #define NULL 0
     42  1.1.1.1.2.2  jym #endif
     43  1.1.1.1.2.2  jym 
     44  1.1.1.1.2.2  jym int
     45  1.1.1.1.2.2  jym getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
     46  1.1.1.1.2.2  jym 	     const struct option *long_options, int *opt_index)
     47  1.1.1.1.2.2  jym {
     48  1.1.1.1.2.2  jym   return _getopt_internal (argc, (char **) argv, options, long_options,
     49  1.1.1.1.2.2  jym 			   opt_index, 0, 0);
     50  1.1.1.1.2.2  jym }
     51  1.1.1.1.2.2  jym 
     52  1.1.1.1.2.2  jym int
     53  1.1.1.1.2.2  jym _getopt_long_r (int argc, char **argv, const char *options,
     54  1.1.1.1.2.2  jym 		const struct option *long_options, int *opt_index,
     55  1.1.1.1.2.2  jym 		struct _getopt_data *d)
     56  1.1.1.1.2.2  jym {
     57  1.1.1.1.2.2  jym   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
     58  1.1.1.1.2.2  jym 			     0, 0, d);
     59  1.1.1.1.2.2  jym }
     60  1.1.1.1.2.2  jym 
     61  1.1.1.1.2.2  jym /* Like getopt_long, but '-' as well as '--' can indicate a long option.
     62  1.1.1.1.2.2  jym    If an option that starts with '-' (not '--') doesn't match a long option,
     63  1.1.1.1.2.2  jym    but does match a short option, it is parsed as a short option
     64  1.1.1.1.2.2  jym    instead.  */
     65  1.1.1.1.2.2  jym 
     66  1.1.1.1.2.2  jym int
     67  1.1.1.1.2.2  jym getopt_long_only (int argc, char *__getopt_argv_const *argv,
     68  1.1.1.1.2.2  jym 		  const char *options,
     69  1.1.1.1.2.2  jym 		  const struct option *long_options, int *opt_index)
     70  1.1.1.1.2.2  jym {
     71  1.1.1.1.2.2  jym   return _getopt_internal (argc, (char **) argv, options, long_options,
     72  1.1.1.1.2.2  jym 			   opt_index, 1, 0);
     73  1.1.1.1.2.2  jym }
     74  1.1.1.1.2.2  jym 
     75  1.1.1.1.2.2  jym int
     76  1.1.1.1.2.2  jym _getopt_long_only_r (int argc, char **argv, const char *options,
     77  1.1.1.1.2.2  jym 		     const struct option *long_options, int *opt_index,
     78  1.1.1.1.2.2  jym 		     struct _getopt_data *d)
     79  1.1.1.1.2.2  jym {
     80  1.1.1.1.2.2  jym   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
     81  1.1.1.1.2.2  jym 			     1, 0, d);
     82  1.1.1.1.2.2  jym }
     83  1.1.1.1.2.2  jym 
     84  1.1.1.1.2.2  jym 
     85  1.1.1.1.2.2  jym #ifdef TEST
     87  1.1.1.1.2.2  jym 
     88  1.1.1.1.2.2  jym #include <stdio.h>
     89  1.1.1.1.2.2  jym 
     90  1.1.1.1.2.2  jym int
     91  1.1.1.1.2.2  jym main (int argc, char **argv)
     92  1.1.1.1.2.2  jym {
     93  1.1.1.1.2.2  jym   int c;
     94  1.1.1.1.2.2  jym   int digit_optind = 0;
     95  1.1.1.1.2.2  jym 
     96  1.1.1.1.2.2  jym   while (1)
     97  1.1.1.1.2.2  jym     {
     98  1.1.1.1.2.2  jym       int this_option_optind = optind ? optind : 1;
     99  1.1.1.1.2.2  jym       int option_index = 0;
    100  1.1.1.1.2.2  jym       static struct option long_options[] =
    101  1.1.1.1.2.2  jym       {
    102  1.1.1.1.2.2  jym 	{"add", 1, 0, 0},
    103  1.1.1.1.2.2  jym 	{"append", 0, 0, 0},
    104  1.1.1.1.2.2  jym 	{"delete", 1, 0, 0},
    105  1.1.1.1.2.2  jym 	{"verbose", 0, 0, 0},
    106  1.1.1.1.2.2  jym 	{"create", 0, 0, 0},
    107  1.1.1.1.2.2  jym 	{"file", 1, 0, 0},
    108  1.1.1.1.2.2  jym 	{0, 0, 0, 0}
    109  1.1.1.1.2.2  jym       };
    110  1.1.1.1.2.2  jym 
    111  1.1.1.1.2.2  jym       c = getopt_long (argc, argv, "abc:d:0123456789",
    112  1.1.1.1.2.2  jym 		       long_options, &option_index);
    113  1.1.1.1.2.2  jym       if (c == -1)
    114  1.1.1.1.2.2  jym 	break;
    115  1.1.1.1.2.2  jym 
    116  1.1.1.1.2.2  jym       switch (c)
    117  1.1.1.1.2.2  jym 	{
    118  1.1.1.1.2.2  jym 	case 0:
    119  1.1.1.1.2.2  jym 	  printf ("option %s", long_options[option_index].name);
    120  1.1.1.1.2.2  jym 	  if (optarg)
    121  1.1.1.1.2.2  jym 	    printf (" with arg %s", optarg);
    122  1.1.1.1.2.2  jym 	  printf ("\n");
    123  1.1.1.1.2.2  jym 	  break;
    124  1.1.1.1.2.2  jym 
    125  1.1.1.1.2.2  jym 	case '0':
    126  1.1.1.1.2.2  jym 	case '1':
    127  1.1.1.1.2.2  jym 	case '2':
    128  1.1.1.1.2.2  jym 	case '3':
    129  1.1.1.1.2.2  jym 	case '4':
    130  1.1.1.1.2.2  jym 	case '5':
    131  1.1.1.1.2.2  jym 	case '6':
    132  1.1.1.1.2.2  jym 	case '7':
    133  1.1.1.1.2.2  jym 	case '8':
    134  1.1.1.1.2.2  jym 	case '9':
    135  1.1.1.1.2.2  jym 	  if (digit_optind != 0 && digit_optind != this_option_optind)
    136  1.1.1.1.2.2  jym 	    printf ("digits occur in two different argv-elements.\n");
    137  1.1.1.1.2.2  jym 	  digit_optind = this_option_optind;
    138  1.1.1.1.2.2  jym 	  printf ("option %c\n", c);
    139  1.1.1.1.2.2  jym 	  break;
    140  1.1.1.1.2.2  jym 
    141  1.1.1.1.2.2  jym 	case 'a':
    142  1.1.1.1.2.2  jym 	  printf ("option a\n");
    143  1.1.1.1.2.2  jym 	  break;
    144  1.1.1.1.2.2  jym 
    145  1.1.1.1.2.2  jym 	case 'b':
    146  1.1.1.1.2.2  jym 	  printf ("option b\n");
    147  1.1.1.1.2.2  jym 	  break;
    148  1.1.1.1.2.2  jym 
    149  1.1.1.1.2.2  jym 	case 'c':
    150  1.1.1.1.2.2  jym 	  printf ("option c with value `%s'\n", optarg);
    151  1.1.1.1.2.2  jym 	  break;
    152  1.1.1.1.2.2  jym 
    153  1.1.1.1.2.2  jym 	case 'd':
    154  1.1.1.1.2.2  jym 	  printf ("option d with value `%s'\n", optarg);
    155  1.1.1.1.2.2  jym 	  break;
    156  1.1.1.1.2.2  jym 
    157  1.1.1.1.2.2  jym 	case '?':
    158  1.1.1.1.2.2  jym 	  break;
    159  1.1.1.1.2.2  jym 
    160  1.1.1.1.2.2  jym 	default:
    161  1.1.1.1.2.2  jym 	  printf ("?? getopt returned character code 0%o ??\n", c);
    162  1.1.1.1.2.2  jym 	}
    163  1.1.1.1.2.2  jym     }
    164  1.1.1.1.2.2  jym 
    165  1.1.1.1.2.2  jym   if (optind < argc)
    166  1.1.1.1.2.2  jym     {
    167  1.1.1.1.2.2  jym       printf ("non-option ARGV-elements: ");
    168  1.1.1.1.2.2  jym       while (optind < argc)
    169  1.1.1.1.2.2  jym 	printf ("%s ", argv[optind++]);
    170  1.1.1.1.2.2  jym       printf ("\n");
    171  1.1.1.1.2.2  jym     }
    172  1.1.1.1.2.2  jym 
    173  1.1.1.1.2.2  jym   exit (0);
    174  1.1.1.1.2.2  jym }
    175                   
    176                   #endif /* TEST */
    177