gdb - Local mods (compile)
[dragonfly.git] / bin / pax / getoldopt.c
CommitLineData
984263bc
MD
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.
1de703da
MD
11 *
12 * $FreeBSD: src/bin/pax/getoldopt.c,v 1.1.2.1 2001/08/01 05:03:11 obrien Exp $
b5744197 13 * $DragonFly: src/bin/pax/getoldopt.c,v 1.3 2003/09/28 14:39:14 hmp Exp $
984263bc
MD
14 */
15
984263bc
MD
16#include <stdio.h>
17#include <string.h>
18#include <unistd.h>
19
20int
b5744197 21getoldopt(int argc, char **argv, char *optstring)
984263bc
MD
22{
23 static char *key; /* Points to next keyletter */
24 static char use_getopt; /* !=0 if argv[1][0] was '-' */
25 char c;
26 char *place;
27
28 optarg = NULL;
29
30 if (key == NULL) { /* First time */
31 if (argc < 2) return EOF;
32 key = argv[1];
33 if (*key == '-')
34 use_getopt++;
35 else
36 optind = 2;
37 }
38
39 if (use_getopt)
40 return getopt(argc, argv, optstring);
41
42 c = *key++;
43 if (c == '\0') {
44 key--;
45 return EOF;
46 }
47 place = strchr(optstring, c);
48
49 if (place == NULL || c == ':') {
50 fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
51 return('?');
52 }
53
54 place++;
55 if (*place == ':') {
56 if (optind < argc) {
57 optarg = argv[optind];
58 optind++;
59 } else {
60 fprintf(stderr, "%s: %c argument missing\n",
61 argv[0], c);
62 return('?');
63 }
64 }
65
66 return(c);
67}