getopt1.c
上传用户:wstnjxml
上传日期:2014-04-03
资源大小:7248k
文件大小:5k
源码类别:

Windows CE

开发平台:

C/C++

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