home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / 014r / pdtar.zip / GETOOPT.C < prev    next >
C/C++ Source or Header  |  1988-05-16  |  2KB  |  79 lines

  1. /*
  2.  * Plug-compatible replacement for getopt() for parsing tar-like
  3.  * arguments.  If the first argument begins with "-", it uses getopt;
  4.  * otherwise, it uses the old rules used by tar, dump, and ps.
  5.  *
  6.  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
  7.  * in the Pubic Domain for your edification and enjoyment.
  8.  * MS-DOS port 2/87 by Eric Roskos.
  9.  * Minix  port 3/88 by Eric Roskos.
  10.  *
  11.  * @(#)getoldopt.c 1.4 2/4/86 Public Domain - gnu
  12.  */
  13.  
  14. #include <stdio.h>
  15.  
  16.  
  17. int
  18. getoldopt(argc, argv, optstring)
  19. int             argc;
  20. char          **argv;
  21. char           *optstring;
  22. {
  23.     extern char    *optarg;        /* Points to next arg */
  24.     extern int      optind;        /* Global argv index */
  25.     static char    *key;        /* Points to next keyletter */
  26.     static char     use_getopt;    /* !=0 if argv[1][0] was '-' */
  27.     extern char    *index();
  28.     char            c;
  29.     char           *place;
  30.  
  31.     optarg = NULL;
  32.  
  33.     if (key == NULL)
  34.     {                            /* First time */
  35.         if (argc < 2)
  36.             return EOF;
  37.         key = argv[1];
  38.         if (*key == '-')
  39.             use_getopt++;
  40.         else
  41.             optind = 2;
  42.     }
  43.  
  44.     if (use_getopt)
  45.         return getopt(argc, argv, optstring);
  46.  
  47.     c = *key++;
  48.     if (c == '\0')
  49.     {
  50.         key--;
  51.         return EOF;
  52.     }
  53.     place = index(optstring, c);
  54.  
  55.     if (place == NULL || c == ':')
  56.     {
  57.         fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
  58.         return ('?');
  59.     }
  60.  
  61.     place++;
  62.     if (*place == ':')
  63.     {
  64.         if (optind < argc)
  65.         {
  66.             optarg = argv[optind];
  67.             optind++;
  68.         }
  69.         else
  70.         {
  71.             fprintf(stderr, "%s: %c argument missing\n",
  72.                 argv[0], c);
  73.             return ('?');
  74.         }
  75.     }
  76.  
  77.     return (c);
  78. }
  79.