home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / sqmurd01.zip / getopt.c < prev    next >
C/C++ Source or Header  |  1995-12-06  |  3KB  |  89 lines

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