Rich Jones wanted to use xstrtoull and/or xstrtoll in libguestfs, but it didn't exist yet. So here is the new module.
I took the opportunity to write the test using the new init.sh framework. A couple things can be improved: Currently, every test that uses init.sh must include tests/init.sh in the "Files:" section and must append EXEEXT and srcdir definitions to the TESTS_ENVIRONMENT in the "Makefile.am:" section: Files: tests/init.sh ... Makefile.am: TESTS_ENVIRONMENT += EXEEXT='$(EXEEXT)' srcdir='$(srcdir)' Does anyone object to making gnulib-tool do those things automatically? The EXEEXT may appear to be unnecessary right now, but I have a patch (in progress) for init.sh that will make it automatically do the right thing even for systems that require use of an $(EXEEXT) suffix. Also, as I write this, I remember that the test uses these +#define __spec "llu" +#define __spec "lld" and don't know off hand if I need to depend on some module to ensure that they work properly. Jim >From 19487fbc2cbe9bf3fe8f98c58bd450f9cd54aaf2 Mon Sep 17 00:00:00 2001 From: Jim Meyering <meyer...@redhat.com> Date: Sat, 23 Jan 2010 11:52:44 +0100 Subject: [PATCH] xstrtoll: new module * modules/xstrtoll: New file. * MODULES.html.sh (Numeric conversion functions): Add xstrtoll. * lib/xstrtol.h [HAVE_LONG_LONG_INT]: Declare xstrtoll and xstrtoull. * lib/xstrtoll.c, lib/xstrtoull.c: New files. ./configure fails if you use this module and lack "long long". * modules/xstrtoll-tests: New module. * tests/test-xstrtoll.c, tests/test-xstrtoull.c: New files. * tests/test-xstrtoll.sh: Like test-xstrtol.c, but use the new init.sh-based test framework. --- ChangeLog | 13 +++++++++ MODULES.html.sh | 1 + lib/xstrtol.h | 5 +++ lib/xstrtoll.c | 6 ++++ lib/xstrtoull.c | 6 ++++ modules/xstrtoll | 30 +++++++++++++++++++++ modules/xstrtoll-tests | 18 +++++++++++++ tests/test-xstrtoll.c | 4 +++ tests/test-xstrtoll.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test-xstrtoull.c | 4 +++ 10 files changed, 154 insertions(+), 0 deletions(-) create mode 100644 lib/xstrtoll.c create mode 100644 lib/xstrtoull.c create mode 100644 modules/xstrtoll create mode 100644 modules/xstrtoll-tests create mode 100644 tests/test-xstrtoll.c create mode 100755 tests/test-xstrtoll.sh create mode 100644 tests/test-xstrtoull.c diff --git a/ChangeLog b/ChangeLog index 0ced685..35bf9cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2010-01-23 Jim Meyering <meyer...@redhat.com> + + xstrtoll: new module + * modules/xstrtoll: New file. + * MODULES.html.sh (Numeric conversion functions): Add xstrtoll. + * lib/xstrtol.h [HAVE_LONG_LONG_INT]: Declare xstrtoll and xstrtoull. + * lib/xstrtoll.c, lib/xstrtoull.c: New files. + ./configure fails if you use this module and lack "long long". + * modules/xstrtoll-tests: New module. + * tests/test-xstrtoll.c, tests/test-xstrtoull.c: New files. + * tests/test-xstrtoll.sh: Like test-xstrtol.c, but use the + new init.sh-based test framework. + 2010-01-22 Jim Meyering <meyer...@redhat.com> userspec: add unit tests diff --git a/MODULES.html.sh b/MODULES.html.sh index e3602ef..4c8cefa 100755 --- a/MODULES.html.sh +++ b/MODULES.html.sh @@ -1820,6 +1820,7 @@ func_all_modules () func_module c-strtold func_module xstrtod func_module xstrtol + func_module xstrtoll func_module xstrtold func_end_table diff --git a/lib/xstrtol.h b/lib/xstrtol.h index 95475f0..3a94a9c 100644 --- a/lib/xstrtol.h +++ b/lib/xstrtol.h @@ -46,6 +46,11 @@ _DECLARE_XSTRTOL (xstrtoul, unsigned long int) _DECLARE_XSTRTOL (xstrtoimax, intmax_t) _DECLARE_XSTRTOL (xstrtoumax, uintmax_t) +#if HAVE_LONG_LONG_INT +_DECLARE_XSTRTOL (xstrtoll, long long int) +_DECLARE_XSTRTOL (xstrtoull, unsigned long long int) +#endif + #ifndef __attribute__ # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) # define __attribute__(x) diff --git a/lib/xstrtoll.c b/lib/xstrtoll.c new file mode 100644 index 0000000..344e5a4 --- /dev/null +++ b/lib/xstrtoll.c @@ -0,0 +1,6 @@ +#define __strtol strtoll +#define __strtol_t long long int +#define __xstrtol xstrtoll +#define STRTOL_T_MINIMUM LONG_LONG_MIN +#define STRTOL_T_MAXIMUM LONG_LONG_MAX +#include "xstrtol.c" diff --git a/lib/xstrtoull.c b/lib/xstrtoull.c new file mode 100644 index 0000000..2f2d83c --- /dev/null +++ b/lib/xstrtoull.c @@ -0,0 +1,6 @@ +#define __strtol strtoull +#define __strtol_t unsigned long long int +#define __xstrtol xstrtoull +#define STRTOL_T_MINIMUM 0 +#define STRTOL_T_MAXIMUM ULONG_LONG_MAX +#include "xstrtol.c" diff --git a/modules/xstrtoll b/modules/xstrtoll new file mode 100644 index 0000000..a5da211 --- /dev/null +++ b/modules/xstrtoll @@ -0,0 +1,30 @@ +Description: +Convert string to 'long long' or 'unsigned long long', with error checking. + +Files: +lib/xstrtoll.c +lib/xstrtoull.c + +Depends-on: +strtoll +strtoull +xstrtol + +configure.ac: +AC_LIBOBJ([xstrtoll]) +AC_LIBOBJ([xstrtoull]) +AC_TYPE_LONG_LONG_INT +test $ac_cv_type_long_long_int = no \ + && AC_MSG_ERROR( + [you lack long long support; required by gnulib's xstrtoll module]) + +Makefile.am: + +Include: +"xstrtol.h" + +License: +GPL + +Maintainer: +Jim Meyering diff --git a/modules/xstrtoll-tests b/modules/xstrtoll-tests new file mode 100644 index 0000000..d2cf589 --- /dev/null +++ b/modules/xstrtoll-tests @@ -0,0 +1,18 @@ +Files: +tests/init.sh +tests/test-xstrtol.c +tests/test-xstrtoll.c +tests/test-xstrtoull.c +tests/test-xstrtoll.sh + +Depends-on: +xstrtoll + +configure.ac: + +Makefile.am: +TESTS += test-xstrtoll.sh +TESTS_ENVIRONMENT += EXEEXT='$(EXEEXT)' srcdir='$(srcdir)' +check_PROGRAMS += test-xstrtoll test-xstrtoull +test_xstrtoll_LDADD = $(LDADD) $(LIBINTL) +test_xstrtoull_LDADD = $(LDADD) $(LIBINTL) diff --git a/tests/test-xstrtoll.c b/tests/test-xstrtoll.c new file mode 100644 index 0000000..47a552e --- /dev/null +++ b/tests/test-xstrtoll.c @@ -0,0 +1,4 @@ +#define __xstrtol xstrtoll +#define __strtol_t long long int +#define __spec "lld" +#include "test-xstrtol.c" diff --git a/tests/test-xstrtoll.sh b/tests/test-xstrtoll.sh new file mode 100755 index 0000000..78a08c8 --- /dev/null +++ b/tests/test-xstrtoll.sh @@ -0,0 +1,67 @@ +#!/bin/sh +: ${srcdir=.} +. "$srcdir/init.sh"; path_prepend_ . + +too_big=99999999999999999999999999999999999999999999999999999999999999999999 +result=0 + +# test xstrtoll +test-xstrtoll 1 >> out 2>&1 || result=1 +test-xstrtoll -1 >> out 2>&1 || result=1 +test-xstrtoll 1k >> out 2>&1 || result=1 +test-xstrtoll ${too_big}h >> out 2>&1 && result=1 +test-xstrtoll $too_big >> out 2>&1 && result=1 +test-xstrtoll x >> out 2>&1 && result=1 +test-xstrtoll 9x >> out 2>&1 && result=1 +test-xstrtoll 010 >> out 2>&1 || result=1 +# suffix without integer is valid +test-xstrtoll MiB >> out 2>&1 || result=1 + +# test xstrtoull +test-xstrtoull 1 >> out 2>&1 || result=1 +test-xstrtoull -1 >> out 2>&1 && result=1 +test-xstrtoull 1k >> out 2>&1 || result=1 +test-xstrtoull ${too_big}h >> out 2>&1 && result=1 +test-xstrtoull $too_big >> out 2>&1 && result=1 +test-xstrtoull x >> out 2>&1 && result=1 +test-xstrtoull 9x >> out 2>&1 && result=1 +test-xstrtoull 010 >> out 2>&1 || result=1 +test-xstrtoull MiB >> out 2>&1 || result=1 + +# Find out how to remove carriage returns from output. Solaris /usr/ucb/tr +# does not understand '\r'. +if echo solaris | tr -d '\r' | grep solais > /dev/null; then + cr='\015' +else + cr='\r' +fi + +# normalize output +LC_ALL=C tr -d "$cr" < out > k +mv k out + +# compare expected output +cat > expected <<EOF +1->1 () +-1->-1 () +1k->1024 () +invalid suffix in X argument \`${too_big}h' +X argument \`$too_big' too large +invalid X argument \`x' +invalid suffix in X argument \`9x' +010->8 () +MiB->1048576 () +1->1 () +invalid X argument \`-1' +1k->1024 () +invalid suffix in X argument \`${too_big}h' +X argument \`$too_big' too large +invalid X argument \`x' +invalid suffix in X argument \`9x' +010->8 () +MiB->1048576 () +EOF + +compare expected out || result=1 + +Exit $result diff --git a/tests/test-xstrtoull.c b/tests/test-xstrtoull.c new file mode 100644 index 0000000..cf6c853 --- /dev/null +++ b/tests/test-xstrtoull.c @@ -0,0 +1,4 @@ +#define __xstrtol xstrtoull +#define __strtol_t unsigned long long int +#define __spec "llu" +#include "test-xstrtol.c" -- 1.6.6.1.531.gd34f4