getopt1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* getopt_long and getopt_long_only entry points for GNU getopt.
  2. Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
  3. 1998, 2004, 2006, 2009, 2010 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 3 of the License, or
  8. (at your option) 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
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #ifdef _LIBC
  16. # include <getopt.h>
  17. #else
  18. # include <config.h>
  19. # include "getopt.h"
  20. #endif
  21. #include "getopt_int.h"
  22. #include <stdio.h>
  23. /* This needs to come after some library #include
  24. to get __GNU_LIBRARY__ defined. */
  25. #ifdef __GNU_LIBRARY__
  26. #include <stdlib.h>
  27. #endif
  28. #ifndef NULL
  29. #define NULL 0
  30. #endif
  31. int
  32. getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
  33. const struct option *long_options, int *opt_index)
  34. {
  35. return _getopt_internal (argc, (char **) argv, options, long_options,
  36. opt_index, 0, 0);
  37. }
  38. int
  39. _getopt_long_r (int argc, char **argv, const char *options,
  40. const struct option *long_options, int *opt_index,
  41. struct _getopt_data *d)
  42. {
  43. return _getopt_internal_r (argc, argv, options, long_options, opt_index,
  44. 0, d, 0);
  45. }
  46. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  47. If an option that starts with '-' (not '--') doesn't match a long option,
  48. but does match a short option, it is parsed as a short option
  49. instead. */
  50. int
  51. getopt_long_only (int argc, char *__getopt_argv_const *argv,
  52. const char *options,
  53. const struct option *long_options, int *opt_index)
  54. {
  55. return _getopt_internal (argc, (char **) argv, options, long_options,
  56. opt_index, 1, 0);
  57. }
  58. int
  59. _getopt_long_only_r (int argc, char **argv, const char *options,
  60. const struct option *long_options, int *opt_index,
  61. struct _getopt_data *d)
  62. {
  63. return _getopt_internal_r (argc, argv, options, long_options, opt_index,
  64. 1, d, 0);
  65. }
  66. #ifdef TEST
  67. #include <stdio.h>
  68. int
  69. main (int argc, char **argv)
  70. {
  71. int c;
  72. int digit_optind = 0;
  73. while (1)
  74. {
  75. int this_option_optind = optind ? optind : 1;
  76. int option_index = 0;
  77. static const struct option long_options[] =
  78. {
  79. {"add", 1, 0, 0},
  80. {"append", 0, 0, 0},
  81. {"delete", 1, 0, 0},
  82. {"verbose", 0, 0, 0},
  83. {"create", 0, 0, 0},
  84. {"file", 1, 0, 0},
  85. {0, 0, 0, 0}
  86. };
  87. c = getopt_long (argc, argv, "abc:d:0123456789",
  88. long_options, &option_index);
  89. if (c == -1)
  90. break;
  91. switch (c)
  92. {
  93. case 0:
  94. printf ("option %s", long_options[option_index].name);
  95. if (optarg)
  96. printf (" with arg %s", optarg);
  97. printf ("\n");
  98. break;
  99. case '0':
  100. case '1':
  101. case '2':
  102. case '3':
  103. case '4':
  104. case '5':
  105. case '6':
  106. case '7':
  107. case '8':
  108. case '9':
  109. if (digit_optind != 0 && digit_optind != this_option_optind)
  110. printf ("digits occur in two different argv-elements.\n");
  111. digit_optind = this_option_optind;
  112. printf ("option %c\n", c);
  113. break;
  114. case 'a':
  115. printf ("option a\n");
  116. break;
  117. case 'b':
  118. printf ("option b\n");
  119. break;
  120. case 'c':
  121. printf ("option c with value `%s'\n", optarg);
  122. break;
  123. case 'd':
  124. printf ("option d with value `%s'\n", optarg);
  125. break;
  126. case '?':
  127. break;
  128. default:
  129. printf ("?? getopt returned character code 0%o ??\n", c);
  130. }
  131. }
  132. if (optind < argc)
  133. {
  134. printf ("non-option ARGV-elements: ");
  135. while (optind < argc)
  136. printf ("%s ", argv[optind++]);
  137. printf ("\n");
  138. }
  139. exit (0);
  140. }
  141. #endif /* TEST */