getopt-susv3.c 2.6 KB

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