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.

78 lines
1.6 KiB

  1. /* got this off net.sources */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "getopt.h"
  5. /*
  6. * get option letter from argument vector
  7. */
  8. int
  9. opterr = 1, // should error messages be printed?
  10. optind = 1, // index into parent argv vector
  11. optopt; // character checked for validity
  12. char
  13. *optarg; // argument associated with option
  14. #define EMSG ""
  15. char *progname; // may also be defined elsewhere
  16. static void
  17. error(char *pch)
  18. {
  19. if (!opterr) {
  20. return; // without printing
  21. }
  22. fprintf(stderr, "%s: %s: %c\n",
  23. (NULL != progname) ? progname : "getopt", pch, optopt);
  24. }
  25. int
  26. getopt(int argc, char **argv, char *ostr)
  27. {
  28. static char *place = EMSG; /* option letter processing */
  29. register char *oli; /* option letter list index */
  30. if (!*place) {
  31. // update scanning pointer
  32. if (optind >= argc || *(place = argv[optind]) != '-' || !*++place) {
  33. return EOF;
  34. }
  35. if (*place == '-') {
  36. // found "--"
  37. ++optind;
  38. return EOF;
  39. }
  40. }
  41. /* option letter okay? */
  42. if ((optopt = (int)*place++) == (int)':'
  43. || !(oli = strchr(ostr, optopt))) {
  44. if (!*place) {
  45. ++optind;
  46. }
  47. error("illegal option");
  48. return BADCH;
  49. }
  50. if (*++oli != ':') {
  51. /* don't need argument */
  52. optarg = NULL;
  53. if (!*place)
  54. ++optind;
  55. } else {
  56. /* need an argument */
  57. if (*place) {
  58. optarg = place; /* no white space */
  59. } else if (argc <= ++optind) {
  60. /* no arg */
  61. place = EMSG;
  62. error("option requires an argument");
  63. return BADCH;
  64. } else {
  65. optarg = argv[optind]; /* white space */
  66. }
  67. place = EMSG;
  68. ++optind;
  69. }
  70. return optopt; // return option letter
  71. }