getopt1.c 4.3 KB

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