From 6f9aefde214b9438551268cf1c9c128b1cef6413 Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Fri, 2 Sep 2016 18:28:02 +0300 Subject: [PATCH] tests: move sprintrc function to libtests * tests/tests.h (sprintrc): New prototype. * tests/futex.c (sprintrc): Move to ... * tests/sprintrc.c: ... new file. * tests/Makefile.am (libtests_a_SOURCES): Add sprintrc.c. --- tests/Makefile.am | 1 + tests/futex.c | 20 ---------------- tests/sprintrc.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++ tests/tests.h | 3 +++ 4 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 tests/sprintrc.c diff --git a/tests/Makefile.am b/tests/Makefile.am index b879bf4a..2cf96748 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -54,6 +54,7 @@ libtests_a_SOURCES = \ printflags.c \ printxval.c \ signal2name.c \ + sprintrc.c \ tail_alloc.c \ tests.h \ tprintf.c \ diff --git a/tests/futex.c b/tests/futex.c index 09a6c25c..63669d27 100644 --- a/tests/futex.c +++ b/tests/futex.c @@ -143,26 +143,6 @@ void invalid_op(int *val, int op, uint32_t argmask, ...) printf(") = -1 ENOSYS (%m)\n"); } -const char *sprintrc(long rc) -{ - static char buf[4096]; - - if (rc == 0) - return "0"; - - int ret = (rc == -1) - ? snprintf(buf, sizeof(buf), "-1 %s (%m)", errno2name()) - : snprintf(buf, sizeof(buf), "%ld", rc); - - if (ret < 0) - perror_msg_and_fail("snprintf"); - if ((size_t) ret >= sizeof(buf)) - error_msg_and_fail("snprintf overflow: got %d, expected " - "no more than %zu", ret, sizeof(buf)); - - return buf; -} - # define CHECK_INVALID_CLOCKRT(op, ...) \ do { \ invalid_op(uaddr, FUTEX_CLOCK_REALTIME | (op), __VA_ARGS__); \ diff --git a/tests/sprintrc.c b/tests/sprintrc.c new file mode 100644 index 00000000..5e27680e --- /dev/null +++ b/tests/sprintrc.c @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2016 Eugene Syromiatnikov + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "tests.h" +#include + +/** + * Provides pointer to static string buffer with printed return code in format + * used by strace - with errno and error message. + * + * @param rc Return code. + * @return Pointer to (statically allocated) buffer containing decimal + * representation of return code and errno/error message in case @rc + * is equal to -1. + */ +const char * +sprintrc(long rc) +{ + static char buf[4096]; + + if (rc == 0) + return "0"; + + int ret = (rc == -1) + ? snprintf(buf, sizeof(buf), "-1 %s (%m)", errno2name()) + : snprintf(buf, sizeof(buf), "%ld", rc); + + if (ret < 0) + perror_msg_and_fail("snprintf"); + if ((size_t) ret >= sizeof(buf)) + error_msg_and_fail("snprintf overflow: got %d, expected" + " no more than %zu", ret, sizeof(buf)); + + return buf; +} diff --git a/tests/tests.h b/tests/tests.h index 81abe826..3043f97c 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -99,6 +99,9 @@ const char *errno2name(void); /* Translate signal number to its name. */ const char *signal2name(int); +/* Print return code and, in case return code is -1, errno information. */ +const char *sprintrc(long rc); + struct xlat; /* Print flags in symbolic form according to xlat table. */ -- 2.40.0