home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / netio113.zip / getopt.c < prev    next >
C/C++ Source or Header  |  1999-10-24  |  3KB  |  93 lines

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