home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / CC21.ZIP / GETOPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-27  |  1.8 KB  |  73 lines

  1. /*
  2.  * AT&T's public domain getopt().
  3.  *
  4.  * This was FTPed from comp.sources.unix archives, vol 3 (Feb '86)
  5.  *    as att_getopt.c. These archives can be found on UUNET.UU.NET.
  6.  *
  7.  * Code modified to support ANSI function prototypes, GAT, 29-Oct-89.
  8.  *
  9.  * Modifications to provide prototypes for all functions, GAT, 27-Jul-90.
  10.  */
  11.  
  12. /*LINTLIBRARY*/
  13. #define NULL   0
  14. #define EOF    (-1)
  15. #define ERR(s, c)    if(opterr){\
  16.    extern int strlen(char *), write(int, char *, unsigned int);\
  17.    char errbuf[2];\
  18.    errbuf[0] = c; errbuf[1] = '\n';\
  19.    (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  20.    (void) write(2, s, (unsigned)strlen(s));\
  21.    (void) write(2, errbuf, 2);}
  22.  
  23. extern int strcmp(char *, char *);
  24. extern char *strchr(char *, int);
  25.  
  26. int   opterr = 1;
  27. int   optind = 1;
  28. int   optopt;
  29. char  *optarg;
  30.  
  31. int getopt(int argc, char **argv, char *opts)
  32. {
  33.    static int sp = 1;
  34.    register int c;
  35.    register char *cp;
  36.  
  37.    if(sp == 1)
  38.       if(optind >= argc ||
  39.             argv[optind][0] != '-' || argv[optind][1] == '\0')
  40.          return(EOF);
  41.       else if(strcmp(argv[optind], "--") == NULL) {
  42.          optind++;
  43.          return(EOF);
  44.       }
  45.    optopt = c = argv[optind][sp];
  46.    if(c == ':' || (cp=strchr(opts, c)) == NULL) {
  47.       ERR(": illegal option -- ", c);
  48.       if(argv[optind][++sp] == '\0') {
  49.          optind++;
  50.          sp = 1;
  51.       }
  52.       return('?');
  53.    }
  54.    if(*++cp == ':') {
  55.       if(argv[optind][sp+1] != '\0')
  56.          optarg = &argv[optind++][sp+1];
  57.       else if(++optind >= argc) {
  58.          ERR(": option requires an argument -- ", c);
  59.          sp = 1;
  60.          return('?');
  61.       } else
  62.          optarg = argv[optind++];
  63.       sp = 1;
  64.    } else {
  65.       if(argv[optind][++sp] == '\0') {
  66.          sp = 1;
  67.          optind++;
  68.       }
  69.       optarg = NULL;
  70.    }
  71.    return(c);
  72. }
  73.