getopt.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 1987 Regents of the University of California.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms are permitted
  6. * provided that: (1) source distributions retain this entire copyright
  7. * notice and comment, and (2) distributions including binaries display
  8. * the following acknowledgement: ``This product includes software
  9. * developed by the University of California, Berkeley and its contributors''
  10. * in the documentation or other materials provided with the distribution
  11. * and in all advertising materials mentioning features or use of this
  12. * software. Neither the name of the University nor the names of its
  13. * contributors may be used to endorse or promote products derived
  14. * from this software without specific prior written permission.
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  18. */
  19. #ifdef _WINDOWS
  20. #if defined(LIBC_SCCS) && !defined(lint)
  21. static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 6/1/90";
  22. #endif /* LIBC_SCCS and not lint */
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "lber.h"
  27. #define index strchr
  28. #define rindex strrchr
  29. /*
  30. * get option letter from argument vector
  31. */
  32. int opterr = 1, /* if error message should be printed */
  33. optind = 1, /* index into parent argv vector */
  34. optopt; /* character checked for validity */
  35. char *optarg; /* argument associated with option */
  36. #define BADCH (int)'?'
  37. #define EMSG ""
  38. int getopt(int nargc, char *const *nargv, const char *ostr)
  39. {
  40. static char *place = EMSG; /* option letter processing */
  41. register char *oli; /* option letter list index */
  42. char *p;
  43. if (!*place) { /* update scanning pointer */
  44. if (optind >= nargc || *(place = nargv[optind]) != '-') {
  45. place = EMSG;
  46. return(EOF);
  47. }
  48. if (place[1] && *++place == '-') { /* found "--" */
  49. ++optind;
  50. place = EMSG;
  51. return(EOF);
  52. }
  53. } /* option letter okay? */
  54. if ((optopt = (int)*place++) == (int)':' ||
  55. !(oli = index(ostr, optopt))) {
  56. /*
  57. * if the user didn't specify '-' as an option,
  58. * assume it means EOF.
  59. */
  60. if (optopt == (int)'-')
  61. return(EOF);
  62. if (!*place)
  63. ++optind;
  64. if (opterr) {
  65. if (!(p = rindex(*nargv, '/')))
  66. p = *nargv;
  67. else
  68. ++p;
  69. (void)fprintf(stderr, "%s: illegal option -- %c\n",
  70. p, optopt);
  71. }
  72. return(BADCH);
  73. }
  74. if (*++oli != ':') { /* don't need argument */
  75. optarg = NULL;
  76. if (!*place)
  77. ++optind;
  78. }
  79. else { /* need an argument */
  80. if (*place) /* no white space */
  81. optarg = place;
  82. else if (nargc <= ++optind) { /* no arg */
  83. place = EMSG;
  84. if (!(p = rindex(*nargv, '/')))
  85. p = *nargv;
  86. else
  87. ++p;
  88. if (opterr)
  89. (void)fprintf(stderr,
  90. "%s: option requires an argument -- %c\n",
  91. p, optopt);
  92. return(BADCH);
  93. }
  94. else /* white space */
  95. optarg = nargv[optind];
  96. place = EMSG;
  97. ++optind;
  98. }
  99. return(optopt); /* dump back option letter */
  100. }
  101. #endif