home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part03 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-19  |  3.2 KB  |  124 lines

  1. /* getopt.c, getopt.h: (yet another) improved getopt clone
  2. Daniel J. Bernstein, brnstnd@nyu.edu.
  3. No dependencies.
  4. Requires stdio.
  5. 10/20/91: Removed -DGETOPT_SHUT_UP. [sigh]
  6. 8/26/91: Added -DGETOPT_SHUT_UP.
  7. 8/25/91: Changed getopt to not skip blank arguments.
  8. 7/6/91: Baseline. getopt 1.0, public domain.
  9. No known patent problems.
  10.  
  11. This is a clone of the usual getopt() functions. It includes opterr so
  12. that you can turn off error handling, optpos so that you can find out
  13. exactly where the processing is up to (instead of just which argument),
  14. optproblem so that you can accurately diagnose errors yourself, and
  15. optprogname so that you can set the program name for getopt-generated
  16. errors. getopt() takes much more care to ensure that all the variables
  17. still make sense upon errors and EOF. (optproblem is a character. If
  18. argv[optind] is 0, the problem is a missing argument; otherwise it's an
  19. illegal option character.) Unless you define GETOPTORIGDEF, the header
  20. file redefines all names so that you don't have to worry about conflicts
  21. with libc. Finally, the code is public-domain, so you should feel free
  22. to use these extra features in your own programs and just attach a copy
  23. of this.
  24.  
  25. Note that optind and optpos can be read (or set) any time, but the
  26. official getopt interface only defines optind when getopt() returns EOF.
  27. We define optproblem only when getopt() returns '?', optarg all the
  28. time, and optprogram only after getopt() has been called at least once.
  29.  
  30. */
  31.  
  32. #include <stdio.h> /* for EOF and stderr---talk about immodularity! */
  33. #include "getopt.h"
  34.  
  35. int optind = 1;
  36. int optpos = 0;
  37. int opterr = 1;
  38. char *optarg = 0;
  39. int optproblem = 0;
  40. char *optprogname = 0;
  41. int opteof = EOF;
  42.  
  43. int getopt(argc,argv,opts)
  44. int argc;
  45. char **argv;
  46. char *opts;
  47. {
  48.  int c;
  49.  char *s;
  50.  
  51.  optarg = 0;
  52.  if (!optprogname)
  53.   {
  54.    optprogname = *argv;
  55.    if (!optprogname) /* oh boy */
  56.      optprogname = ""; /*XXX*/
  57.    for (s = optprogname;*s;++s)
  58.      if (*s == '/')
  59.        optprogname = s + 1;
  60.   }
  61.  if (!argv || (optind >= argc) || !argv[optind])
  62.    return opteof;
  63.  while (optpos && !argv[optind][optpos])
  64.   {
  65.    /* we simply skip blank arguments... not any more */
  66.    ++optind;
  67.    optpos = 0;
  68.    if ((optind >= argc) || !argv[optind])
  69.      return opteof;
  70.   }
  71.  if (!optpos)
  72.   {
  73.    if (argv[optind][0] != '-')
  74.      return opteof;
  75.    ++optpos;
  76.    c = argv[optind][1];
  77.    if ((c == '-') || (c == 0))
  78.     {
  79.      /* XXX: this behavior of "-" is stupid */
  80.      if (c)
  81.        ++optind;
  82.      optpos = 0;
  83.      return opteof;
  84.     }
  85.    /* otherwise c is reassigned below */
  86.   }
  87.  c = argv[optind][optpos];
  88.  ++optpos;
  89.  s = opts;
  90.  while (*s)
  91.   {
  92.    if (c == *s)
  93.     {
  94.      if (s[1] == ':')
  95.       {
  96.        optarg = argv[optind] + optpos;
  97.        ++optind;
  98.        optpos = 0;
  99.        if (!*optarg)
  100.         {
  101.          optarg = argv[optind];
  102.          if ((optind >= argc) || !optarg) /* argument past end */
  103.           {
  104.            optproblem = c;
  105.            if (opterr)
  106.              fprintf(stderr,"%s: option requires an argument -- %c\n"
  107.                ,optprogname,c);
  108.            return '?';
  109.           }
  110.      ++optind;
  111.         }
  112.       }
  113.      return c;
  114.     }
  115.    ++s;
  116.    if (*s == ':')
  117.      ++s;
  118.   }
  119.  optproblem = c;
  120.  if (opterr)
  121.    fprintf(stderr,"%s: illegal option -- %c\n",optprogname,c);
  122.  return '?';
  123. }
  124.