getopt.c 3.5 KB

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