1*d47295ccSrillig /* $NetBSD: getoldopt.c,v 1.24 2024/10/03 20:14:01 rillig Exp $ */ 249f0ad86Scgd 3eb066159Sjtc /* 4eb066159Sjtc * Plug-compatible replacement for getopt() for parsing tar-like 5eb066159Sjtc * arguments. If the first argument begins with "-", it uses getopt; 6eb066159Sjtc * otherwise, it uses the old rules used by tar, dump, and ps. 7eb066159Sjtc * 8eb066159Sjtc * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed 926db98beSjtc * in the Public Domain for your edification and enjoyment. 10eb066159Sjtc */ 11eb066159Sjtc 12171d6532Slukem #if HAVE_NBTOOL_CONFIG_H 13171d6532Slukem #include "nbtool_config.h" 14171d6532Slukem #endif 15171d6532Slukem 16f3cd6022Schristos #include <sys/cdefs.h> 17171d6532Slukem #if !defined(lint) 18*d47295ccSrillig __RCSID("$NetBSD: getoldopt.c,v 1.24 2024/10/03 20:14:01 rillig Exp $"); 19eb066159Sjtc #endif /* not lint */ 20eb066159Sjtc 21b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H 22b2f78261Sjmc #include "compat_getopt.h" 23b2f78261Sjmc #else 249fbd8888Stv #include <getopt.h> 25b2f78261Sjmc #endif 26eb066159Sjtc #include <stdio.h> 27eb066159Sjtc #include <string.h> 280c612021Schristos #include <stdlib.h> 295dad1439Scgd #include <unistd.h> 306d8547fdSchristos #include <stdint.h> 31f3cd6022Schristos #include <sys/stat.h> 32f3cd6022Schristos #include "pax.h" 33f3cd6022Schristos #include "extern.h" 34eb066159Sjtc 35eb066159Sjtc int 36c1bd745cSlukem getoldopt(int argc, char **argv, const char *optstring, 3733b90eebSlukem struct option *longopts, int *idx) 38eb066159Sjtc { 39eb066159Sjtc static char *key; /* Points to next keyletter */ 40d1a05abeSchristos static char use_getopt; /* !=0 if argv[1][0] was '-' */ 41d1a05abeSchristos char c; 42*d47295ccSrillig const char *place; 43d1a05abeSchristos 44d1a05abeSchristos optarg = NULL; 45eb066159Sjtc 46eb066159Sjtc if (key == NULL) { /* First time */ 47332c413bSlukem if (argc < 2) return -1; 48eb066159Sjtc key = argv[1]; 49d1a05abeSchristos if (*key == '-') 50d1a05abeSchristos use_getopt++; 51d1a05abeSchristos else 52d1a05abeSchristos optind = 2; 53eb066159Sjtc } 54eb066159Sjtc 557bbdd188Schs c = '\0'; 56d1a05abeSchristos if (!use_getopt) { 57d1a05abeSchristos c = *key++; 58d1a05abeSchristos if (c == '\0') { 59d1a05abeSchristos key--; 60d1a05abeSchristos use_getopt = 1; 61d1a05abeSchristos } 62d1a05abeSchristos } 63d1a05abeSchristos if (use_getopt) { 640c612021Schristos if (longopts != NULL) { 650c612021Schristos return getopt_long(argc, argv, optstring, 660c612021Schristos longopts, idx); 670c612021Schristos } else { 680c612021Schristos return getopt(argc, argv, optstring); 690c612021Schristos } 700c612021Schristos } 71d1a05abeSchristos 72d1a05abeSchristos place = strchr(optstring, c); 73d1a05abeSchristos 74d1a05abeSchristos if (place == NULL || c == ':') { 75d1a05abeSchristos fprintf(stderr, "%s: unknown option %c\n", argv[0], c); 76cdec4ac1Sdsl return '?'; 77d1a05abeSchristos } 78d1a05abeSchristos 79d1a05abeSchristos place++; 80d1a05abeSchristos if (*place == ':') { 81d1a05abeSchristos if (optind < argc) { 82d1a05abeSchristos optarg = argv[optind]; 83d1a05abeSchristos optind++; 84d1a05abeSchristos } else { 85d1a05abeSchristos fprintf(stderr, "%s: %c argument missing\n", 86d1a05abeSchristos argv[0], c); 87cdec4ac1Sdsl return '?'; 88d1a05abeSchristos } 89d1a05abeSchristos } 90d1a05abeSchristos 91cdec4ac1Sdsl return c; 92d1a05abeSchristos } 93