home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / misc / cal.lha / opt_utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-17  |  1.2 KB  |  63 lines

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