getopt1.c 5.0 KB

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