wingetopt.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. POSIX getopt for Windows
  3. AT&T Public License
  4. Code given out at the 1985 UNIFORUM conference in Dallas.
  5. */
  6. #ifdef _MSC_VER
  7. #include "wingetopt.h"
  8. #include <stdio.h>
  9. #include <string.h>
  10. #define EOF (-1)
  11. #define ERR(s, c) if(opterr){\
  12. char errbuf[2];\
  13. errbuf[0] = c; errbuf[1] = '\n';\
  14. fputs(argv[0], stderr);\
  15. fputs(s, stderr);\
  16. fputc(c, stderr);}
  17. //(void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  18. //(void) write(2, s, (unsigned)strlen(s));\
  19. //(void) write(2, errbuf, 2);}
  20. int opterr = 1;
  21. int optind = 1;
  22. int optopt;
  23. char *optarg;
  24. int getopt(int argc, char * const argv[], const char *opts)
  25. {
  26. static int sp = 1;
  27. register int c;
  28. register char *cp;
  29. if (sp == 1)
  30. if (optind >= argc ||
  31. argv[optind][0] != '-' || argv[optind][1] == '\0')
  32. return(EOF);
  33. else if (strcmp(argv[optind], "--") == 0) {
  34. optind++;
  35. return(EOF);
  36. }
  37. optopt = c = argv[optind][sp];
  38. if (c == ':' || (cp = strchr(opts, c)) == NULL) {
  39. ERR(": illegal option -- ", (char)c);
  40. if (argv[optind][++sp] == '\0') {
  41. optind++;
  42. sp = 1;
  43. }
  44. return('?');
  45. }
  46. if (*++cp == ':') {
  47. if (argv[optind][sp + 1] != '\0')
  48. optarg = (char*)&argv[optind++][sp + 1];
  49. else if (++optind >= argc) {
  50. ERR(": option requires an argument -- ", (char)c);
  51. sp = 1;
  52. return('?');
  53. }
  54. else
  55. optarg = (char*)argv[optind++];
  56. sp = 1;
  57. }
  58. else {
  59. if (argv[optind][++sp] == '\0') {
  60. sp = 1;
  61. optind++;
  62. }
  63. optarg = NULL;
  64. }
  65. return(c);
  66. }
  67. #endif /* __GNUC__ */