Leaked source code of windows server 2003
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.

77 lines
1.7 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. static void ERR(char **argv, char *s, char c)
  22. {
  23. if (opterr) {
  24. fprintf(stderr, "%s%s%c\n", argv[0], s, c);
  25. }
  26. }
  27. int getopt(int argc, char **argv, char *opts)
  28. {
  29. static int sp = 1, error = (int) '?';
  30. static char sw = '-', eos = '\0', arg = ':';
  31. char c, * cp;
  32. if (sp == 1)
  33. if (optind >= argc || argv[optind][0] != sw
  34. || argv[optind][1] == eos)
  35. return EOF;
  36. else if (strcmp(argv[optind],"--") == 0) {
  37. optind++;
  38. return EOF;
  39. }
  40. c = argv[optind][sp];
  41. optopt = (int) c;
  42. if (c == arg || (cp = strchr(opts,c)) == NULL) {
  43. ERR(argv,": illegal option: -",c);
  44. if (argv[optind][++sp] == eos) {
  45. optind++;
  46. sp = 1;
  47. }
  48. return error;
  49. }
  50. else if (*++cp == arg) {
  51. if (argv[optind][sp + 1] != eos)
  52. optarg = &argv[optind++][sp + 1];
  53. else if (++optind >= argc) {
  54. ERR(argv,": option requires an argument--",c);
  55. sp = 1;
  56. return error;
  57. }
  58. else
  59. optarg = argv[optind++];
  60. sp = 1;
  61. }
  62. else {
  63. if (argv[optind][++sp] == eos) {
  64. sp = 1;
  65. optind++;
  66. }
  67. optarg = NULL;
  68. }
  69. return (int)c;
  70. }