Home | History | Annotate | Line # | Download | only in pax
getoldopt.c revision 1.1
      1  1.1  jtc /*
      2  1.1  jtc  * Plug-compatible replacement for getopt() for parsing tar-like
      3  1.1  jtc  * arguments.  If the first argument begins with "-", it uses getopt;
      4  1.1  jtc  * otherwise, it uses the old rules used by tar, dump, and ps.
      5  1.1  jtc  *
      6  1.1  jtc  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
      7  1.1  jtc  * in the Pubic Domain for your edification and enjoyment.
      8  1.1  jtc  */
      9  1.1  jtc 
     10  1.1  jtc #ifndef lint
     11  1.1  jtc static char *rcsid = "$Id: getoldopt.c,v 1.1 1994/06/14 01:16:07 jtc Exp $";
     12  1.1  jtc #endif /* not lint */
     13  1.1  jtc 
     14  1.1  jtc #include <stdio.h>
     15  1.1  jtc #include <string.h>
     16  1.1  jtc 
     17  1.1  jtc int
     18  1.1  jtc getoldopt(argc, argv, optstring)
     19  1.1  jtc 	int	argc;
     20  1.1  jtc 	char	**argv;
     21  1.1  jtc 	char	*optstring;
     22  1.1  jtc {
     23  1.1  jtc 	extern char	*optarg;	/* Points to next arg */
     24  1.1  jtc 	extern int	optind;		/* Global argv index */
     25  1.1  jtc 	static char	*key;		/* Points to next keyletter */
     26  1.1  jtc 	static char	use_getopt;	/* !=0 if argv[1][0] was '-' */
     27  1.1  jtc 	char		c;
     28  1.1  jtc 	char		*place;
     29  1.1  jtc 
     30  1.1  jtc 	optarg = NULL;
     31  1.1  jtc 
     32  1.1  jtc 	if (key == NULL) {		/* First time */
     33  1.1  jtc 		if (argc < 2) return EOF;
     34  1.1  jtc 		key = argv[1];
     35  1.1  jtc 		if (*key == '-')
     36  1.1  jtc 			use_getopt++;
     37  1.1  jtc 		else
     38  1.1  jtc 			optind = 2;
     39  1.1  jtc 	}
     40  1.1  jtc 
     41  1.1  jtc 	if (use_getopt)
     42  1.1  jtc 		return getopt(argc, argv, optstring);
     43  1.1  jtc 
     44  1.1  jtc 	c = *key++;
     45  1.1  jtc 	if (c == '\0') {
     46  1.1  jtc 		key--;
     47  1.1  jtc 		return EOF;
     48  1.1  jtc 	}
     49  1.1  jtc 	place = strchr(optstring, c);
     50  1.1  jtc 
     51  1.1  jtc 	if (place == NULL || c == ':') {
     52  1.1  jtc 		fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
     53  1.1  jtc 		return('?');
     54  1.1  jtc 	}
     55  1.1  jtc 
     56  1.1  jtc 	place++;
     57  1.1  jtc 	if (*place == ':') {
     58  1.1  jtc 		if (optind < argc) {
     59  1.1  jtc 			optarg = argv[optind];
     60  1.1  jtc 			optind++;
     61  1.1  jtc 		} else {
     62  1.1  jtc 			fprintf(stderr, "%s: %c argument missing\n",
     63  1.1  jtc 				argv[0], c);
     64  1.1  jtc 			return('?');
     65  1.1  jtc 		}
     66  1.1  jtc 	}
     67  1.1  jtc 
     68  1.1  jtc 	return(c);
     69  1.1  jtc }
     70