home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / ELM23-2 / ELM23-2.ZIP / os2 / getopt.c < prev    next >
C/C++ Source or Header  |  1992-10-04  |  3KB  |  95 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. #if defined(__STDC__) || defined(USG) || defined(MSDOS) || defined(VMS)
  30. #define index strchr
  31. #endif
  32.  
  33. #define ERR(s, c)\
  34.   if(opterr){\
  35.     char errbuf[2];\
  36.     errbuf[0] = (char)c; errbuf[1] = '\n';\
  37.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  38.     (void) write(2, s, (unsigned)strlen(s));\
  39.     (void) write(2, errbuf, 2);\
  40.   }
  41.  
  42. extern char *index();
  43.  
  44. int    opterr = 1;
  45. int    optind = 1;
  46. int    optopt = 0;
  47. char    *optarg = 0;
  48.  
  49. int
  50. getopt(argc, argv, opts)
  51. int    argc;
  52. char    **argv, *opts;
  53. {
  54.     static int sp = 1;
  55.     register int c;
  56.     register char *cp;
  57.  
  58.     if(sp == 1)
  59.         if(optind >= argc ||
  60.            argv[optind][0] != '-' && argv[optind][0] != '/' ||
  61.            argv[optind][1] == '\0')
  62.             return(EOF);
  63.         else if(strcmp(argv[optind], "--") == 0) {
  64.             optind++;
  65.             return(EOF);
  66.         }
  67.     optopt = c = argv[optind][sp];
  68.     if(c == ':' || (cp=index(opts, c)) == 0) {
  69.         ERR(": illegal option -- ", c);
  70.         if(argv[optind][++sp] == '\0') {
  71.             optind++;
  72.             sp = 1;
  73.         }
  74.         return('?');
  75.     }
  76.     if(*++cp == ':') {
  77.         if(argv[optind][sp+1] != '\0')
  78.             optarg = &argv[optind++][sp+1];
  79.         else if(++optind >= argc) {
  80.             ERR(": option requires an argument -- ", c);
  81.             sp = 1;
  82.             return('?');
  83.         } else
  84.             optarg = argv[optind++];
  85.         sp = 1;
  86.     } else {
  87.         if(argv[optind][++sp] == '\0') {
  88.             sp = 1;
  89.             optind++;
  90.         }
  91.         optarg = NULL;
  92.     }
  93.     return(c);
  94. }
  95.