Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
1.9 KiB

  1. /*
  2. getopt.c
  3. modified public-domain AT&T getopt(3)
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #ifdef _POSIX_SOURCE
  8. # include <unistd.h>
  9. #else
  10. # define STDERR_FILENO 2
  11. # ifdef __STDC__
  12. extern int write (int fildes, char * buf, unsigned nbyte);
  13. # else
  14. extern int write ();
  15. # endif
  16. #endif
  17. int opterr = 1;
  18. int optind = 1;
  19. int optopt;
  20. char * optarg;
  21. #ifdef __STDC__
  22. static void ERR (char ** argv, char * s, char c)
  23. #else
  24. static void ERR (argv, s, c)
  25. char ** argv, * s, c;
  26. #endif
  27. {
  28. char errbuf[2];
  29. #ifdef DF_TRACE_DEBUG
  30. printf("DF_TRACE_DEBUG: static void ERR () in getopt.c\n");
  31. #endif
  32. if (opterr)
  33. {
  34. errbuf[0] = c;
  35. errbuf[1] = '\n';
  36. (void) write(STDERR_FILENO,argv[0],strlen(argv[0]));
  37. (void) write(STDERR_FILENO,s,strlen(s));
  38. (void) write(STDERR_FILENO,errbuf,sizeof errbuf);
  39. }
  40. }
  41. #ifdef __STDC__
  42. int getopt (int argc, char ** argv, char * opts)
  43. #else
  44. int getopt (argc, argv, opts)
  45. int argc;
  46. char ** argv, * opts;
  47. #endif
  48. {
  49. static int sp = 1, error = (int) '?';
  50. static char sw = '-', eos = '\0', arg = ':';
  51. register char c, * cp;
  52. #ifdef DF_TRACE_DEBUG
  53. printf("DF_TRACE_DEBUG: int getopt () in getopt.c\n");
  54. #endif
  55. if (sp == 1)
  56. if (optind >= argc || argv[optind][0] != sw
  57. || argv[optind][1] == eos)
  58. return EOF;
  59. else if (strcmp(argv[optind],"--") == 0)
  60. {
  61. optind++;
  62. return EOF;
  63. }
  64. c = argv[optind][sp];
  65. optopt = (int) c;
  66. if (c == arg || (cp = strchr(opts,c)) == NULL)
  67. {
  68. ERR(argv,": illegal option--",c);
  69. if (argv[optind][++sp] == eos)
  70. {
  71. optind++;
  72. sp = 1;
  73. }
  74. return error;
  75. }
  76. else if (*++cp == arg)
  77. {
  78. if (argv[optind][sp + 1] != eos)
  79. optarg = &argv[optind++][sp + 1];
  80. else if (++optind >= argc)
  81. {
  82. ERR(argv,": option requires an argument--",c);
  83. sp = 1;
  84. return error;
  85. }
  86. else
  87. optarg = argv[optind++];
  88. sp = 1;
  89. }
  90. else
  91. {
  92. if (argv[optind][++sp] == eos)
  93. {
  94. sp = 1;
  95. optind++;
  96. }
  97. optarg = NULL;
  98. }
  99. return (int) c;
  100. }