Home | History | Annotate | Line # | Download | only in pax
getoldopt.c revision 1.2
      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.2  cgd static char *rcsid = "$Id: getoldopt.c,v 1.2 1994/12/04 07:11:53 cgd 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.2  cgd #include <unistd.h>
     17  1.1  jtc 
     18  1.1  jtc int
     19  1.1  jtc getoldopt(argc, argv, optstring)
     20  1.1  jtc 	int	argc;
     21  1.1  jtc 	char	**argv;
     22  1.1  jtc 	char	*optstring;
     23  1.1  jtc {
     24  1.1  jtc 	extern char	*optarg;	/* Points to next arg */
     25  1.1  jtc 	extern int	optind;		/* Global argv index */
     26  1.1  jtc 	static char	*key;		/* Points to next keyletter */
     27  1.1  jtc 	static char	use_getopt;	/* !=0 if argv[1][0] was '-' */
     28  1.1  jtc 	char		c;
     29  1.1  jtc 	char		*place;
     30  1.1  jtc 
     31  1.1  jtc 	optarg = NULL;
     32  1.1  jtc 
     33  1.1  jtc 	if (key == NULL) {		/* First time */
     34  1.1  jtc 		if (argc < 2) return EOF;
     35  1.1  jtc 		key = argv[1];
     36  1.1  jtc 		if (*key == '-')
     37  1.1  jtc 			use_getopt++;
     38  1.1  jtc 		else
     39  1.1  jtc 			optind = 2;
     40  1.1  jtc 	}
     41  1.1  jtc 
     42  1.1  jtc 	if (use_getopt)
     43  1.1  jtc 		return getopt(argc, argv, optstring);
     44  1.1  jtc 
     45  1.1  jtc 	c = *key++;
     46  1.1  jtc 	if (c == '\0') {
     47  1.1  jtc 		key--;
     48  1.1  jtc 		return EOF;
     49  1.1  jtc 	}
     50  1.1  jtc 	place = strchr(optstring, c);
     51  1.1  jtc 
     52  1.1  jtc 	if (place == NULL || c == ':') {
     53  1.1  jtc 		fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
     54  1.1  jtc 		return('?');
     55  1.1  jtc 	}
     56  1.1  jtc 
     57  1.1  jtc 	place++;
     58  1.1  jtc 	if (*place == ':') {
     59  1.1  jtc 		if (optind < argc) {
     60  1.1  jtc 			optarg = argv[optind];
     61  1.1  jtc 			optind++;
     62  1.1  jtc 		} else {
     63  1.1  jtc 			fprintf(stderr, "%s: %c argument missing\n",
     64  1.1  jtc 				argv[0], c);
     65  1.1  jtc 			return('?');
     66  1.1  jtc 		}
     67  1.1  jtc 	}
     68  1.1  jtc 
     69  1.1  jtc 	return(c);
     70  1.1  jtc }
     71