getopt-susv3.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 __BCC__
  23. static const char missing[] = "option requires an argument";
  24. static const char illegal[] = "illegal option";
  25. #else
  26. static const char missing[] = "%s: option requires an argument -- %c\n";
  27. static const char illegal[] = "%s: illegal option -- %c\n";
  28. #endif
  29. int opterr = 1;
  30. int optind = 1;
  31. int optopt = 0;
  32. char *optarg = NULL;
  33. int getopt(int argc, char * const argv[], const char *optstring)
  34. {
  35. static const char *o; /* multi opt position */
  36. register const char *p;
  37. register const char *s;
  38. int retval = -1;
  39. optopt = 0;
  40. optarg = NULL;
  41. /*
  42. * POSIX says behavior when optind == 0 is unspecified, but other
  43. * libcs use this wiggle room to allow reseting getopt; we can too
  44. */
  45. if (!optind) {
  46. o = NULL;
  47. optind = 1;
  48. }
  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)