* tests/tests.h: Add sprintrc declaration. * tests/futex.c (sprintrc): Remove. * tests/sprintrc.c: New file. * tests/Makefile.am (libtests_a_SOURCES): Add sprintrc.c. --- tests/Makefile.am | 1 + tests/futex.c | 24 ------------------------ tests/sprintrc.c | 36 ++++++++++++++++++++++++++++++++++++ tests/tests.h | 3 +++ 4 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 tests/sprintrc.c
diff --git a/tests/Makefile.am b/tests/Makefile.am index b879bf4..2cf9674 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 fb7833c..63669d2 100644 --- a/tests/futex.c +++ b/tests/futex.c @@ -143,30 +143,6 @@ void invalid_op(int *val, int op, uint32_t argmask, ...) printf(") = -1 ENOSYS (%m)\n"); } -const char *sprintrc(int rc) -{ - enum { RES_BUF_SIZE = 4095 }; - static char buf[RES_BUF_SIZE]; - int saved_errno = errno; - int ret; - - /* Common case, making it quick and error-free */ - if (rc == 0) - return "0"; - - ret = snprintf(buf, sizeof(buf), "%d", rc); - - if (ret < 0) - perror_msg_and_fail("snprintf"); - if ((rc != -1) || ((size_t)ret >= sizeof(buf))) - return buf; - - errno = saved_errno; - snprintf(buf + ret, sizeof(buf) - ret, " %s (%m)", errno2name()); - - 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 0000000..3c05f08 --- /dev/null +++ b/tests/sprintrc.c @@ -0,0 +1,36 @@ +#include <errno.h> + +#include "tests.h" + +/** + * 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(int rc) +{ + enum { RES_BUF_SIZE = 4095 }; + static char buf[RES_BUF_SIZE]; + int saved_errno = errno; + int ret; + + /* Common case, making it quick and error-free */ + if (rc == 0) + return "0"; + + ret = snprintf(buf, sizeof(buf), "%d", rc); + + if (ret < 0) + perror_msg_and_fail("snprintf"); + if ((rc != -1) || ((size_t)ret >= sizeof(buf))) + return buf; + + errno = saved_errno; + snprintf(buf + ret, sizeof(buf) - ret, " %s (%m)", errno2name()); + + return buf; +} diff --git a/tests/tests.h b/tests/tests.h index 81abe82..ae99c45 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(int rc); + struct xlat; /* Print flags in symbolic form according to xlat table. */ -- 1.7.10.4 ------------------------------------------------------------------------------ _______________________________________________ Strace-devel mailing list Strace-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/strace-devel