1 /* $OpenBSD: getoldopt.c,v 1.4 2000/01/22 20:24:51 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 Pubic Domain for your edification and enjoyment. 11 */ 12 13 #ifndef lint 14 static const char rcsid[] = "$FreeBSD$"; 15 #endif /* not lint */ 16 17 #include <stdio.h> 18 #include <string.h> 19 #include <unistd.h> 20 21 int 22 getoldopt(int argc, char **argv, char *optstring) 23 { 24 static char *key; /* Points to next keyletter */ 25 static char use_getopt; /* !=0 if argv[1][0] was '-' */ 26 char c; 27 char *place; 28 29 optarg = NULL; 30 31 if (key == NULL) { /* First time */ 32 if (argc < 2) return EOF; 33 key = argv[1]; 34 if (*key == '-') 35 use_getopt++; 36 else 37 optind = 2; 38 } 39 40 if (use_getopt) 41 return getopt(argc, argv, optstring); 42 43 c = *key++; 44 if (c == '\0') { 45 key--; 46 return EOF; 47 } 48 place = strchr(optstring, c); 49 50 if (place == NULL || c == ':') { 51 fprintf(stderr, "%s: unknown option %c\n", argv[0], c); 52 return('?'); 53 } 54 55 place++; 56 if (*place == ':') { 57 if (optind < argc) { 58 optarg = argv[optind]; 59 optind++; 60 } else { 61 fprintf(stderr, "%s: %c argument missing\n", 62 argv[0], c); 63 return('?'); 64 } 65 } 66 67 return(c); 68 } 69