home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / tnlogin.zip / getopt.c < prev    next >
C/C++ Source or Header  |  1992-08-13  |  2KB  |  89 lines

  1. /*
  2. **    @(#)getopt.c    2.5 (smail) 9/15/87
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <io.h>
  7. #include <string.h>
  8.  
  9. /*
  10.  * Here's something you've all been waiting for:  the AT&T public domain
  11.  * source for getopt(3).  It is the code which was given out at the 1985
  12.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  13.  * directly from AT&T.  The people there assure me that it is indeed
  14.  * in the public domain.
  15.  *
  16.  * There is no manual page.  That is because the one they gave out at
  17.  * UNIFORUM was slightly different from the current System V Release 2
  18.  * manual page.  The difference apparently involved a note about the
  19.  * famous rules 5 and 6, recommending using white space between an option
  20.  * and its first argument, and not grouping options that have arguments.
  21.  * Getopt itself is currently lenient about both of these things.  White
  22.  * space is allowed, but not mandatory, and the last option in a group can
  23.  * have an argument.  That particular version of the man page evidently
  24.  * has no official existence, and my source at AT&T did not send a copy.
  25.  * The current SVR2 man page reflects the actual behavor of this getopt.
  26.  * However, I am not about to post a copy of anything licensed by AT&T.
  27.  */
  28.  
  29. #define ERR(s, c)\
  30.   if(opterr){\
  31.     char errbuf[2];\
  32.     errbuf[0] = (char)c; errbuf[1] = '\n';\
  33.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  34.     (void) write(2, s, (unsigned)strlen(s));\
  35.     (void) write(2, errbuf, 2);\
  36.   }
  37.  
  38. int    opterr = 1;
  39. int    optind = 1;
  40. int    optopt;
  41. char    *optarg;
  42.  
  43. int
  44. getopt(argc, argv, opts)
  45. int    argc;
  46. char    **argv, *opts;
  47. {
  48.     static int sp = 1;
  49.     register int c;
  50.     register char *cp;
  51.  
  52.     if(sp == 1)
  53.         if(optind >= argc ||
  54.            argv[optind][0] != '-' /* && argv[optind][0] != '/' */ ||
  55.            argv[optind][1] == '\0')
  56.             return(EOF);
  57.         else if(strcmp(argv[optind], "--") == 0) {
  58.             optind++;
  59.             return(EOF);
  60.         }
  61.     optopt = c = argv[optind][sp];
  62.     if(c == ':' || (cp=strchr(opts, c)) == 0) {
  63.         ERR(": illegal option -- ", c);
  64.         if(argv[optind][++sp] == '\0') {
  65.             optind++;
  66.             sp = 1;
  67.         }
  68.         return('?');
  69.     }
  70.     if(*++cp == ':') {
  71.         if(argv[optind][sp+1] != '\0')
  72.             optarg = &argv[optind++][sp+1];
  73.         else if(++optind >= argc) {
  74.             ERR(": option requires an argument -- ", c);
  75.             sp = 1;
  76.             return('?');
  77.         } else
  78.             optarg = argv[optind++];
  79.         sp = 1;
  80.     } else {
  81.         if(argv[optind][++sp] == '\0') {
  82.             sp = 1;
  83.             optind++;
  84.         }
  85.         optarg = NULL;
  86.     }
  87.     return(c);
  88. }
  89.