home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tools / make / tcmak / getopt.c < prev    next >
Text File  |  1988-06-20  |  2KB  |  100 lines

  1. /* getopt.c -- command line option/argument parser.
  2. *  From AT&T UNIX System Toolchest
  3. */
  4.  
  5. #define  DOS_MODS 1
  6.  
  7. #if defined (DOS_MODS)
  8.    /* Program name set by main() */
  9.    extern char *Pgm;
  10. #else
  11. ident "@(#)getopt.c   1.9"
  12. #endif
  13.  
  14. /*     3.0 SID #      1.2"     */
  15. /* LINTLIBRARY */
  16. #define  NULL  0
  17. #define  EOF   (-1)
  18. #define  ERR(s, c)   if(opterr){\
  19.          char errbuf[2];\
  20.          errbuf[0] = c; errbuf[1] = '\n';\
  21.          (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  22.          (void) write(2, s, (unsigned)strlen(s));\
  23.          (void) write(2, errbuf, 2);}
  24.  
  25. #if defined (DOS_MODS)
  26. /* permit funtion prototyping under DOS */
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #else
  30. /* standard UNIX declarations */
  31. extern int strcmp();
  32. extern char *strchr();
  33. /*
  34. *     The following line was moved here from the ERR definition to
  35. *     prevent a "duplicate definition" error message when the code
  36. *     is compiled under DOS.
  37. */
  38. extern int strlen(), write();
  39. #endif
  40.  
  41. int   opterr = 1;
  42. int   optind = 1;
  43. int   optopt;
  44. char  *optarg;
  45.  
  46. int   getopt(argc, argv, opts)
  47. int   argc;
  48. char **argv, *opts;
  49.    {
  50.    static int sp = 1;
  51.    register int c;
  52.    register char *cp;
  53.    if(sp == 1)
  54.       if(optind >= argc ||
  55.          argv[optind][0] != '-' || argv[optind][1] == '\0')
  56.             return(EOF);
  57.       else if(strcmp(argv[optind], "--") == NULL)
  58.          {
  59.          optind++;
  60.          return(EOF);
  61.          }
  62.    optopt = c = argv[optind][sp];
  63.    if(c == ':' || (cp = strchr(opts, c)) == NULL)
  64.       {
  65.       ERR(": illegal option -- ", c);
  66.       if(argv[optind][++sp] == '\0')
  67.          {
  68.          optind++;
  69.          sp = 1;
  70.          }
  71.       return('?');
  72.       }
  73.    if(*++cp == ':')
  74.       {
  75.       if(argv[optind][sp+1] != '\0')
  76.          optarg = &argv[optind++][sp+1];
  77.       else
  78.          if(++optind >= argc)
  79.             {
  80.             ERR(": option requires an argument -- ", c);
  81.             sp = 1;
  82.             return('?');
  83.             }
  84.          else
  85.             optarg = argv[optind++];
  86.       sp = 1;
  87.       }
  88.    else
  89.       {
  90.       if(argv[optind][++sp] == '\0')
  91.          {
  92.          sp = 1;
  93.          optind++;
  94.          }
  95.       optarg = NULL;
  96.       }
  97.    return(c);
  98.    }
  99.  
  100.