"Fossies" - the Fresh Open Source Software Archive

Member "npadmin-0.14/getopt1.c" (19 Apr 2001, 5359 Bytes) of package /linux/misc/old/npadmin-0.14.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.

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