getopt.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <unistd.h>
  2. #include <wchar.h>
  3. #include <string.h>
  4. #include <limits.h>
  5. #include <stdlib.h>
  6. char *optarg;
  7. int optind=1, opterr=1, optopt;
  8. static int optpos;
  9. int getopt(int argc, char * const argv[], const char *optstring)
  10. {
  11. int i;
  12. wchar_t c, d;
  13. int k, l;
  14. char *optchar;
  15. if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
  16. return -1;
  17. if (argv[optind][1] == '-' && !argv[optind][2])
  18. return optind++, -1;
  19. if (!optpos) optpos++;
  20. if ((k = mbtowc(&c, argv[optind]+optpos, MB_LEN_MAX)) < 0) {
  21. k = 1;
  22. c = 0xfffd; /* replacement char */
  23. }
  24. optchar = argv[optind]+optpos;
  25. optopt = c;
  26. optpos += k;
  27. if (!argv[optind][optpos]) {
  28. optind++;
  29. optpos = 0;
  30. }
  31. for (i=0; (l = mbtowc(&d, optstring+i, MB_LEN_MAX)) && d!=c; i+=l>0?l:1);
  32. if (d != c) {
  33. if (optstring[0] != ':' && opterr) {
  34. write(2, argv[0], strlen(argv[0]));
  35. write(2, ": illegal option: ", 18);
  36. write(2, optchar, k);
  37. write(2, "\n", 1);
  38. }
  39. return '?';
  40. }
  41. if (optstring[i+1] == ':') {
  42. if (optind >= argc) {
  43. if (optstring[0] == ':') return ':';
  44. if (opterr) {
  45. write(2, argv[0], strlen(argv[0]));
  46. write(2, ": option requires an argument: ", 31);
  47. write(2, optchar, k);
  48. write(2, "\n", 1);
  49. }
  50. return '?';
  51. }
  52. optarg = argv[optind++] + optpos;
  53. optpos = 0;
  54. }
  55. return c;
  56. }