blob: 21b0d12f7d219cbeaa9f39021a8307f2e7bb1dc0 [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 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Manuel Novoa III cad53642003-03-19 09:13:01 +00008 */
9
10#include <stdlib.h>
11#include <string.h>
12#include <limits.h>
13#include <ctype.h>
14#include <errno.h>
15#include <assert.h>
16#include "libbb.h"
17
18#ifdef L_xgetularg_bnd_sfx
Manuel Novoa III cad53642003-03-19 09:13:01 +000019unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
20 unsigned long lower,
21 unsigned long upper,
22 const struct suffix_mult *suffixes)
23{
24 unsigned long r;
25 int old_errno;
26 char *e;
27
28 assert(arg);
29
30 /* Disallow '-' and any leading whitespace. Speed isn't critical here
31 * since we're parsing commandline args. So make sure we get the
32 * actual isspace function rather than a larger macro implementaion. */
33 if ((*arg == '-') || (isspace)(*arg)) {
34 bb_show_usage();
35 }
36
37 /* Since this is a lib function, we're not allowed to reset errno to 0.
38 * Doing so could break an app that is deferring checking of errno.
39 * So, save the old value so that we can restore it if successful. */
40 old_errno = errno;
41 errno = 0;
42 r = strtoul(arg, &e, base);
43 /* Do the initial validity check. Note: The standards do not
44 * guarantee that errno is set if no digits were found. So we
45 * must test for this explicitly. */
46 if (errno || (arg == e)) { /* error or no digits */
47 bb_show_usage();
48 }
49 errno = old_errno; /* Ok. So restore errno. */
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 /* Do optional suffix parsing. Allow 'empty' suffix tables.
52 * Note that we also all nul suffixes with associated multipliers,
53 * to allow for scaling of the arg by some default multiplier. */
54
55 if (suffixes) {
56 while (suffixes->suffix) {
57 if (strcmp(suffixes->suffix, e) == 0) {
58 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
59 bb_show_usage();
60 }
61 ++e;
62 r *= suffixes->mult;
63 break;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000064 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000065 ++suffixes;
66 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000067 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000068
69 /* Finally, check for illegal trailing chars and range limits. */
70 /* Note: although we allow leading space (via stroul), trailing space
71 * is an error. It would be easy enough to allow though if desired. */
72 if (*e || (r < lower) || (r > upper)) {
73 bb_show_usage();
74 }
75
76 return r;
77}
78#endif
79
80#ifdef L_xgetlarg_bnd_sfx
Manuel Novoa III cad53642003-03-19 09:13:01 +000081long bb_xgetlarg_bnd_sfx(const char *arg, int base,
82 long lower,
83 long upper,
84 const struct suffix_mult *suffixes)
85{
86 unsigned long u = LONG_MAX;
87 long r;
88 const char *p = arg;
89
90 if ((*p == '-') && (p[1] != '+')) {
91 ++p;
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 ++u; /* two's complement */
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 }
94
95 r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
96
97 if (*arg == '-') {
98 r = -r;
99 }
100
101 if ((r < lower) || (r > upper)) {
102 bb_show_usage();
103 }
104
105 return r;
106}
107#endif
108
109#ifdef L_getlarg10_sfx
Manuel Novoa III cad53642003-03-19 09:13:01 +0000110long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
111{
112 return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
113}
114#endif
115
116#ifdef L_xgetularg_bnd
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117unsigned long bb_xgetularg_bnd(const char *arg, int base,
118 unsigned long lower,
119 unsigned long upper)
120{
121 return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
122}
123#endif
124
125#ifdef L_xgetularg10_bnd
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126unsigned long bb_xgetularg10_bnd(const char *arg,
127 unsigned long lower,
128 unsigned long upper)
129{
130 return bb_xgetularg_bnd(arg, 10, lower, upper);
131}
132#endif
133
134#ifdef L_xgetularg10
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135unsigned long bb_xgetularg10(const char *arg)
136{
137 return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
138}
139#endif