getopt1.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #ifdef HAVE_CONFIG_H
  18. #include <config.h>
  19. #endif
  20. #include "getopt.h"
  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. /* 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 (argc, argv, options, long_options, opt_index)
  54. int argc;
  55. char *const *argv;
  56. const char *options;
  57. const struct option *long_options;
  58. int *opt_index;
  59. {
  60. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  61. }
  62. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  63. If an option that starts with '-' (not '--') doesn't match a long option,
  64. but does match a short option, it is parsed as a short option
  65. instead. */
  66. int
  67. getopt_long_only (argc, argv, options, long_options, opt_index)
  68. int argc;
  69. char *const *argv;
  70. const char *options;
  71. const struct option *long_options;
  72. int *opt_index;
  73. {
  74. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  75. }
  76. #endif /* Not ELIDE_CODE. */
  77. #ifdef TEST
  78. #include <stdio.h>
  79. int
  80. main (argc, argv)
  81. int argc;
  82. char **argv;
  83. {
  84. int c;
  85. int digit_optind = 0;
  86. while (1)
  87. {
  88. int this_option_optind = optind ? optind : 1;
  89. int option_index = 0;
  90. static struct option long_options[] =
  91. {
  92. {"add", 1, 0, 0},
  93. {"append", 0, 0, 0},
  94. {"delete", 1, 0, 0},
  95. {"verbose", 0, 0, 0},
  96. {"create", 0, 0, 0},
  97. {"file", 1, 0, 0},
  98. {0, 0, 0, 0}
  99. };
  100. c = getopt_long (argc, argv, "abc:d:0123456789",
  101. long_options, &option_index);
  102. if (c == -1)
  103. break;
  104. switch (c)
  105. {
  106. case 0:
  107. printf ("option %s", long_options[option_index].name);
  108. if (optarg)
  109. printf (" with arg %s", optarg);
  110. printf ("\n");
  111. break;
  112. case '0':
  113. case '1':
  114. case '2':
  115. case '3':
  116. case '4':
  117. case '5':
  118. case '6':
  119. case '7':
  120. case '8':
  121. case '9':
  122. if (digit_optind != 0 && digit_optind != this_option_optind)
  123. printf ("digits occur in two different argv-elements.\n");
  124. digit_optind = this_option_optind;
  125. printf ("option %c\n", c);
  126. break;
  127. case 'a':
  128. printf ("option a\n");
  129. break;
  130. case 'b':
  131. printf ("option b\n");
  132. break;
  133. case 'c':
  134. printf ("option c with value `%s'\n", optarg);
  135. break;
  136. case 'd':
  137. printf ("option d with value `%s'\n", optarg);
  138. break;
  139. case '?':
  140. break;
  141. default:
  142. printf ("?? getopt returned character code 0%o ??\n", c);
  143. }
  144. }
  145. if (optind < argc)
  146. {
  147. printf ("non-option ARGV-elements: ");
  148. while (optind < argc)
  149. printf ("%s ", argv[optind++]);
  150. printf ("\n");
  151. }
  152. exit (0);
  153. }
  154. #endif /* TEST */