Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.07 KB | None | 0 0
  1. #include    <stdio.h>
  2. #define EPR                 fprintf(stderr,
  3. #define ERR(str, chr)       if(opterr){EPR "%s%c\n", str, chr);}
  4. int     opterr = 1;
  5. int     optind = 1;
  6. int optopt;
  7. char    *optarg;
  8. char    *strchr();
  9.  
  10. int
  11. getopt (int argc, char *const argv[], const char *opts)
  12. {
  13.     static int sp = 1;
  14.     int c;
  15.     char *cp;
  16.  
  17.     if (sp == 1)
  18.         if (optind >= argc ||
  19.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  20.             return -1;
  21.         else if (strcmp(argv[optind], "--") == 0) {
  22.             optind++;
  23.             return -1;
  24.         }
  25.     optopt = c = argv[optind][sp];
  26.     if (c == ':' || (cp=strchr(opts, c)) == 0) {
  27.         ERR (": illegal option -- ", c);
  28.         if (argv[optind][++sp] == '\0') {
  29.             optind++;
  30.             sp = 1;
  31.         }
  32.         return '?';
  33.     }
  34.     if (*++cp == ':') {
  35.         if (argv[optind][sp+1] != '\0')
  36.             optarg = &argv[optind++][sp+1];
  37.         else if (++optind >= argc) {
  38.             ERR (": option requires an argument -- ", c);
  39.             sp = 1;
  40.             return '?';
  41.         } else
  42.             optarg = argv[optind++];
  43.         sp = 1;
  44.     } else {
  45.         if (argv[optind][++sp] == '\0') {
  46.             sp = 1;
  47.             optind++;
  48.         }
  49.         optarg = 0;
  50.     }
  51.     return c;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement