home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / MODEM / UWPC201.ZIP / UWSERVER.TAR / misc / getopt.c next >
Encoding:
C/C++ Source or Header  |  1991-01-25  |  1.2 KB  |  65 lines

  1. /*LINTLIBRARY*/
  2. #define NULL    0
  3. #define EOF    (-1)
  4. #define ERR(s, c)    if(opterr){\
  5.     extern int strlen(), write();\
  6.     char errbuf[2];\
  7.     errbuf[0] = c; errbuf[1] = '\n';\
  8.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  9.     (void) write(2, s, (unsigned)strlen(s));\
  10.     (void) write(2, errbuf, 2);}
  11.  
  12. extern int strcmp();
  13. extern char *strchr();
  14.  
  15. int    opterr = 1;
  16. int    optind = 1;
  17. int    optopt;
  18. char    *optarg;
  19.  
  20. int
  21. getopt(argc, argv, opts)
  22. int    argc;
  23. char    **argv, *opts;
  24. {
  25.     static int sp = 1;
  26.     register int c;
  27.     register char *cp;
  28.  
  29.     if(sp == 1)
  30.         if(optind >= argc ||
  31.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  32.             return(EOF);
  33.         else if(strcmp(argv[optind], "--") == NULL) {
  34.             optind++;
  35.             return(EOF);
  36.         }
  37.     optopt = c = argv[optind][sp];
  38.     if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  39.         ERR(": illegal option -- ", c);
  40.         if(argv[optind][++sp] == '\0') {
  41.             optind++;
  42.             sp = 1;
  43.         }
  44.         return('?');
  45.     }
  46.     if(*++cp == ':') {
  47.         if(argv[optind][sp+1] != '\0')
  48.             optarg = &argv[optind++][sp+1];
  49.         else if(++optind >= argc) {
  50.             ERR(": option requires an argument -- ", c);
  51.             sp = 1;
  52.             return('?');
  53.         } else
  54.             optarg = argv[optind++];
  55.         sp = 1;
  56.     } else {
  57.         if(argv[optind][++sp] == '\0') {
  58.             sp = 1;
  59.             optind++;
  60.         }
  61.         optarg = NULL;
  62.     }
  63.     return(c);
  64. }
  65.