home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS 1992 June / SIMTEL_0692.cdr / msdos / comptbls / attclock.arc / GETOPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-10  |  2.8 KB  |  103 lines

  1. /*
  2.  *      Copyright (c) 1984, 1985 AT&T
  3.  *      All Rights Reserved
  4.  *
  5.  *      -- Note on Pg 127 of Proficient C, Augie Hanson, Microsoft Press --
  6.  *      getopt() is reproduced with permission of the AT&T UNIX(R) System
  7.  *      Toolchest.  This is a public domain version of getopt(3) that is
  8.  *      distributed to registered Toolchest participants.
  9.  *      Defining DOS_MODS alters the code slightly to obtain compatibility
  10.  *      with DOS and support libraries provided with most DOS C compilers.
  11.  */
  12.  
  13. #define DOS_MODS
  14.  
  15. #if defined (DOS_MODS)
  16.     /* program name set by main() */
  17.     extern char *Pgm;
  18. #else
  19. /* #ident "@(#)getopt.c   1.9" */
  20. #endif
  21.  
  22. /*     3.0 SID #       1.2"    */
  23. /*LINTLIBRARY*/
  24. #define NULL    0
  25. #define EOF     (-1)
  26.  
  27. /*
  28.  *      For this to work under versions of DOS prior to 3.00, argv[0]
  29.  *      must be set in main() to point to a valid program name or a
  30.  *      reasonable substitute string.  (ARH 10-8-86)
  31.  */
  32. #define ERR(s, c)      if (opterr){\
  33.         char errbuf[2];\
  34.         errbuf[0] = c; errbuf[1] = '\n';\
  35.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  36.         (void) write(2, s, (unsigned)strlen(s));\
  37.         (void) write(2, errbuf, 2);}
  38.  
  39. #if defined (DOS_MODS)
  40. /* permit function prototyping under DOS */
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #else
  44. /* standard UNIX declarations */
  45. extern int strcpy();
  46. extern char *strchr();
  47. /*
  48.  *      The following line was moved here from the ERR definition
  49.  *      to prevent a "duplicate definition" error message when the
  50.  *      code is compiled under DOS.  (ARH, 10-8-86)
  51.  */
  52.  extern int strlen(), write();
  53.  #endif
  54.  
  55. int    opterr = 1;
  56. int    optind = 1;
  57. int    optopt;
  58. char   *optarg;
  59.  
  60. int getopt(argc, argv, opts)
  61. int argc;
  62. char **argv, *opts;
  63. {
  64.     static int sp = 1;
  65.     register int c;
  66.     register char *cp;
  67.  
  68.     if (sp == 1)
  69.         if (optind >= argc ||
  70.             argv[optind][0] != '-' || argv[optind][1] == '\0')
  71.                 return(EOF);
  72.         else if (strcmp(argv[optind], "--") == NULL) {
  73.             optind++;
  74.             return(EOF);
  75.         }
  76.     optopt = c = argv[optind][sp];
  77.     if (c == ':' || (cp = strchr(opts, c)) == NULL) {
  78.         ERR(": illegal option -- ", c);
  79.         if (argv[optind][++sp] == '\0') {
  80.             optind++;
  81.             sp = 1;
  82.         }
  83.         return('?');
  84.     }
  85.     if (*++cp == ':') {
  86.         if (argv[optind][sp+1] != '\0')
  87.             optarg = &argv[optind++][sp+1];
  88.         else if (++optind >= argc) {
  89.             ERR(": option requires an argument -- ", c);
  90.             sp = 1;
  91.             return('?');
  92.         } else
  93.             optarg = argv[optind++];
  94.         sp = 1;
  95.     } else {
  96.         if (argv[optind][++sp] == '\0') {
  97.             sp = 1;
  98.             optind++;
  99.         }
  100.         optarg = NULL;
  101.    }
  102.    return(c);
  103. }