2010-03-30 23:14:35 +00:00
|
|
|
/* $OpenBSD: getoldopt.c,v 1.9 2009/10/27 23:59:22 deraadt Exp $ */
|
2001-05-08 06:19:06 +00:00
|
|
|
/* $NetBSD: getoldopt.c,v 1.3 1995/03/21 09:07:28 cgd Exp $ */
|
|
|
|
|
2005-01-10 08:39:26 +00:00
|
|
|
/*-
|
2001-05-08 06:19:06 +00:00
|
|
|
* Plug-compatible replacement for getopt() for parsing tar-like
|
|
|
|
* arguments. If the first argument begins with "-", it uses getopt;
|
|
|
|
* otherwise, it uses the old rules used by tar, dump, and ps.
|
|
|
|
*
|
|
|
|
* Written 25 August 1985 by John Gilmore (ihnp4!hoptoad!gnu) and placed
|
2010-03-30 23:14:35 +00:00
|
|
|
* in the Public Domain for your edification and enjoyment.
|
2001-05-08 06:19:06 +00:00
|
|
|
*/
|
|
|
|
|
2002-06-30 05:15:05 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-05-08 06:19:06 +00:00
|
|
|
|
2003-05-03 16:39:34 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2001-05-08 06:19:06 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2003-05-03 16:39:34 +00:00
|
|
|
#include "pax.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2001-05-08 06:19:06 +00:00
|
|
|
int
|
2003-05-03 16:39:34 +00:00
|
|
|
getoldopt(int argc, char **argv, const char *optstring)
|
2001-05-08 06:19:06 +00:00
|
|
|
{
|
|
|
|
static char *key; /* Points to next keyletter */
|
|
|
|
static char use_getopt; /* !=0 if argv[1][0] was '-' */
|
|
|
|
char c;
|
|
|
|
char *place;
|
|
|
|
|
|
|
|
optarg = NULL;
|
|
|
|
|
|
|
|
if (key == NULL) { /* First time */
|
2010-03-30 23:14:35 +00:00
|
|
|
if (argc < 2)
|
|
|
|
return (-1);
|
2001-05-08 06:19:06 +00:00
|
|
|
key = argv[1];
|
|
|
|
if (*key == '-')
|
|
|
|
use_getopt++;
|
|
|
|
else
|
|
|
|
optind = 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_getopt)
|
2010-03-30 23:14:35 +00:00
|
|
|
return (getopt(argc, argv, optstring));
|
2001-05-08 06:19:06 +00:00
|
|
|
|
|
|
|
c = *key++;
|
|
|
|
if (c == '\0') {
|
|
|
|
key--;
|
2010-03-30 23:14:35 +00:00
|
|
|
return (-1);
|
2001-05-08 06:19:06 +00:00
|
|
|
}
|
|
|
|
place = strchr(optstring, c);
|
|
|
|
|
|
|
|
if (place == NULL || c == ':') {
|
|
|
|
fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
|
2010-03-30 23:14:35 +00:00
|
|
|
return ('?');
|
2001-05-08 06:19:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
place++;
|
|
|
|
if (*place == ':') {
|
|
|
|
if (optind < argc) {
|
|
|
|
optarg = argv[optind];
|
|
|
|
optind++;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "%s: %c argument missing\n",
|
|
|
|
argv[0], c);
|
2010-03-30 23:14:35 +00:00
|
|
|
return ('?');
|
2001-05-08 06:19:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-30 23:14:35 +00:00
|
|
|
return (c);
|
2001-05-08 06:19:06 +00:00
|
|
|
}
|