getoldopt.c revision 1.17
1/* $NetBSD: getoldopt.c,v 1.17 2003/06/23 13:15:15 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.17 2003/06/23 13:15:15 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 26int 27getoldopt(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 argv1[64]; 32 33 if (key == NULL) { /* First time */ 34 if (argc < 2) return -1; 35 key = argv[1]; 36 if (*key != '-') 37 (void)snprintf(argv[1] = argv1, sizeof(argv1), "-%s", 38 key); 39 } 40 41 if (longopts != NULL) { 42 return getopt_long(argc, argv, optstring, 43 longopts, idx); 44 } else { 45 return getopt(argc, argv, optstring); 46 } 47} 48