libextractor

GNU libextractor
Log | Files | Refs | Submodules | README | LICENSE

getopt1.c (4517B)


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