getopt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* $Id: getopt.c,v 1.3 2009-01-22 20:53:07 fwarmerdam 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 0
  31. static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
  32. __RCSID("$NetBSD: getopt.c,v 1.26 2003/08/07 16:43:40 agc Exp $");
  33. #endif
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include "libport.h"
  37. int opterr = 1, /* if error message should be printed */
  38. optind = 1, /* index into parent argv vector */
  39. optopt, /* character checked for validity */
  40. optreset; /* reset getopt */
  41. char *optarg; /* argument associated with option */
  42. #define BADCH (int)'?'
  43. #define BADARG (int)':'
  44. #define EMSG ""
  45. /*
  46. * getopt --
  47. * Parse argc/argv argument vector.
  48. */
  49. int
  50. getopt(int argc, char * const argv[], const char *optstring)
  51. {
  52. static char *place = EMSG; /* option letter processing */
  53. char *oli; /* option letter list index */
  54. if (optreset || *place == 0) { /* update scanning pointer */
  55. optreset = 0;
  56. place = argv[optind];
  57. if (optind >= argc || *place++ != '-') {
  58. /* Argument is absent or is not an option */
  59. place = EMSG;
  60. return (-1);
  61. }
  62. optopt = *place++;
  63. if (optopt == '-' && *place == 0) {
  64. /* "--" => end of options */
  65. ++optind;
  66. place = EMSG;
  67. return (-1);
  68. }
  69. if (optopt == 0) {
  70. /* Solitary '-', treat as a '-' option
  71. if the program (eg su) is looking for it. */
  72. place = EMSG;
  73. if (strchr(optstring, '-') == NULL)
  74. return -1;
  75. optopt = '-';
  76. }
  77. } else
  78. optopt = *place++;
  79. /* See if option letter is one the caller wanted... */
  80. if (optopt == ':' || (oli = strchr(optstring, optopt)) == NULL) {
  81. if (*place == 0)
  82. ++optind;
  83. if (opterr && *optstring != ':')
  84. (void)fprintf(stderr,
  85. "unknown option -- %c\n", optopt);
  86. return (BADCH);
  87. }
  88. /* Does this option need an argument? */
  89. if (oli[1] != ':') {
  90. /* don't need argument */
  91. optarg = NULL;
  92. if (*place == 0)
  93. ++optind;
  94. } else {
  95. /* Option-argument is either the rest of this argument or the
  96. entire next argument. */
  97. if (*place)
  98. optarg = place;
  99. else if (argc > ++optind)
  100. optarg = argv[optind];
  101. else {
  102. /* option-argument absent */
  103. place = EMSG;
  104. if (*optstring == ':')
  105. return (BADARG);
  106. if (opterr)
  107. (void)fprintf(stderr,
  108. "option requires an argument -- %c\n",
  109. optopt);
  110. return (BADCH);
  111. }
  112. place = EMSG;
  113. ++optind;
  114. }
  115. return (optopt); /* return option letter */
  116. }