1/* $OpenBSD: getoldopt.c,v 1.8 2003/07/02 21:19:33 deraadt Exp $ */ 2/* $NetBSD: getoldopt.c,v 1.3 1995/03/21 09:07:28 cgd Exp $ */ 3 4/* 5 * Plug-compatible replacement for getopt() for parsing tar-like 6 * arguments. If the first argument begins with "-", it uses getopt; 7 * otherwise, it uses the old rules used by tar, dump, and ps. 8 * 9 * Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed 10 * in the Public Domain for your edification and enjoyment. 11 */ 12 13#include <sys/cdefs.h> 14#ifndef lint 15__used static const char rcsid[] = "$OpenBSD: getoldopt.c,v 1.8 2003/07/02 21:19:33 deraadt Exp $"; 16#endif /* not lint */ 17 18#include <sys/types.h> 19#include <sys/stat.h> 20#include <stdio.h> 21#include <string.h> 22#include <unistd.h> 23#include "pax.h" 24#include "extern.h" 25 26int 27getoldopt(int argc, char **argv, const char *optstring) 28{ 29 static char *key; /* Points to next keyletter */ 30 static char use_getopt; /* !=0 if argv[1][0] was '-' */ 31 char c; 32 char *place; 33 34 optarg = NULL; 35 36 if (key == NULL) { /* First time */ 37 if (argc < 2) 38 return (-1); 39 key = argv[1]; 40 if (*key == '-') 41 use_getopt++; 42 else 43 optind = 2; 44 } 45 46 if (use_getopt) 47 return (getopt(argc, argv, optstring)); 48 49 c = *key++; 50 if (c == '\0') { 51 key--; 52 return (-1); 53 } 54 place = strchr(optstring, c); 55 56 if (place == NULL || c == ':') { 57 fprintf(stderr, "%s: unknown option %c\n", argv[0], c); 58 return ('?'); 59 } 60 61 place++; 62 if (*place == ':') { 63 if (optind < argc) { 64 optarg = argv[optind]; 65 optind++; 66 } else { 67 fprintf(stderr, "%s: %c argument missing\n", 68 argv[0], c); 69 return ('?'); 70 } 71 } 72 73 return (c); 74} 75