ares_getopt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Original file name getopt.c Initial import into the c-ares source tree
  3. * on 2007-04-11. Lifted from version 5.2 of the 'Open Mash' project with
  4. * the modified BSD license, BSD license without the advertising clause.
  5. *
  6. */
  7. /*
  8. * getopt.c --
  9. *
  10. * Standard UNIX getopt function. Code is from BSD.
  11. *
  12. * Copyright (c) 1987-2001 The Regents of the University of California.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * A. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * B. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * C. Neither the names of the copyright holders nor the names of its
  24. * contributors may be used to endorse or promote products derived from this
  25. * software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
  28. * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  29. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. /* #if !defined(lint)
  40. * static char sccsid[] = "@(#)getopt.c 8.2 (Berkeley) 4/2/94";
  41. * #endif
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include "ares_getopt.h"
  47. int opterr = 1, /* if error message should be printed */
  48. optind = 1; /* index into parent argv vector */
  49. int optopt = 0; /* character checked for validity */
  50. static int optreset; /* reset getopt */
  51. char *optarg; /* argument associated with option */
  52. #define BADCH (int)'?'
  53. #define BADARG (int)':'
  54. #define EMSG (char *)""
  55. /*
  56. * ares_getopt --
  57. * Parse argc/argv argument vector.
  58. */
  59. int
  60. ares_getopt(int nargc, char * const nargv[], const char *ostr)
  61. {
  62. static char *place = EMSG; /* option letter processing */
  63. char *oli; /* option letter list index */
  64. if (optreset || !*place) { /* update scanning pointer */
  65. optreset = 0;
  66. if (optind >= nargc || *(place = nargv[optind]) != '-') {
  67. place = EMSG;
  68. return (EOF);
  69. }
  70. if (place[1] && *++place == '-') { /* found "--" */
  71. ++optind;
  72. place = EMSG;
  73. return (EOF);
  74. }
  75. } /* option letter okay? */
  76. if ((optopt = (int)*place++) == (int)':' ||
  77. (oli = strchr(ostr, optopt)) == NULL) {
  78. /*
  79. * if the user didn't specify '-' as an option,
  80. * assume it means EOF.
  81. */
  82. if (optopt == (int)'-')
  83. return (EOF);
  84. if (!*place)
  85. ++optind;
  86. if (opterr && *ostr != ':')
  87. (void)fprintf(stderr,
  88. "%s: illegal option -- %c\n", __FILE__, optopt);
  89. return (BADCH);
  90. }
  91. if (*++oli != ':') { /* don't need argument */
  92. optarg = NULL;
  93. if (!*place)
  94. ++optind;
  95. }
  96. else { /* need an argument */
  97. if (*place) /* no white space */
  98. optarg = place;
  99. else if (nargc <= ++optind) { /* no arg */
  100. place = EMSG;
  101. if (*ostr == ':')
  102. return (BADARG);
  103. if (opterr)
  104. (void)fprintf(stderr,
  105. "%s: option requires an argument -- %c\n",
  106. __FILE__, optopt);
  107. return (BADCH);
  108. }
  109. else /* white space */
  110. optarg = nargv[optind];
  111. place = EMSG;
  112. ++optind;
  113. }
  114. return (optopt); /* dump back option letter */
  115. }