[PATCH v2 4/5] tests: Move return code printing into a separate file

Eugene Syromyatnikov evgsyr at gmail.com
Fri Sep 2 15:28:02 UTC 2016


* 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     |   20 --------------------
 tests/sprintrc.c  |   33 +++++++++++++++++++++++++++++++++
 tests/tests.h     |    3 +++
 4 files changed, 37 insertions(+), 20 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 09a6c25..63669d2 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 0000000..a12dc24
--- /dev/null
+++ b/tests/sprintrc.c
@@ -0,0 +1,33 @@
+#include "tests.h"
+
+#include <errno.h>
+#include <stdio.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(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 81abe82..3043f97 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. */
-- 
1.7.10.4





More information about the Strace-devel mailing list