]> git.saurik.com Git - apple/file_cmds.git/blame - pax/getoldopt.c
file_cmds-272.tar.gz
[apple/file_cmds.git] / pax / getoldopt.c
CommitLineData
864a4b6e 1/* $OpenBSD: getoldopt.c,v 1.8 2003/07/02 21:19:33 deraadt Exp $ */
44a7a5ab
A
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
864a4b6e 10 * in the Public Domain for your edification and enjoyment.
44a7a5ab
A
11 */
12
00337e45 13#include <sys/cdefs.h>
44a7a5ab 14#ifndef lint
00337e45 15__used static const char rcsid[] = "$OpenBSD: getoldopt.c,v 1.8 2003/07/02 21:19:33 deraadt Exp $";
44a7a5ab
A
16#endif /* not lint */
17
864a4b6e
A
18#include <sys/types.h>
19#include <sys/stat.h>
44a7a5ab
A
20#include <stdio.h>
21#include <string.h>
22#include <unistd.h>
864a4b6e
A
23#include "pax.h"
24#include "extern.h"
44a7a5ab
A
25
26int
864a4b6e 27getoldopt(int argc, char **argv, const char *optstring)
44a7a5ab 28{
44a7a5ab
A
29 static char *key; /* Points to next keyletter */
30 static char use_getopt; /* !=0 if argv[1][0] was '-' */
31 char c;
32 char *place;
33
34 optarg = NULL;
35
36 if (key == NULL) { /* First time */
864a4b6e
A
37 if (argc < 2)
38 return (-1);
44a7a5ab
A
39 key = argv[1];
40 if (*key == '-')
41 use_getopt++;
42 else
43 optind = 2;
44 }
45
46 if (use_getopt)
864a4b6e 47 return (getopt(argc, argv, optstring));
44a7a5ab
A
48
49 c = *key++;
50 if (c == '\0') {
51 key--;
864a4b6e 52 return (-1);
44a7a5ab
A
53 }
54 place = strchr(optstring, c);
55
56 if (place == NULL || c == ':') {
57 fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
864a4b6e 58 return ('?');
44a7a5ab
A
59 }
60
61 place++;
62 if (*place == ':') {
63 if (optind < argc) {
64 optarg = argv[optind];
65 optind++;
66 } else {
67 fprintf(stderr, "%s: %c argument missing\n",
68 argv[0], c);
864a4b6e 69 return ('?');
44a7a5ab
A
70 }
71 }
72
864a4b6e 73 return (c);
44a7a5ab 74}