getoldopt.c revision 1.10
1/* $NetBSD: getoldopt.c,v 1.10 2000/07/04 17:17:49 thorpej 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#ifndef lint 14__RCSID("$NetBSD: getoldopt.c,v 1.10 2000/07/04 17:17:49 thorpej Exp $"); 15#endif /* not lint */ 16 17#include <stdio.h> 18#include <string.h> 19#include <unistd.h> 20#include <getopt.h> 21#include <sys/stat.h> 22#include "pax.h" 23#include "extern.h" 24 25int 26getoldopt(argc, argv, optstring, longopts, index) 27 int argc; 28 char **argv; 29 const char *optstring; 30 struct option *longopts; 31 int *index; 32{ 33 static char *key; /* Points to next keyletter */ 34 static char use_getopt; /* !=0 if argv[1][0] was '-' */ 35 char c; 36 char *place; 37 38 optarg = NULL; 39 40 if (key == NULL) { /* First time */ 41 if (argc < 2) return -1; 42 key = argv[1]; 43 if (*key == '-') 44 use_getopt++; 45 else 46 optind = 2; 47 } 48 49 if (use_getopt) 50 return ((longopts != NULL) ? 51 getopt_long(argc, argv, optstring, longopts, index) : 52 getopt(argc, argv, optstring)); 53 54 c = *key++; 55 if (c == '\0') { 56 key--; 57 return -1; 58 } 59 place = strchr(optstring, c); 60 61 if (place == NULL || c == ':') { 62 fprintf(stderr, "%s: unknown option %c\n", argv[0], c); 63 return('?'); 64 } 65 66 place++; 67 if (*place == ':') { 68 if (optind < argc) { 69 optarg = argv[optind]; 70 optind++; 71 } else { 72 fprintf(stderr, "%s: %c argument missing\n", 73 argv[0], c); 74 return('?'); 75 } 76 } 77 78 return(c); 79} 80