getopt-susv3.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #ifdef __UCLIBC_MJN3_ONLY__
  23. #warning TODO: Enable gettext awareness.
  24. #endif /* __UCLIBC_MJN3_ONLY__ */
  25. #undef _
  26. #define _(X) X
  27. #ifdef __BCC__
  28. static const char missing[] = "option requires an argument";
  29. static const char illegal[] = "illegal option";
  30. #else
  31. static const char missing[] = "%s: option requires an argument -- %c\n";
  32. static const char illegal[] = "%s: illegal option -- %c\n";
  33. #endif
  34. int opterr = 1;
  35. int optind = 1;
  36. int optopt = 0;
  37. char *optarg = NULL;
  38. int getopt(int argc, char * const argv[], const char *optstring)
  39. {
  40. static const char *o; /* multi opt position */
  41. register const char *p;
  42. register const char *s;
  43. int retval = -1;
  44. optopt = 0;
  45. optarg = NULL;
  46. if (!o) { /* Not in a multi-option arg. */
  47. if ((optind >= argc) /* No more args? */
  48. || ((p = argv[optind]) == NULL) /* Missing? */
  49. || (*p != '-') /* Not an option? */
  50. || (!*++p) /* "-" case? */
  51. ) {
  52. goto DONE;
  53. }
  54. if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
  55. /* ++optind; */
  56. /* goto DONE; */
  57. goto NEXTOPT; /* Less code generated... */
  58. }
  59. o = p;
  60. }
  61. #ifdef __BCC__
  62. p = o; /* Sigh... Help out bcc. */
  63. #define o p
  64. #endif
  65. retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
  66. if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
  67. s = illegal;
  68. retval = '?';
  69. goto BAD;
  70. }
  71. if (s[1] == ':') { /* Option takes an arg? */
  72. if (o[1]) { /* No space between option and arg? */
  73. optarg = (char *)(o + 1);
  74. goto NEXTOPT;
  75. }
  76. if (optind + 1 < argc) { /* Space between option and arg? */
  77. optarg = argv[++optind];
  78. } else { /* Out of args! */
  79. s = missing;
  80. retval = ':';
  81. BAD:
  82. optopt = *o;
  83. if (*optstring != ':') {
  84. retval = '?';
  85. if (opterr) {
  86. #ifdef __BCC__
  87. fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
  88. #else
  89. fprintf(stderr, _(s), argv[0], *o);
  90. #endif
  91. }
  92. }
  93. }
  94. }
  95. #ifdef __BCC__
  96. #undef o
  97. #endif
  98. if (!*++o) {
  99. NEXTOPT:
  100. o = NULL;
  101. ++optind;
  102. }
  103. DONE:
  104. return retval;
  105. }
  106. libc_hidden_def(getopt)