getopt1.c 4.2 KB

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