🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.1 KiB

  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. * 4. 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 "getopt.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. * Get program name in Windows
  47. */
  48. const char * _getprogname(void);
  49. /*
  50. * getopt --
  51. * Parse argc/argv argument vector.
  52. */
  53. int
  54. getopt(int nargc, char * const nargv[], 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. }
  122. const char * _getprogname() {
  123. char *pgmptr = NULL;
  124. _get_pgmptr(&pgmptr);
  125. return strrchr(pgmptr,'\\')+1;
  126. }