getopt-susv3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (C) 2003 Manuel Novoa III
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Library General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2 of the License, or (at your option) any later version.
  7. *
  8. * This library 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 GNU
  11. * Library General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Library General Public
  14. * License along with this library; if not, write to the Free
  15. * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  18. *
  19. * Besides uClibc, I'm using this code in my libc for elks, which is
  20. * a 16-bit environment with a fairly limited compiler. It would make
  21. * things much easier for me if this file isn't modified unnecessarily.
  22. * In particular, please put any new or replacement functions somewhere
  23. * else, and modify the makefile to use your version instead.
  24. * Thanks. Manuel
  25. *
  26. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  27. /* Sep 7, 2003
  28. * Initial version of a SUSv3 compliant getopt().
  29. */
  30. #include <unistd.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <getopt.h>
  34. libc_hidden_proto(fprintf)
  35. libc_hidden_proto(strchr)
  36. libc_hidden_proto(stderr)
  37. #ifdef __UCLIBC_MJN3_ONLY__
  38. #warning TODO: Enable gettext awareness.
  39. #endif /* __UCLIBC_MJN3_ONLY__ */
  40. #undef _
  41. #define _(X) X
  42. #ifdef __BCC__
  43. static const char missing[] = "option requires an argument";
  44. static const char illegal[] = "illegal option";
  45. #else
  46. static const char missing[] = "%s: option requires an argument -- %c\n";
  47. static const char illegal[] = "%s: illegal option -- %c\n";
  48. #endif
  49. libc_hidden_proto(opterr)
  50. int opterr = 1;
  51. libc_hidden_data_def(opterr)
  52. libc_hidden_proto(optind)
  53. int optind = 1;
  54. libc_hidden_data_def(optind)
  55. libc_hidden_proto(optopt)
  56. int optopt = 0;
  57. libc_hidden_data_def(optopt)
  58. libc_hidden_proto(optarg)
  59. char *optarg = NULL;
  60. libc_hidden_data_def(optarg)
  61. int getopt(int argc, char * const argv[], const char *optstring)
  62. {
  63. static const char *o; /* multi opt position */
  64. register const char *p;
  65. register const char *s;
  66. int retval = -1;
  67. optopt = 0;
  68. optarg = NULL;
  69. if (!o) { /* Not in a multi-option arg. */
  70. if ((optind >= argc) /* No more args? */
  71. || ((p = argv[optind]) == NULL) /* Missing? */
  72. || (*p != '-') /* Not an option? */
  73. || (!*++p) /* "-" case? */
  74. ) {
  75. goto DONE;
  76. }
  77. if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
  78. /* ++optind; */
  79. /* goto DONE; */
  80. goto NEXTOPT; /* Less code generated... */
  81. }
  82. o = p;
  83. }
  84. #ifdef __BCC__
  85. p = o; /* Sigh... Help out bcc. */
  86. #define o p
  87. #endif
  88. retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
  89. if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
  90. s = illegal;
  91. retval = '?';
  92. goto BAD;
  93. }
  94. if (s[1] == ':') { /* Option takes an arg? */
  95. if (o[1]) { /* No space between option and arg? */
  96. optarg = (char *)(o + 1);
  97. goto NEXTOPT;
  98. }
  99. if (optind + 1 < argc) { /* Space between option and arg? */
  100. optarg = argv[++optind];
  101. } else { /* Out of args! */
  102. s = missing;
  103. retval = ':';
  104. BAD:
  105. optopt = *o;
  106. if (*optstring != ':') {
  107. retval = '?';
  108. if (opterr) {
  109. #ifdef __BCC__
  110. fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
  111. #else
  112. fprintf(stderr, _(s), argv[0], *o);
  113. #endif
  114. }
  115. }
  116. }
  117. }
  118. #ifdef __BCC__
  119. #undef o
  120. #endif
  121. if (!*++o) {
  122. NEXTOPT:
  123. o = NULL;
  124. ++optind;
  125. }
  126. DONE:
  127. return retval;
  128. }