/src/compat/getopt1.c

https://code.google.com/ · C · 185 lines · 124 code · 28 blank · 33 comment · 14 complexity · 7046d39b5054d58b664a4426e3ad39e9 MD5 · raw file

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