getopt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* $PostgreSQL: pgsql/src/port/getopt.c,v 1.11 2007/03/26 21:44:11 momjian Exp $ */
  2. /* This is used by psql under Win32 */
  3. /*
  4. * Copyright (c) 1987, 1993, 1994
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #ifdef _MSC_VER
  33. /* disable the following warnings
  34. * C4706: assignment within conditional expression
  35. */
  36. #pragma warning(disable:4706)
  37. #endif
  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(int nargc, char * const *nargv, const char *ostr)
  52. {
  53. static char *place = EMSG; /* option letter processing */
  54. char *oli; /* option letter list index */
  55. if (optreset || !*place)
  56. { /* update scanning pointer */
  57. optreset = 0;
  58. if (optind >= nargc || *(place = nargv[optind]) != '-')
  59. {
  60. place = EMSG;
  61. return -1;
  62. }
  63. if (place[1] && *++place == '-' && place[1] == '\0')
  64. { /* found "--" */
  65. ++optind;
  66. place = EMSG;
  67. return -1;
  68. }
  69. } /* option letter okay? */
  70. if ((optopt = (int) *place++) == (int) ':' ||
  71. !(oli = strchr(ostr, optopt)))
  72. {
  73. /*
  74. * if the user didn't specify '-' as an option, assume it means -1.
  75. */
  76. if (optopt == (int) '-')
  77. return -1;
  78. if (!*place)
  79. ++optind;
  80. if (opterr && *ostr != ':')
  81. (void) fprintf(stderr,
  82. "illegal option -- %c\n", optopt);
  83. return BADCH;
  84. }
  85. if (*++oli != ':')
  86. { /* don't need argument */
  87. optarg = NULL;
  88. if (!*place)
  89. ++optind;
  90. }
  91. else
  92. { /* need an argument */
  93. if (*place) /* no white space */
  94. optarg = place;
  95. else if (nargc <= ++optind)
  96. { /* no arg */
  97. place = EMSG;
  98. if (*ostr == ':')
  99. return BADARG;
  100. if (opterr)
  101. (void) fprintf(stderr,
  102. "option requires an argument -- %c\n",
  103. optopt);
  104. return BADCH;
  105. }
  106. else
  107. /* white space */
  108. optarg = nargv[optind];
  109. place = EMSG;
  110. ++optind;
  111. }
  112. return optopt; /* dump back option letter */
  113. }