blob: d743520c3918bea437aab5a1a6145e33bb6262f9 [file] [log] [blame]
Manuel Novoa III cad53642003-03-19 09:13:01 +00001/* vi: set sw=4 ts=4: */
2/*
3 * xgetularg* implementations for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <stdlib.h>
24#include <string.h>
25#include <limits.h>
26#include <ctype.h>
27#include <errno.h>
28#include <assert.h>
29#include "libbb.h"
30
31#ifdef L_xgetularg_bnd_sfx
32extern
33unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
34 unsigned long lower,
35 unsigned long upper,
36 const struct suffix_mult *suffixes)
37{
38 unsigned long r;
39 int old_errno;
40 char *e;
41
42 assert(arg);
43
44 /* Disallow '-' and any leading whitespace. Speed isn't critical here
45 * since we're parsing commandline args. So make sure we get the
46 * actual isspace function rather than a larger macro implementaion. */
47 if ((*arg == '-') || (isspace)(*arg)) {
48 bb_show_usage();
49 }
50
51 /* Since this is a lib function, we're not allowed to reset errno to 0.
52 * Doing so could break an app that is deferring checking of errno.
53 * So, save the old value so that we can restore it if successful. */
54 old_errno = errno;
55 errno = 0;
56 r = strtoul(arg, &e, base);
57 /* Do the initial validity check. Note: The standards do not
58 * guarantee that errno is set if no digits were found. So we
59 * must test for this explicitly. */
60 if (errno || (arg == e)) { /* error or no digits */
61 bb_show_usage();
62 }
63 errno = old_errno; /* Ok. So restore errno. */
64
65 /* Do optional suffix parsing. Allow 'empty' suffix tables.
66 * Note that we also all nul suffixes with associated multipliers,
67 * to allow for scaling of the arg by some default multiplier. */
68
69 if (suffixes) {
70 while (suffixes->suffix) {
71 if (strcmp(suffixes->suffix, e) == 0) {
72 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
73 bb_show_usage();
74 }
75 ++e;
76 r *= suffixes->mult;
77 break;
78 }
79 ++suffixes;
80 }
81 }
82
83 /* Finally, check for illegal trailing chars and range limits. */
84 /* Note: although we allow leading space (via stroul), trailing space
85 * is an error. It would be easy enough to allow though if desired. */
86 if (*e || (r < lower) || (r > upper)) {
87 bb_show_usage();
88 }
89
90 return r;
91}
92#endif
93
94#ifdef L_xgetlarg_bnd_sfx
95extern
96long bb_xgetlarg_bnd_sfx(const char *arg, int base,
97 long lower,
98 long upper,
99 const struct suffix_mult *suffixes)
100{
101 unsigned long u = LONG_MAX;
102 long r;
103 const char *p = arg;
104
105 if ((*p == '-') && (p[1] != '+')) {
106 ++p;
107#if LONG_MAX == (-(LONG_MIN + 1))
108 ++u; /* two's complement */
109#endif
110 }
111
112 r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
113
114 if (*arg == '-') {
115 r = -r;
116 }
117
118 if ((r < lower) || (r > upper)) {
119 bb_show_usage();
120 }
121
122 return r;
123}
124#endif
125
126#ifdef L_getlarg10_sfx
127extern
128long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
129{
130 return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
131}
132#endif
133
134#ifdef L_xgetularg_bnd
135extern
136unsigned long bb_xgetularg_bnd(const char *arg, int base,
137 unsigned long lower,
138 unsigned long upper)
139{
140 return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
141}
142#endif
143
144#ifdef L_xgetularg10_bnd
145extern
146unsigned long bb_xgetularg10_bnd(const char *arg,
147 unsigned long lower,
148 unsigned long upper)
149{
150 return bb_xgetularg_bnd(arg, 10, lower, upper);
151}
152#endif
153
154#ifdef L_xgetularg10
155extern
156unsigned long bb_xgetularg10(const char *arg)
157{
158 return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
159}
160#endif