~vlad-lesin/percona-server/mysql-5.0.33-original

« back to all changes in this revision

Viewing changes to bdb/common/db_getlong.c

  • Committer: Vlad Lesin
  • Date: 2012-07-31 09:21:34 UTC
  • Revision ID: vladislav.lesin@percona.com-20120731092134-zfodx022b7992wsi
VirginĀ 5.0.33

Show diffs side-by-side

added added

removed removed

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