getopt-susv3.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. if (!o) { /* Not in a multi-option arg. */
  42. if ((optind >= argc) /* No more args? */
  43. || ((p = argv[optind]) == NULL) /* Missing? */
  44. || (*p != '-') /* Not an option? */
  45. || (!*++p) /* "-" case? */
  46. ) {
  47. goto DONE;
  48. }
  49. if ((*p == '-') && (p[1] == 0)) { /* "--" case. */
  50. /* ++optind; */
  51. /* goto DONE; */
  52. goto NEXTOPT; /* Less code generated... */
  53. }
  54. o = p;
  55. }
  56. #ifdef __BCC__
  57. p = o; /* Sigh... Help out bcc. */
  58. #define o p
  59. #endif
  60. retval = (unsigned char) *o; /* Avoid problems for char val of -1. */
  61. if ((*o == ':') || !(s = strchr(optstring, *o))) { /* Illegal option? */
  62. s = illegal;
  63. retval = '?';
  64. goto BAD;
  65. }
  66. if (s[1] == ':') { /* Option takes an arg? */
  67. if (o[1]) { /* No space between option and arg? */
  68. optarg = (char *)(o + 1);
  69. goto NEXTOPT;
  70. }
  71. if (optind + 1 < argc) { /* Space between option and arg? */
  72. optarg = argv[++optind];
  73. } else { /* Out of args! */
  74. s = missing;
  75. retval = ':';
  76. BAD:
  77. optopt = *o;
  78. if (*optstring != ':') {
  79. retval = '?';
  80. if (opterr) {
  81. #ifdef __BCC__
  82. fprintf(stderr, "%s: %s -- %c\n", argv[0], s, *o);
  83. #else
  84. fprintf(stderr, _(s), argv[0], *o);
  85. #endif
  86. }
  87. }
  88. }
  89. }
  90. #ifdef __BCC__
  91. #undef o
  92. #endif
  93. if (!*++o) {
  94. NEXTOPT:
  95. o = NULL;
  96. ++optind;
  97. }
  98. DONE:
  99. return retval;
  100. }
  101. libc_hidden_def(getopt)