getopt1.c 4.3 KB

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