home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 024 / psi110g.zip / GETOPT.C < prev    next >
C/C++ Source or Header  |  1994-04-17  |  3KB  |  103 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. #endif
  30.   
  31. #ifdef  __TURBOC__
  32. #include <io.h>     /* added by KA9Q for Turbo-C */
  33. #endif
  34. #ifdef  AMIGA
  35. #include <fcntl.h>
  36. #endif
  37.   
  38. /*LINTLIBRARY*/
  39. #ifndef NULL
  40. #define NULL    0
  41. #endif
  42. #define EOF (-1)
  43. #ifdef LINUX
  44. extern int write __ARGS((int,void*,unsigned));
  45. #endif
  46. #define ERR(s, c)   if(opterr){\
  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. int opterr = 1;
  54. int optind = 1;
  55. int optopt;
  56. char    *optarg;
  57.   
  58. int
  59. getopt(argc, argv, opts)
  60. int argc;
  61. char    **argv, *opts;
  62. {
  63.     static int sp = 1;
  64.     register int c;
  65.     register char *cp;
  66.   
  67.     if(sp == 1)
  68.         if(optind >= argc ||
  69.             argv[optind][0] != '-' || argv[optind][1] == '\0')
  70.             return(EOF);
  71.         else if(strcmp(argv[optind], "--") == 0) {
  72.             optind++;
  73.             return(EOF);
  74.         }
  75.     optopt = c = argv[optind][sp];
  76.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  77.         ERR(": illegal option -- ", c);
  78.         if(argv[optind][++sp] == '\0') {
  79.             optind++;
  80.             sp = 1;
  81.         }
  82.         return('?');
  83.     }
  84.     if(*++cp == ':') {
  85.         if(argv[optind][sp+1] != '\0')
  86.             optarg = &argv[optind++][sp+1];
  87.         else if(++optind >= argc) {
  88.             ERR(": option requires an argument -- ", c);
  89.             sp = 1;
  90.             return('?');
  91.         } else
  92.             optarg = argv[optind++];
  93.         sp = 1;
  94.     } else {
  95.         if(argv[optind][++sp] == '\0') {
  96.             sp = 1;
  97.             optind++;
  98.         }
  99.         optarg = NULL;
  100.     }
  101.     return(c);
  102. }
  103.