diff options
Diffstat (limited to 'archivers/pax')
-rw-r--r-- | archivers/pax/files/getoldopt.c | 57 |
1 files changed, 46 insertions, 11 deletions
diff --git a/archivers/pax/files/getoldopt.c b/archivers/pax/files/getoldopt.c index 9f595ff984c..21af930b81d 100644 --- a/archivers/pax/files/getoldopt.c +++ b/archivers/pax/files/getoldopt.c @@ -1,4 +1,4 @@ -/* $NetBSD: getoldopt.c,v 1.2 2003/06/23 13:39:01 grant Exp $ */ +/* $NetBSD: getoldopt.c,v 1.3 2003/06/30 11:45:40 grant Exp $ */ /* * Plug-compatible replacement for getopt() for parsing tar-like @@ -16,7 +16,7 @@ #endif #if defined(__RCSID) && !defined(lint) -__RCSID("$NetBSD: getoldopt.c,v 1.2 2003/06/23 13:39:01 grant Exp $"); +__RCSID("$NetBSD: getoldopt.c,v 1.3 2003/06/30 11:45:40 grant Exp $"); #endif /* not lint */ #include <getopt.h> @@ -33,20 +33,55 @@ getoldopt(int argc, char **argv, const char *optstring, struct option *longopts, int *idx) { static char *key; /* Points to next keyletter */ - static char argv1[64]; + static char use_getopt; /* !=0 if argv[1][0] was '-' */ + char c; + char *place; + + optarg = NULL; if (key == NULL) { /* First time */ if (argc < 2) return -1; key = argv[1]; - if (*key != '-') - (void)snprintf(argv[1] = argv1, sizeof(argv1), "-%s", - key); + if (*key == '-') + use_getopt++; + else + optind = 2; + } + + if (!use_getopt) { + c = *key++; + if (c == '\0') { + key--; + use_getopt = 1; + } + } + if (use_getopt) { + if (longopts != NULL) { + return getopt_long(argc, argv, optstring, + longopts, idx); + } else { + return getopt(argc, argv, optstring); + } } - if (longopts != NULL) { - return getopt_long(argc, argv, optstring, - longopts, idx); - } else { - return getopt(argc, argv, optstring); + place = strchr(optstring, c); + + if (place == NULL || c == ':') { + fprintf(stderr, "%s: unknown option %c\n", argv[0], c); + return('?'); + } + + place++; + if (*place == ':') { + if (optind < argc) { + optarg = argv[optind]; + optind++; + } else { + fprintf(stderr, "%s: %c argument missing\n", + argv[0], c); + return('?'); + } } + + return(c); } |