getopt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
  2. /*
  3. * Copyright (c) 1987, 1993, 1994
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. */
  34. #if 0
  35. static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
  36. #endif
  37. #include <assert.h>
  38. #include <errno.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #define __P(x) x
  42. #define _DIAGASSERT(x) assert(x)
  43. #ifdef __weak_alias
  44. __weak_alias(getopt,_getopt);
  45. #endif
  46. int opterr = 1, /* if error message should be printed */
  47. optind = 1, /* index into parent argv vector */
  48. optopt, /* character checked for validity */
  49. optreset; /* reset getopt */
  50. char *optarg; /* argument associated with option */
  51. static char * _progname __P((char *));
  52. int getopt_internal __P((int, char * const *, const char *));
  53. static char *
  54. _progname(nargv0)
  55. char * nargv0;
  56. {
  57. char * tmp;
  58. _DIAGASSERT(nargv0 != NULL);
  59. tmp = strrchr(nargv0, '/');
  60. if (tmp)
  61. tmp++;
  62. else
  63. tmp = nargv0;
  64. return(tmp);
  65. }
  66. #define BADCH (int)'?'
  67. #define BADARG (int)':'
  68. #define EMSG ""
  69. /*
  70. * getopt --
  71. * Parse argc/argv argument vector.
  72. */
  73. int
  74. getopt(nargc, nargv, ostr)
  75. int nargc;
  76. char * const nargv[];
  77. const char *ostr;
  78. {
  79. static char *__progname = 0;
  80. static char *place = EMSG; /* option letter processing */
  81. char *oli; /* option letter list index */
  82. __progname = __progname?__progname:_progname(*nargv);
  83. _DIAGASSERT(nargv != NULL);
  84. _DIAGASSERT(ostr != NULL);
  85. if (optreset || !*place) { /* update scanning pointer */
  86. optreset = 0;
  87. if (optind >= nargc || *(place = nargv[optind]) != '-') {
  88. place = EMSG;
  89. return (-1);
  90. }
  91. if (place[1] && *++place == '-' /* found "--" */
  92. && place[1] == '\0') {
  93. ++optind;
  94. place = EMSG;
  95. return (-1);
  96. }
  97. } /* option letter okay? */
  98. if ((optopt = (int)*place++) == (int)':' ||
  99. !(oli = strchr(ostr, optopt))) {
  100. /*
  101. * if the user didn't specify '-' as an option,
  102. * assume it means -1.
  103. */
  104. if (optopt == (int)'-')
  105. return (-1);
  106. if (!*place)
  107. ++optind;
  108. if (opterr && *ostr != ':')
  109. (void)fprintf(stderr,
  110. "%s: illegal option -- %c\n", __progname, optopt);
  111. return (BADCH);
  112. }
  113. if (*++oli != ':') { /* don't need argument */
  114. optarg = NULL;
  115. if (!*place)
  116. ++optind;
  117. }
  118. else { /* need an argument */
  119. if (*place) /* no white space */
  120. optarg = place;
  121. else if (nargc <= ++optind) { /* no arg */
  122. place = EMSG;
  123. if (*ostr == ':')
  124. return (BADARG);
  125. if (opterr)
  126. (void)fprintf(stderr,
  127. "%s: option requires an argument -- %c\n",
  128. __progname, optopt);
  129. return (BADCH);
  130. }
  131. else /* white space */
  132. optarg = nargv[optind];
  133. place = EMSG;
  134. ++optind;
  135. }
  136. return (optopt); /* dump back option letter */
  137. }