getopt1.c 4.7 KB

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