home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / pty4 / part02 / getoptquiet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-18  |  1.6 KB  |  90 lines

  1. /* XXX: this file shouldn't exist */
  2.  
  3. #include <stdio.h> /* for EOF and stderr---talk about immodularity! */
  4. #include "getoptquiet.h"
  5.  
  6. int optind = 1;
  7. int optpos = 0;
  8. int opterr = 1;
  9. char *optarg = 0;
  10. int optproblem = 0;
  11. char *optprogname = 0;
  12. int opteof = EOF;
  13.  
  14. int getopt(argc,argv,opts)
  15. int argc;
  16. char **argv;
  17. char *opts;
  18. {
  19.  int c;
  20.  char *s;
  21.  
  22.  optarg = 0;
  23.  if (!optprogname)
  24.   {
  25.    optprogname = *argv;
  26.    if (!optprogname) /* oh boy */
  27.      optprogname = ""; /*XXX*/
  28.    for (s = optprogname;*s;++s)
  29.      if (*s == '/')
  30.        optprogname = s + 1;
  31.   }
  32.  if (!argv || (optind >= argc) || !argv[optind])
  33.    return opteof;
  34.  while (optpos && !argv[optind][optpos])
  35.   {
  36.    /* we simply skip blank arguments... not any more */
  37.    ++optind;
  38.    optpos = 0;
  39.    if ((optind >= argc) || !argv[optind])
  40.      return opteof;
  41.   }
  42.  if (!optpos)
  43.   {
  44.    if (argv[optind][0] != '-')
  45.      return opteof;
  46.    ++optpos;
  47.    c = argv[optind][1];
  48.    if ((c == '-') || (c == 0))
  49.     {
  50.      /* XXX: this behavior of "-" is stupid */
  51.      if (c)
  52.        ++optind;
  53.      optpos = 0;
  54.      return opteof;
  55.     }
  56.    /* otherwise c is reassigned below */
  57.   }
  58.  c = argv[optind][optpos];
  59.  ++optpos;
  60.  s = opts;
  61.  while (*s)
  62.   {
  63.    if (c == *s)
  64.     {
  65.      if (s[1] == ':')
  66.       {
  67.        optarg = argv[optind] + optpos;
  68.        ++optind;
  69.        optpos = 0;
  70.        if (!*optarg)
  71.         {
  72.          optarg = argv[optind];
  73.          if ((optind >= argc) || !optarg) /* argument past end */
  74.           {
  75.            optproblem = c;
  76.            return '?';
  77.           }
  78.      ++optind;
  79.         }
  80.       }
  81.      return c;
  82.     }
  83.    ++s;
  84.    if (*s == ':')
  85.      ++s;
  86.   }
  87.  optproblem = c;
  88.  return '?';
  89. }
  90.