summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/Kbuild2
-rw-r--r--libbb/xatol.c236
-rw-r--r--libbb/xatonum.c91
-rw-r--r--libbb/xatonum_template.c175
4 files changed, 267 insertions, 237 deletions
diff --git a/libbb/Kbuild b/libbb/Kbuild
index fc4798b..1ddec9a 100644
--- a/libbb/Kbuild
+++ b/libbb/Kbuild
@@ -87,7 +87,7 @@ lib-y += vperror_msg.o
lib-y += warn_ignoring_args.o
lib-y += wfopen.o
lib-y += wfopen_input.o
-lib-y += xatol.o
+lib-y += xatonum.o
lib-y += xconnect.o
lib-y += xfuncs.o
lib-y += xgetcwd.o
diff --git a/libbb/xatol.c b/libbb/xatol.c
deleted file mode 100644
index cce8ad3..0000000
--- a/libbb/xatol.c
+++ /dev/null
@@ -1,236 +0,0 @@
-/* vi: set sw=4 ts=4: */
-/*
- * ascii-to-numbers implementations for busybox
- *
- * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
- *
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
- */
-
-#include "libbb.h"
-
-unsigned long long xstrtoull(const char *numstr, int base)
-{
- unsigned long long r;
- int old_errno;
- char *e;
- if ((*numstr == '-') || (isspace)(*numstr))
- bb_error_msg_and_die("invalid number '%s'", numstr);
- old_errno = errno;
- errno = 0;
- r = strtoull(numstr, &e, base);
- if (errno || (numstr == e) || *e)
- /* Error / no digits / illegal trailing chars */
- bb_error_msg_and_die("invalid number '%s'", numstr);
- /* No error. So restore errno. */
- errno = old_errno;
- return r;
-}
-
-unsigned long long xatoull(const char *numstr)
-{
- return xstrtoull(numstr, 10);
-}
-
-unsigned long xstrtoul_range_sfx(const char *numstr, int base,
- unsigned long lower,
- unsigned long upper,
- const struct suffix_mult *suffixes)
-{
- unsigned long r;
- int old_errno;
- char *e;
-
- /* 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 lnumstrer macro implementaion. */
- if ((*numstr == '-') || (isspace)(*numstr))
- goto inval;
-
- /* 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(numstr, &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 || (numstr == e))
- goto inval; /* error / no digits / illegal trailing chars */
-
- errno = old_errno; /* Ok. So restore errno. */
-
- /* Do optional suffix parsing. Allow 'empty' suffix tables.
- * Note that we also allow nul suffixes with associated multipliers,
- * to allow for scaling of the numstr by some default multiplier. */
- if (suffixes) {
- while (suffixes->suffix) {
- if (strcmp(suffixes->suffix, e) == 0) {
- if (ULONG_MAX / suffixes->mult < r)
- goto range; /* overflow! */
- ++e;
- r *= suffixes->mult;
- break;
- }
- ++suffixes;
- }
- }
-
- /* Note: trailing space is an error.
- It would be easy enough to allow though if desired. */
- if (*e)
- goto inval;
- /* Finally, check for range limits. */
- if (r >= lower && r <= upper)
- return r;
- range:
- bb_error_msg_and_die("number %s is not in %lu..%lu range",
- numstr, lower, upper);
- inval:
- bb_error_msg_and_die("invalid number '%s'", numstr);
-}
-
-unsigned long xstrtoul_range(const char *numstr, int base,
- unsigned long lower,
- unsigned long upper)
-{
- return xstrtoul_range_sfx(numstr, base, lower, upper, NULL);
-}
-
-unsigned long xstrtoul_sfx(const char *numstr, int base,
- const struct suffix_mult *suffixes)
-{
- return xstrtoul_range_sfx(numstr, base, 0, ULONG_MAX, suffixes);
-}
-
-unsigned long xstrtoul(const char *numstr, int base)
-{
- return xstrtoul_range_sfx(numstr, base, 0, ULONG_MAX, NULL);
-}
-
-unsigned long xatoul_range_sfx(const char *numstr,
- unsigned long lower,
- unsigned long upper,
- const struct suffix_mult *suffixes)
-{
- return xstrtoul_range_sfx(numstr, 10, lower, upper, suffixes);
-}
-
-unsigned long xatoul_sfx(const char *numstr,
- const struct suffix_mult *suffixes)
-{
- return xstrtoul_range_sfx(numstr, 10, 0, ULONG_MAX, suffixes);
-}
-
-unsigned long xatoul_range(const char *numstr,
- unsigned long lower,
- unsigned long upper)
-{
- return xstrtoul_range_sfx(numstr, 10, lower, upper, NULL);
-}
-
-unsigned long xatoul(const char *numstr)
-{
- return xatoul_sfx(numstr, NULL);
-}
-
-/* Signed ones */
-
-long xstrtol_range_sfx(const char *numstr, int base,
- long lower,
- long upper,
- const struct suffix_mult *suffixes)
-{
- unsigned long u = LONG_MAX;
- long r;
- const char *p = numstr;
-
- if ((p[0] == '-') && (p[1] != '+')) {
- ++p;
- ++u; /* two's complement */
- }
-
- r = xstrtoul_range_sfx(p, base, 0, u, suffixes);
-
- if (*numstr == '-') {
- r = -r;
- }
-
- if (r < lower || r > upper) {
- bb_error_msg_and_die("number %s is not in %ld..%ld range",
- numstr, lower, upper);
- }
-
- return r;
-}
-
-long xstrtol_range(const char *numstr, int base, long lower, long upper)
-{
- return xstrtol_range_sfx(numstr, base, lower, upper, NULL);
-}
-
-long xatol_range_sfx(const char *numstr,
- long lower,
- long upper,
- const struct suffix_mult *suffixes)
-{
- return xstrtol_range_sfx(numstr, 10, lower, upper, suffixes);
-}
-
-long xatol_range(const char *numstr, long lower, long upper)
-{
- return xstrtol_range_sfx(numstr, 10, lower, upper, NULL);
-}
-
-long xatol_sfx(const char *numstr, const struct suffix_mult *suffixes)
-{
- return xstrtol_range_sfx(numstr, 10, LONG_MIN, LONG_MAX, suffixes);
-}
-
-long xatol(const char *numstr)
-{
- return xstrtol_range_sfx(numstr, 10, LONG_MIN, LONG_MAX, NULL);
-}
-
-/* Others */
-
-unsigned xatou_range(const char *numstr, unsigned lower, unsigned upper)
-{
- return xstrtoul_range_sfx(numstr, 10, lower, upper, NULL);
-}
-
-unsigned xatou_sfx(const char *numstr, const struct suffix_mult *suffixes)
-{
- return xstrtoul_range_sfx(numstr, 10, 0, UINT_MAX, suffixes);
-}
-
-unsigned xatou(const char *numstr)
-{
- return xatoul_range(numstr, 0, UINT_MAX);
-}
-
-int xatoi_range(const char *numstr, int lower, int upper)
-{
- return xatol_range(numstr, lower, upper);
-}
-
-int xatoi(const char *numstr)
-{
- return xatol_range(numstr, INT_MIN, INT_MAX);
-}
-
-int xatoi_u(const char *numstr)
-{
- return xatoul_range(numstr, 0, INT_MAX);
-}
-
-uint32_t xatou32(const char *numstr)
-{
- return xatoul_range(numstr, 0, 0xffffffff);
-}
-
-uint16_t xatou16(const char *numstr)
-{
- return xatoul_range(numstr, 0, 0xffff);
-}
diff --git a/libbb/xatonum.c b/libbb/xatonum.c
new file mode 100644
index 0000000..910667c
--- /dev/null
+++ b/libbb/xatonum.c
@@ -0,0 +1,91 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * ascii-to-numbers implementations for busybox
+ *
+ * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
+ *
+ * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+
+#define type long long
+#define xstrtou(rest) xstrtoull##rest
+#define xstrto(rest) xstrtoll##rest
+#define xatou(rest) xatoull##rest
+#define xato(rest) xatoll##rest
+#define XSTR_UTYPE_MAX ULLONG_MAX
+#define XSTR_TYPE_MAX LLONG_MAX
+#define XSTR_TYPE_MIN LLONG_MIN
+#define XSTR_STRTOU strtoull
+#include "xatonum_template.c"
+#undef type
+#undef xstrtou
+#undef xstrto
+#undef xatou
+#undef xato
+#undef XSTR_UTYPE_MAX
+#undef XSTR_TYPE_MAX
+#undef XSTR_TYPE_MIN
+#undef XSTR_STRTOU
+
+#if ULONG_MAX != ULLONG_MAX
+#define type long
+#define xstrtou(rest) xstrtoul##rest
+#define xstrto(rest) xstrtol##rest
+#define xatou(rest) xatoul##rest
+#define xato(rest) xatol##rest
+#define XSTR_UTYPE_MAX ULONG_MAX
+#define XSTR_TYPE_MAX LONG_MAX
+#define XSTR_TYPE_MIN LONG_MIN
+#define XSTR_STRTOU strtoul
+#include "xatonum_template.c"
+#undef type
+#undef xstrtou
+#undef xstrto
+#undef xatou
+#undef xato
+#undef XSTR_UTYPE_MAX
+#undef XSTR_TYPE_MAX
+#undef XSTR_TYPE_MIN
+#undef XSTR_STRTOU
+#endif
+
+#if UINT_MAX != ULONG_MAX
+#define type int
+#define xstrtou(rest) xstrtou##rest
+#define xstrto(rest) xstrtoi##rest
+#define xatou(rest) xatou##rest
+#define xato(rest) xatoi##rest
+#define XSTR_UTYPE_MAX UINT_MAX
+#define XSTR_TYPE_MAX INT_MAX
+#define XSTR_TYPE_MIN INT_MIN
+#define XSTR_STRTOU strtoul
+#include "xatonum_template.c"
+#undef type
+#undef xstrtou
+#undef xstrto
+#undef xatou
+#undef xato
+#undef XSTR_UTYPE_MAX
+#undef XSTR_TYPE_MAX
+#undef XSTR_TYPE_MIN
+#undef XSTR_STRTOU
+#endif
+
+/* A few special cases */
+
+int xatoi_u(const char *numstr)
+{
+ return xatoul_range(numstr, 0, INT_MAX);
+}
+
+uint32_t xatou32(const char *numstr)
+{
+ return xatoul_range(numstr, 0, 0xffffffff);
+}
+
+uint16_t xatou16(const char *numstr)
+{
+ return xatoul_range(numstr, 0, 0xffff);
+}
diff --git a/libbb/xatonum_template.c b/libbb/xatonum_template.c
new file mode 100644
index 0000000..245586a
--- /dev/null
+++ b/libbb/xatonum_template.c
@@ -0,0 +1,175 @@
+/*
+You need to define the following (example):
+
+#define type long
+#define xstrtou(rest) xstrtoul##rest
+#define xstrto(rest) xstrtol##rest
+#define xatou(rest) xatoul##rest
+#define xato(rest) xatol##rest
+#define XSTR_UTYPE_MAX ULONG_MAX
+#define XSTR_TYPE_MAX LONG_MAX
+#define XSTR_TYPE_MIN LONG_MIN
+#define XSTR_STRTOU strtoul
+*/
+
+unsigned type xstrtou(_range_sfx)(const char *numstr, int base,
+ unsigned type lower,
+ unsigned type upper,
+ const struct suffix_mult *suffixes)
+{
+ unsigned type r;
+ int old_errno;
+ char *e;
+
+ /* 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 lnumstrer macro implementaion. */
+ if ((*numstr == '-') || (isspace)(*numstr))
+ goto inval;
+
+ /* 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 = XSTR_STRTOU(numstr, &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 || (numstr == e))
+ goto inval; /* error / no digits / illegal trailing chars */
+
+ errno = old_errno; /* Ok. So restore errno. */
+
+ /* Do optional suffix parsing. Allow 'empty' suffix tables.
+ * Note that we also allow nul suffixes with associated multipliers,
+ * to allow for scaling of the numstr by some default multiplier. */
+ if (suffixes) {
+ while (suffixes->suffix) {
+ if (strcmp(suffixes->suffix, e) == 0) {
+ if (XSTR_UTYPE_MAX / suffixes->mult < r)
+ goto range; /* overflow! */
+ ++e;
+ r *= suffixes->mult;
+ break;
+ }
+ ++suffixes;
+ }
+ }
+
+ /* Note: trailing space is an error.
+ It would be easy enough to allow though if desired. */
+ if (*e)
+ goto inval;
+ /* Finally, check for range limits. */
+ if (r >= lower && r <= upper)
+ return r;
+ range:
+ bb_error_msg_and_die("number %s is not in %llu..%llu range",
+ numstr, (unsigned long long)lower,
+ (unsigned long long)upper);
+ inval:
+ bb_error_msg_and_die("invalid number '%s'", numstr);
+}
+
+unsigned type xstrtou(_range)(const char *numstr, int base,
+ unsigned type lower,
+ unsigned type upper)
+{
+ return xstrtou(_range_sfx)(numstr, base, lower, upper, NULL);
+}
+
+unsigned type xstrtou(_sfx)(const char *numstr, int base,
+ const struct suffix_mult *suffixes)
+{
+ return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, suffixes);
+}
+
+unsigned type xstrtou()(const char *numstr, int base)
+{
+ return xstrtou(_range_sfx)(numstr, base, 0, XSTR_UTYPE_MAX, NULL);
+}
+
+unsigned type xatou(_range_sfx)(const char *numstr,
+ unsigned type lower,
+ unsigned type upper,
+ const struct suffix_mult *suffixes)
+{
+ return xstrtou(_range_sfx)(numstr, 10, lower, upper, suffixes);
+}
+
+unsigned type xatou(_range)(const char *numstr,
+ unsigned type lower,
+ unsigned type upper)
+{
+ return xstrtou(_range_sfx)(numstr, 10, lower, upper, NULL);
+}
+
+unsigned type xatou(_sfx)(const char *numstr,
+ const struct suffix_mult *suffixes)
+{
+ return xstrtou(_range_sfx)(numstr, 10, 0, XSTR_UTYPE_MAX, suffixes);
+}
+
+unsigned type xatou()(const char *numstr)
+{
+ return xatou(_sfx)(numstr, NULL);
+}
+
+/* Signed ones */
+
+type xstrto(_range_sfx)(const char *numstr, int base,
+ type lower,
+ type upper,
+ const struct suffix_mult *suffixes)
+{
+ unsigned type u = XSTR_TYPE_MAX;
+ type r;
+ const char *p = numstr;
+
+ if ((p[0] == '-') && (p[1] != '+')) {
+ ++p;
+ ++u; /* two's complement */
+ }
+
+ r = xstrtou(_range_sfx)(p, base, 0, u, suffixes);
+
+ if (*numstr == '-') {
+ r = -r;
+ }
+
+ if (r < lower || r > upper) {
+ bb_error_msg_and_die("number %s is not in %lld..%lld range",
+ numstr, (long long)lower, (long long)upper);
+ }
+
+ return r;
+}
+
+type xstrto(_range)(const char *numstr, int base, type lower, type upper)
+{
+ return xstrto(_range_sfx)(numstr, base, lower, upper, NULL);
+}
+
+type xato(_range_sfx)(const char *numstr,
+ type lower,
+ type upper,
+ const struct suffix_mult *suffixes)
+{
+ return xstrto(_range_sfx)(numstr, 10, lower, upper, suffixes);
+}
+
+type xato(_range)(const char *numstr, type lower, type upper)
+{
+ return xstrto(_range_sfx)(numstr, 10, lower, upper, NULL);
+}
+
+type xato(_sfx)(const char *numstr, const struct suffix_mult *suffixes)
+{
+ return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, suffixes);
+}
+
+type xato()(const char *numstr)
+{
+ return xstrto(_range_sfx)(numstr, 10, XSTR_TYPE_MIN, XSTR_TYPE_MAX, NULL);
+}