getopt1.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:4k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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