1#include "config.h"
2#include <stdio.h>
3#include <ctype.h>
4#include "sdbm.h"
5
6#ifdef HAVE_SYS_FILE_H
7#include <sys/file.h>
8#endif
9
10#ifdef HAVE_UNISTD_H
11#include <unistd.h>
12#endif
13#include <errno.h>
14extern int errno;
15
16void
17oops(s1, s2)
18register char *s1;
19register char *s2;
20{
21#ifndef HAVE_STRERROR
22	extern int sys_nerr;
23	extern char *sys_errlist[];
24#endif
25	extern char *progname;
26
27	if (progname)
28		fprintf(stderr, "%s: ", progname);
29	fprintf(stderr, s1, s2);
30#ifndef HAVE_STRERROR
31	if (errno > 0 && errno < sys_nerr)
32		fprintf(stderr, " (%s)", sys_errlist[errno]);
33#else
34	if (errno > 0)
35		fprintf(stderr, " (%s)", strerror(errno));
36#endif
37	fprintf(stderr, "\n");
38	exit(1);
39}
40
41int
42okpage(char *pag)
43{
44	register unsigned n;
45	register off;
46	register short *ino = (short *) pag;
47
48	if ((n = ino[0]) > PBLKSIZ / sizeof(short))
49		return 0;
50
51	if (!n)
52		return 1;
53
54	off = PBLKSIZ;
55	for (ino++; n; ino += 2) {
56		if (ino[0] > off || ino[1] > off ||
57		    ino[1] > ino[0])
58			return 0;
59		off = ino[1];
60		n -= 2;
61	}
62
63	return 1;
64}
65
66
67
68/***************************************************************************\
69**                                                                         **
70**   Function name: getopt()                                               **
71**   Author:        Henry Spencer, UofT                                    **
72**   Coding date:   84/04/28                                               **
73**                                                                         **
74**   Description:                                                          **
75**                                                                         **
76**   Parses argv[] for arguments.                                          **
77**   Works with Whitesmith's C compiler.                                   **
78**                                                                         **
79**   Inputs   - The number of arguments                                    **
80**            - The base address of the array of arguments                 **
81**            - A string listing the valid options (':' indicates an       **
82**              argument to the preceding option is required, a ';'        **
83**              indicates an argument to the preceding option is optional) **
84**                                                                         **
85**   Outputs  - Returns the next option character,                         **
86**              '?' for non '-' arguments                                  **
87**              or ':' when there is no more arguments.                    **
88**                                                                         **
89**   Side Effects + The argument to an option is pointed to by 'optarg'    **
90**                                                                         **
91*****************************************************************************
92**                                                                         **
93**   REVISION HISTORY:                                                     **
94**                                                                         **
95**     DATE           NAME                        DESCRIPTION              **
96**   YY/MM/DD  ------------------   ------------------------------------   **
97**   88/10/20  Janick Bergeron      Returns '?' on unamed arguments        **
98**                                  returns '!' on unknown options         **
99**                                  and 'EOF' only when exhausted.         **
100**   88/11/18  Janick Bergeron      Return ':' when no more arguments      **
101**   89/08/11  Janick Bergeron      Optional optarg when ';' in optstring  **
102**                                                                         **
103\***************************************************************************/
104
105char *optarg;			       /* Global argument pointer. */
106
107#ifdef VMS
108#define index  strchr
109#endif
110
111#ifndef HAVE_GETOPT
112char
113getopt(argc, argv, optstring)
114int argc;
115char **argv;
116char *optstring;
117{
118	register int c;
119	register char *place;
120	extern char *index();
121	static int optind = 0;
122	static char *scan = NULL;
123
124	optarg = NULL;
125
126	if (scan == NULL || *scan == '\0') {
127
128		if (optind == 0)
129			optind++;
130		if (optind >= argc)
131			return ':';
132
133		optarg = place = argv[optind++];
134		if (place[0] != '-' || place[1] == '\0')
135			return '?';
136		if (place[1] == '-' && place[2] == '\0')
137			return '?';
138		scan = place + 1;
139	}
140
141	c = *scan++;
142	place = index(optstring, c);
143	if (place == NULL || c == ':' || c == ';') {
144
145		(void) fprintf(stderr, "%s: unknown option %c\n", argv[0], c);
146		scan = NULL;
147		return '!';
148	}
149	if (*++place == ':') {
150
151		if (*scan != '\0') {
152
153			optarg = scan;
154			scan = NULL;
155
156		}
157		else {
158
159			if (optind >= argc) {
160
161				(void) fprintf(stderr, "%s: %c requires an argument\n",
162					       argv[0], c);
163				return '!';
164			}
165			optarg = argv[optind];
166			optind++;
167		}
168	}
169	else if (*place == ';') {
170
171		if (*scan != '\0') {
172
173			optarg = scan;
174			scan = NULL;
175
176		}
177		else {
178
179			if (optind >= argc || *argv[optind] == '-')
180				optarg = NULL;
181			else {
182				optarg = argv[optind];
183				optind++;
184			}
185		}
186	}
187	return c;
188}
189#endif
190