home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / rc / part06 / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  1.6 KB  |  51 lines

  1. #include "rc.h"
  2.  
  3. int     rc_opterr = 1;
  4. int     rc_optind = 1;
  5. int     rc_optopt;
  6. char    *rc_optarg;
  7.  
  8. /* getopt routine courtesy of David Sanderson */
  9.  
  10. extern int rc_getopt(int argc, char **argv, char *opts) {
  11.         static int sp = 1;
  12.         int c;
  13.         char *cp;
  14.     if (rc_optind == 0) /* reset rc_getopt() */
  15.         rc_optind = sp = 1;
  16.         if (sp == 1)
  17.                 if (rc_optind >= argc || argv[rc_optind][0] != '-' || argv[rc_optind][1] == '\0') {
  18.                         return -1;
  19.                 } else if (strcmp(argv[rc_optind], "--") == 0) {
  20.                         rc_optind++;
  21.                         return -1;
  22.                 }
  23.         rc_optopt = c = argv[rc_optind][sp];
  24.         if (c == ':' || (cp=strchr(opts, c)) == 0) {
  25.                 fprint(2, "%s: bad option: -%c\n", argv[0], c);
  26.                 if (argv[rc_optind][++sp] == '\0') {
  27.                         rc_optind++;
  28.                         sp = 1;
  29.                 }
  30.                 return '?';
  31.         }
  32.         if (*++cp == ':') {
  33.                 if (argv[rc_optind][sp+1] != '\0') {
  34.                         rc_optarg = &argv[rc_optind++][sp+1];
  35.                 } else if (++rc_optind >= argc) {
  36.                         fprint(2, "%s: option requires an argument -- %c\n", argv[0], c);
  37.                         sp = 1;
  38.                         return '?';
  39.                 } else
  40.                         rc_optarg = argv[rc_optind++];
  41.                 sp = 1;
  42.         } else {
  43.                 if (argv[rc_optind][++sp] == '\0') {
  44.                         sp = 1;
  45.                         rc_optind++;
  46.                 }
  47.                 rc_optarg = NULL;
  48.         }
  49.         return c;
  50. }
  51.