Home | History | Annotate | Line # | Download | only in pax
getoldopt.c revision 1.15
      1 /*	$NetBSD: getoldopt.c,v 1.15 2002/10/12 15:39:29 christos Exp $	*/
      2 
      3 /*
      4  * Plug-compatible replacement for getopt() for parsing tar-like
      5  * arguments.  If the first argument begins with "-", it uses getopt;
      6  * otherwise, it uses the old rules used by tar, dump, and ps.
      7  *
      8  * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
      9  * in the Public Domain for your edification and enjoyment.
     10  */
     11 
     12 #include <sys/cdefs.h>
     13 #if defined(__RCSID) && !defined(lint)
     14 __RCSID("$NetBSD: getoldopt.c,v 1.15 2002/10/12 15:39:29 christos Exp $");
     15 #endif /* not lint */
     16 
     17 #include <getopt.h>
     18 #include <stdio.h>
     19 #include <string.h>
     20 #include <stdlib.h>
     21 #include <unistd.h>
     22 #include <sys/stat.h>
     23 #include "pax.h"
     24 #include "extern.h"
     25 
     26 int
     27 getoldopt(int argc, char **argv, const char *optstring,
     28 	struct option *longopts, int *idx)
     29 {
     30 	static char	*key;		/* Points to next keyletter */
     31 	static char	use_getopt;	/* !=0 if argv[1][0] was '-' */
     32 	char		c;
     33 	char		*place;
     34 
     35 	optarg = NULL;
     36 
     37 	if (key == NULL) {		/* First time */
     38 		if (argc < 2) return -1;
     39 		key = argv[1];
     40 		if (*key == '-')
     41 			use_getopt++;
     42 		else
     43 			optind = 2;
     44 	}
     45 
     46 	if (use_getopt) {
     47 		if (longopts != NULL) {
     48 			/*
     49 			 * Setting POSIXLY_CORRECT here, makes getopt_long
     50 			 * stop argument processing at the first non-option.
     51 			 */
     52 			setenv("POSIXLY_CORRECT", "", 0);
     53 			return getopt_long(argc, argv, optstring,
     54 			    longopts, idx);
     55 		} else {
     56 			return getopt(argc, argv, optstring);
     57 		}
     58 	}
     59 
     60 	c = *key++;
     61 	if (c == '\0') {
     62 		key--;
     63 		return -1;
     64 	}
     65 	place = strchr(optstring, c);
     66 
     67 	if (place == NULL || c == ':') {
     68 		fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
     69 		return('?');
     70 	}
     71 
     72 	place++;
     73 	if (*place == ':') {
     74 		if (optind < argc) {
     75 			optarg = argv[optind];
     76 			optind++;
     77 		} else {
     78 			fprintf(stderr, "%s: %c argument missing\n",
     79 				argv[0], c);
     80 			return('?');
     81 		}
     82 	}
     83 
     84 	return(c);
     85 }
     86