getopt-susv3.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* Copyright (C) 2003 Manuel Novoa III
  2. *
  3. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  4. */
  5. /* ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION!
  6. *
  7. * Besides uClibc, I'm using this code in my libc for elks, which is
  8. * a 16-bit environment with a fairly limited compiler. It would make
  9. * things much easier for me if this file isn't modified unnecessarily.
  10. * In particular, please put any new or replacement functions somewhere
  11. * else, and modify the makefile to use your version instead.
  12. * Thanks. Manuel
  13. *
  14. * ATTENTION! ATTENTION! ATTENTION! ATTENTION! ATTENTION! */
  15. /* Sep 7, 2003
  16. * Initial version of a SUSv3 compliant getopt().
  17. */
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <getopt.h>
  22. /* libc_hidden_proto(fprintf) */
  23. /* Experimentally off - libc_hidden_proto(strchr) */
  24. #ifdef __UCLIBC_MJN3_ONLY__
  25. #warning TODO: Enable gettext awareness.
  26. #endif /* __UCLIBC_MJN3_ONLY__ */
  27. #undef _
  28. #define _(X) X
  29. #ifdef __BCC__
  30. static const char missing[] = "option requires an argument";
  31. static const char illegal[] = "illegal option";
  32. #else
  33. static const char missing[] = "%s: option requires an argument -- %c\n";
  34. static const char illegal[] = "%s: illegal option -- %c\n";
  35. #endif
  36. int opterr = 1;
  37. int optind = 1;
  38. int optopt = 0;
  39. char *optarg = NULL;
  40. /* libc_hidden_proto(getopt) */
  41. int getopt(int argc, char * const argv[], const char *optstring)
  42. {
  43. static const char *o; /* multi opt position */
  44. register const char *p;
  45. register const char *s;
  46. int retval = -1;
  47. optopt = 0;
  48. optarg = NULL;
  49. if (!o) { /* Not in a multi-option arg. */
  50. if ((optind >= argc) /* No more args? */
  51. || ((p = argv[optind]) == NULL) /* Missing? */
  52. || (*p != '-') /* Not an option? */
  53. || (!*++p) /* "-" case? */
  54. ) {
  55. goto DONE;
  56. }
  57. if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
  58. /* ++optind; */
  59. /* goto DONE; */
  60. goto NEXTOPT; /* Less code generated... */
  61. }
  62. o = p;
  63. }
  64. #ifdef __BCC__
  65. p = o; /* Sigh... Help out bcc. */
  66. #define o p
  67. #endif
  68. retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
  69. if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
  70. s = illegal;
  71. retval = '?';
  72. goto BAD;
  73. }
  74. if (s[1] == ':') { /* Option takes an arg? */
  75. if (o[1]) { /* No space between option and arg? */
  76. optarg = (char *)(o + 1);
  77. goto NEXTOPT;
  78. }
  79. if (optind + 1 < argc) { /* Space between option and arg? */
  80. optarg = argv[++optind];
  81. } else { /* Out of args! */
  82. s = missing;
  83. retval = ':';
  84. BAD:
  85. optopt = *o;
  86. if (*optstring != ':') {
  87. retval = '?';
  88. if (opterr) {
  89. #ifdef __BCC__
  90. fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
  91. #else
  92. fprintf(stderr, _(s), argv[0], *o);
  93. #endif
  94. }
  95. }
  96. }
  97. }
  98. #ifdef __BCC__
  99. #undef o
  100. #endif
  101. if (!*++o) {
  102. NEXTOPT:
  103. o = NULL;
  104. ++optind;
  105. }
  106. DONE:
  107. return retval;
  108. }
  109. libc_hidden_def(getopt)