getopt1.c 4.0 KB

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