home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / BRIK.ZIP / GETOPT.C < prev    next >
C/C++ Source or Header  |  1989-03-11  |  3KB  |  102 lines

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