summaryrefslogtreecommitdiff
path: root/libbb/xgetularg.c
blob: 17ba581f7201afb9b6cd7e6324a58c329ac88d0d (plain)
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
/* vi: set sw=4 ts=4: */
/*
 * xgetularg* implementations for busybox
 *
 * Copyright (C) 2003  Manuel Novoa III  <mjn3@codepoet.org>
 *
 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
 */

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <errno.h>
#include <assert.h>
#include "libbb.h"

unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
								   unsigned long lower,
								   unsigned long upper,
								   const struct suffix_mult *suffixes)
{
	unsigned long r;
	int old_errno;
	char *e;

	assert(arg);

	/* Disallow '-' and any leading whitespace.  Speed isn't critical here
	 * since we're parsing commandline args.  So make sure we get the
	 * actual isspace function rather than a larger macro implementaion. */
	if ((*arg == '-') || (isspace)(*arg)) {
		bb_show_usage();
	}

	/* Since this is a lib function, we're not allowed to reset errno to 0.
	 * Doing so could break an app that is deferring checking of errno.
	 * So, save the old value so that we can restore it if successful. */
	old_errno = errno;
	errno = 0;
	r = strtoul(arg, &e, base);
	/* Do the initial validity check.  Note: The standards do not
	 * guarantee that errno is set if no digits were found.  So we
	 * must test for this explicitly. */
	if (errno || (arg == e)) {	/* error or no digits */
		bb_show_usage();
	}
	errno = old_errno;	/* Ok.  So restore errno. */

	/* Do optional suffix parsing.  Allow 'empty' suffix tables.
	 * Note that we also all nul suffixes with associated multipliers,
	 * to allow for scaling of the arg by some default multiplier. */

	if (suffixes) {
		while (suffixes->suffix) {
			if (strcmp(suffixes->suffix, e) == 0) {
				if (ULONG_MAX / suffixes->mult < r) {	/* Overflow! */
					bb_show_usage();
				}
				++e;
				r *= suffixes->mult;
				break;
			}
			++suffixes;
		}
	}

	/* Finally, check for illegal trailing chars and range limits. */
	/* Note: although we allow leading space (via stroul), trailing space
	 * is an error.  It would be easy enough to allow though if desired. */
	if (*e || (r < lower) || (r > upper)) {
		bb_show_usage();
	}

	return r;
}

long bb_xgetlarg_bnd_sfx(const char *arg, int base,
						 long lower,
						 long upper,
						 const struct suffix_mult *suffixes)
{
	unsigned long u = LONG_MAX;
	long r;
	const char *p = arg;

	if ((*p == '-') && (p[1] != '+')) {
		++p;
		++u;	/* two's complement */
	}

	r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);

	if (*arg == '-') {
		r = -r;
	}

	if ((r < lower) || (r > upper)) {
		bb_show_usage();
	}

	return r;
}

long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
{
	return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
}

unsigned long bb_xgetularg_bnd(const char *arg, int base,
							   unsigned long lower,
							   unsigned long upper)
{
	return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
}

unsigned long bb_xgetularg10_bnd(const char *arg,
								 unsigned long lower,
								 unsigned long upper)
{
	return bb_xgetularg_bnd(arg, 10, lower, upper);
}

unsigned long bb_xgetularg10(const char *arg)
{
	return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
}