home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / pgmutil / idtag.zip / GETOPT.C next >
C/C++ Source or Header  |  1989-09-27  |  3KB  |  98 lines

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