home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / GETOPT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-12  |  859 b   |  54 lines

  1. /*
  2.  * getopt.c
  3.  *
  4.  * Replacement for a Unix style getopt function
  5.  *
  6.  */
  7.  
  8. #include "frotz.h"
  9. #include "s5api.h"
  10.  
  11. short getopt (struct sg *g, short argc, char *argv[], const char *options)
  12. {
  13.     const char *p;
  14.  
  15.     if (g->optind >= argc || argv[g->optind][0] != '-' || argv[g->optind][1] == 0)
  16.     return 0;
  17.  
  18.     g->optopt = argv[g->optind][g->pos++];
  19.     g->optarg = NULL;
  20.  
  21.     if (argv[g->optind][g->pos] == 0)
  22.     { g->pos = 1; g->optind++; }
  23.  
  24.     p = Srvstrchr (options, (char)g->optopt);
  25.  
  26.     if (g->optopt == ':' || p == NULL) {
  27.  
  28.     goto error;
  29.  
  30.     } else if (p[1] == ':')
  31.  
  32.     if (g->optind >= argc) {
  33.  
  34.         goto error;
  35.  
  36.     } else {
  37.  
  38.         g->optarg = argv[g->optind];
  39.  
  40.         if (g->pos != 1)
  41.         g->optarg += g->pos;
  42.  
  43.         g->pos = 1; g->optind++;
  44.  
  45.     }
  46.  
  47.     return g->optopt;
  48.  
  49. error:
  50.  
  51.     return '?';
  52.  
  53. }/* getopt */
  54.