AC_CONFIG_TESTDIR([.], [t:.])
DX_PROG_AUTOTEST_AM
-AC_CHECK_FUNCS_ONCE([strtoumax strtoull __strtoull])
-AC_CONFIG_FILES([
- Makefile
-])
+AC_CONFIG_FILES([Makefile])
AC_OUTPUT
}
}
- if (count_str && !strict_strtoul(&count, count_str, 0)) {
+ if (count_str && !test_strtoul(&count, count_str)) {
fprintf(stderr, "%s: invalid count: %s\n", progname, count_str);
return EXIT_FAILURE;
}
mode = MODE_ENGLISH;
break;
case 'n':
- if (!strict_strtoul(&n, optarg, 0) || n > (size_t)-2) {
+ if (!test_strtoul(&n, optarg) || n > (size_t)-2) {
fprintf(stderr, "%s: invalid count: %s\n",
progname, optarg);
return EXIT_FAILURE;
return z ^ (z >> 31);
}
-#if HAVE_STRTOULL
-# define STRTOULL strtoull
-#elif HAVE___STRTOULL
-/* HP-UX 11 has __strtoull in <inttypes.h> */
-# define STRTOULL __strtoull
-#else
-/*
- * Just fall back to strtoul -- in the worst case we just lose the ability
- * to set all 64 bits of the seed.
- */
-# define STRTOULL strtoul
-#endif
-
struct test_rng *test_rng_alloc(const char *seed_str)
{
unsigned long long seed;
+ uintmax_t limit, seed_val;
struct test_rng *rng;
- char *end;
- errno = 0;
- seed = STRTOULL(seed_str, &end, 0);
- if (*end != 0) {
+ limit = (uintmax_t)0xffffffff;
+ limit |= (limit << 16 << 16);
+
+ if (!test_strtoumax(&seed_val, seed_str, limit)) {
fprintf(stderr, "%s: invalid seed\n", seed_str);
return NULL;
- } else if (errno != 0) {
- fprintf(stderr, "%s: invalid seed: %s\n",
- seed_str, strerror(errno));
- return NULL;
}
rng = malloc_nofail(sizeof *rng);
void *malloc_nofail(size_t size);
void *realloc_nofail(void *ptr, size_t size);
-bool strict_strtoul(unsigned long *val, const char *str, int base);
+bool test_strtoumax(uintmax_t *out, const char *s, uintmax_t limit);
+
+static inline bool test_strtoul(unsigned long *val, const char *str)
+{
+ uintmax_t v;
+ bool rc;
+
+ rc = test_strtoumax(&v, str, (unsigned long)-1);
+ *val = v;
+ return rc;
+}
void test_print_options(const struct option *lopts);
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <errno.h>
#include <getopt.h>
#include <cdecl.h>
#include "help.h"
#include "test.h"
+#include "intconv.h"
void *realloc_nofail(void *ptr, size_t size)
{
return realloc_nofail(NULL, size);
}
-bool strict_strtoul(unsigned long *val, const char *str, int base)
+static unsigned intconv_base(const char **str)
{
- char *end;
+ if ((*str)[0] == '0') {
+ if ((*str)[1] == 'X' || (*str)[1] == 'x') {
+ *str += 2;
+ return INTCONV_HEXADECIMAL;
+ }
- errno = 0;
- *val = strtoul(str, &end, base);
- if (errno != 0 || *end != 0)
- return false;
+ return INTCONV_OCTAL;
+ }
+
+ return INTCONV_DECIMAL;
+}
+
+bool test_strtoumax(uintmax_t *out, const char *s, uintmax_t limit)
+{
+ static const char idx[] = "0123456789abcdef0123456789ABCDEF";
+ unsigned base = intconv_base(&s);
+ uintmax_t v;
+ char *c, d;
+
+ for (v = 0; (d = *s++);) {
+ if (!(c = strchr(idx, d)) || (d = (c-idx) & 0xf) >= base)
+ return 0;
+
+ if (!intconv_shift(&v, base, d) || v > limit)
+ return 0;
+ }
+ *out = v;
return true;
}