"Fossies" - the Fresh Open Source Software Archive

Member "sitecopy-0.16.6/lib/getopt1.c" (2 Jan 2005, 3833 Bytes) of archive /linux/www/sitecopy-0.16.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "getopt1.c" see the Fossies "Dox" file reference documentation.

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