home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / network / src_1218.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1990-01-16  |  3KB  |  105 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. #include "global.h"
  26. #define index strchr
  27. #ifdef BSD
  28. #include <strings.h>
  29. #else
  30. #include <string.h>
  31. #endif
  32.  
  33. #ifdef    __TURBOC__
  34. #include <io.h>        /* added by KA9Q for Turbo-C */
  35. #endif
  36. #ifdef    AMIGA
  37. #include <fcntl.h>
  38. #endif
  39.  
  40. /*LINTLIBRARY*/
  41. #ifndef    NULL
  42. #define NULL    0
  43. #endif
  44. #define EOF    (-1)
  45. #define ERR(s, c)    if(opterr){\
  46.     extern int write();\
  47.     char errbuf[2];\
  48.     errbuf[0] = c; errbuf[1] = '\n';\
  49.     (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
  50.     (void) write(2, s, (unsigned)strlen(s));\
  51.     (void) write(2, errbuf, 2);}
  52.  
  53. extern char *index();
  54.  
  55. int    opterr = 1;
  56. int    optind = 1;
  57. int    optopt;
  58. char    *optarg;
  59.  
  60. int
  61. getopt(argc, argv, opts)
  62. int    argc;
  63. char    **argv, *opts;
  64. {
  65.     static int sp = 1;
  66.     register int c;
  67.     register char *cp;
  68.  
  69.     if(sp == 1)
  70.         if(optind >= argc ||
  71.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  72.             return(EOF);
  73.         else if(strcmp(argv[optind], "--") == 0) {
  74.             optind++;
  75.             return(EOF);
  76.         }
  77.     optopt = c = argv[optind][sp];
  78.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  79.         ERR(": illegal option -- ", c);
  80.         if(argv[optind][++sp] == '\0') {
  81.             optind++;
  82.             sp = 1;
  83.         }
  84.         return('?');
  85.     }
  86.     if(*++cp == ':') {
  87.         if(argv[optind][sp+1] != '\0')
  88.             optarg = &argv[optind++][sp+1];
  89.         else if(++optind >= argc) {
  90.             ERR(": option requires an argument -- ", c);
  91.             sp = 1;
  92.             return('?');
  93.         } else
  94.             optarg = argv[optind++];
  95.         sp = 1;
  96.     } else {
  97.         if(argv[optind][++sp] == '\0') {
  98.             sp = 1;
  99.             optind++;
  100.         }
  101.         optarg = NULL;
  102.     }
  103.     return(c);
  104. }
  105.