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