forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_getlong.c
151 lines (140 loc) · 3.57 KB
/
db_getlong.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1996-2003
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char revid[] = "$Id: db_getlong.c,v 11.20 2003/01/08 04:07:46 bostic Exp $";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "db_int.h"
#include "logmsg.h"
/*
* __db_getlong --
* Return a long value inside of basic parameters.
*
* PUBLIC: int __db_getlong
* PUBLIC: __P((DB_ENV *, const char *, char *, long, long, long *));
*/
int
__db_getlong(dbenv, progname, p, min, max, storep)
DB_ENV *dbenv;
const char *progname;
char *p;
long min, max, *storep;
{
long val;
char *end;
__os_set_errno(0);
val = strtol(p, &end, 10);
if ((val == LONG_MIN || val == LONG_MAX) &&
__os_get_errno() == ERANGE) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR,
"%s: %s: %s\n", progname, p, strerror(ERANGE));
else
dbenv->err(dbenv, ERANGE, "%s", p);
return (1);
}
if (p[0] == '\0' || (end[0] != '\0' && end[0] != '\n')) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR, "%s: %s: Invalid numeric argument\n", progname, p);
else
dbenv->errx(dbenv, "%s: Invalid numeric argument", p);
return (1);
}
if (val < min) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR,
"%s: %s: Less than minimum value (%ld)\n",
progname, p, min);
else
dbenv->errx(dbenv,
"%s: Less than minimum value (%ld)", p, min);
return (1);
}
if (val > max) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR, "%s: %s: Greater than maximum value (%ld)\n",
progname, p, max);
else
dbenv->errx(dbenv,
"%s: Greater than maximum value (%ld)", p, max);
return (1);
}
*storep = val;
return (0);
}
/*
* __db_getulong --
* Return an unsigned long value inside of basic parameters.
*
* PUBLIC: int __db_getulong
* PUBLIC: __P((DB_ENV *, const char *, char *, u_long, u_long, u_long *));
*/
int
__db_getulong(dbenv, progname, p, min, max, storep)
DB_ENV *dbenv;
const char *progname;
char *p;
u_long min, max, *storep;
{
#if !defined(HAVE_STRTOUL)
COMPQUIET(min, 0);
return (__db_getlong(dbenv, progname, p, 0, max, (long *)storep));
#else
u_long val;
char *end;
__os_set_errno(0);
val = strtoul(p, &end, 10);
if (val == ULONG_MAX && __os_get_errno() == ERANGE) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR, "%s: %s: %s\n", progname, p, strerror(ERANGE));
else
dbenv->err(dbenv, ERANGE, "%s", p);
return (1);
}
if (p[0] == '\0' || (end[0] != '\0' && end[0] != '\n')) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR, "%s: %s: Invalid numeric argument\n", progname, p);
else
dbenv->errx(dbenv, "%s: Invalid numeric argument", p);
return (1);
}
if (val < min) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR,
"%s: %s: Less than minimum value (%lu)\n",
progname, p, min);
else
dbenv->errx(dbenv,
"%s: Less than minimum value (%lu)", p, min);
return (1);
}
/*
* We allow a 0 to substitute as a max value for ULONG_MAX because
* 1) accepting only a 0 value is unlikely to be necessary, and 2)
* we don't want callers to have to use ULONG_MAX explicitly, as it
* may not exist on all platforms.
*/
if (max != 0 && val > max) {
if (dbenv == NULL)
logmsg(LOGMSG_ERROR,
"%s: %s: Greater than maximum value (%lu)\n",
progname, p, max);
else
dbenv->errx(dbenv,
"%s: Greater than maximum value (%lu)", p, max);
return (1);
}
*storep = val;
return (0);
#endif /* !defined(HAVE_STRTOUL) */
}