home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 216.lha / EdLib_v1.0 / getopt.c < prev    next >
C/C++ Source or Header  |  1996-02-15  |  2KB  |  70 lines

  1. /* edlib  version 1.0 of 04/08/88 */
  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.                         return(EOF);
  32.                 else {
  33.                     if(argv[optind][0] != '-') {
  34.                         optarg = argv[optind++];
  35.                         return(NULL);
  36.                     }
  37.                     if(strcmp(argv[optind], "--") == NULL) {
  38.                         optind++;
  39.                         return(EOF);
  40.                     }
  41.                 }
  42.         optopt = c = argv[optind][sp];
  43.         if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  44.                 ERR(": illegal option -- ", c);
  45.                 if(argv[optind][++sp] == '\0') {
  46.                         optind++;
  47.                         sp = 1;
  48.                 }
  49.                 return('?');
  50.         }
  51.         if(*++cp == ':') {
  52.                 if(argv[optind][sp+1] != '\0')
  53.                         optarg = &argv[optind++][sp+1];
  54.                 else if(++optind >= argc) {
  55.                         ERR(": option requires an argument -- ", c);
  56.                         sp = 1;
  57.                         return('?');
  58.                 } else
  59.                         optarg = argv[optind++];
  60.                 sp = 1;
  61.         } else {
  62.                 if(argv[optind][++sp] == '\0') {
  63.                         sp = 1;
  64.                         optind++;
  65.                 }
  66.                 optarg = NULL;
  67.         }
  68.         return(c);
  69. }
  70.