home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / prof_c / util / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  3.0 KB  |  104 lines

  1. /*
  2.  *      Copyright (c) 1984, 1985 AT&T
  3.  *      All Rights Reserved
  4.  *
  5.  *      ----- Author's Note -----
  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.         /* getopt() for DOS */
  17. #else
  18. #ident  "@(#)getopt.c   1.9"
  19. #endif
  20.  
  21. /*      3.0 SID #       1.2     */
  22. /*LINTLIBRARY*/
  23. #define NULL    0
  24. #define EOF     (-1)
  25.  
  26. /*
  27.  *      For this to work under versions of DOS prior to 3.00, argv[0]
  28.  *      must be set in main() to point to a valid program name or a
  29.  *      reasonable substitute string.   (ARH, 10-8-86)
  30.  */
  31. #define ERR(s, c)       if(opterr){\
  32.         char errbuf[2];\
  33.         errbuf[0] = c; errbuf[1] = '\n';\
  34.         (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  35.         (void) write(2, s, (unsigned)strlen(s));\
  36.         (void) write(2, errbuf, 2);}
  37.  
  38. #if defined (DOS_MODS)
  39. /* permit function prototyping under DOS */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #else
  43. /* standard UNIX declarations */
  44. extern int strcmp();
  45. extern char *strchr();
  46. /*
  47.  *      The following line was moved here from the ERR definition
  48.  *      to prevent a "duplicate definition" error message when the
  49.  *      code is compiled under DOS.  (ARH, 10-8-86)
  50.  */
  51. extern int strlen(), write();
  52. #endif
  53.  
  54. int     opterr = 1;
  55. int     optind = 1;
  56. int     optopt;
  57. char    *optarg;
  58.  
  59. int
  60. 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. }
  104.