getopt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* $NetBSD: getopt.c,v 1.26 2003/08/07 16:43:40 agc 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. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #if defined(LIBC_SCCS) && !defined(lint)
  31. static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
  32. #endif /* LIBC_SCCS and not lint */
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. extern const char *_getprogname(void);
  38. int opterr = 1, /* if error message should be printed */
  39. optind = 1, /* index into parent argv vector */
  40. optopt, /* character checked for validity */
  41. optreset; /* reset getopt */
  42. char *optarg; /* argument associated with option */
  43. #define BADCH (int)'?'
  44. #define BADARG (int)':'
  45. #define EMSG ""
  46. /*
  47. * getopt --
  48. * Parse argc/argv argument vector.
  49. */
  50. int
  51. getopt(nargc, nargv, ostr)
  52. int nargc;
  53. char * const nargv[];
  54. const char *ostr;
  55. {
  56. static char *place = EMSG; /* option letter processing */
  57. char *oli; /* option letter list index */
  58. if (optreset || *place == 0) { /* update scanning pointer */
  59. optreset = 0;
  60. place = nargv[optind];
  61. if (optind >= nargc || *place++ != '-') {
  62. /* Argument is absent or is not an option */
  63. place = EMSG;
  64. return (-1);
  65. }
  66. optopt = *place++;
  67. if (optopt == '-' && *place == 0) {
  68. /* "--" => end of options */
  69. ++optind;
  70. place = EMSG;
  71. return (-1);
  72. }
  73. if (optopt == 0) {
  74. /* Solitary '-', treat as a '-' option
  75. if the program (eg su) is looking for it. */
  76. place = EMSG;
  77. if (strchr(ostr, '-') == NULL)
  78. return (-1);
  79. optopt = '-';
  80. }
  81. } else
  82. optopt = *place++;
  83. /* See if option letter is one the caller wanted... */
  84. if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
  85. if (*place == 0)
  86. ++optind;
  87. if (opterr && *ostr != ':')
  88. (void)fprintf(stderr,
  89. "%s: illegal option -- %c\n", _getprogname(),
  90. optopt);
  91. return (BADCH);
  92. }
  93. /* Does this option need an argument? */
  94. if (oli[1] != ':') {
  95. /* don't need argument */
  96. optarg = NULL;
  97. if (*place == 0)
  98. ++optind;
  99. } else {
  100. /* Option-argument is either the rest of this argument or the
  101. entire next argument. */
  102. if (*place)
  103. optarg = place;
  104. else if (nargc > ++optind)
  105. optarg = nargv[optind];
  106. else {
  107. /* option-argument absent */
  108. place = EMSG;
  109. if (*ostr == ':')
  110. return (BADARG);
  111. if (opterr)
  112. (void)fprintf(stderr,
  113. "%s: option requires an argument -- %c\n",
  114. _getprogname(), optopt);
  115. return (BADCH);
  116. }
  117. place = EMSG;
  118. ++optind;
  119. }
  120. return (optopt); /* return option letter */
  121. }