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