]> granicus.if.org Git - strace/commitdiff
tests: move sprintrc function to libtests
authorEugene Syromyatnikov <evgsyr@gmail.com>
Fri, 2 Sep 2016 15:28:02 +0000 (18:28 +0300)
committerDmitry V. Levin <ldv@altlinux.org>
Fri, 2 Sep 2016 17:42:42 +0000 (17:42 +0000)
* 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
tests/futex.c
tests/sprintrc.c [new file with mode: 0644]
tests/tests.h

index b879bf4a953430f2859f5c06acd59e1467cef56a..2cf967483df1defda8900fa8ba84465b45b76b9a 100644 (file)
@@ -54,6 +54,7 @@ libtests_a_SOURCES = \
        printflags.c \
        printxval.c \
        signal2name.c \
+       sprintrc.c \
        tail_alloc.c \
        tests.h \
        tprintf.c \
index 09a6c25c4cf931bce99bf626f01e28b2ceec05ba..63669d27c3e63c71b54eea68d2a38cf69988f069 100644 (file)
@@ -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 (file)
index 0000000..5e27680
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2016 Eugene Syromiatnikov <evgsyr@gmail.com>
+ * 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 <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;
+}
index 81abe826ab6e47d19f6570ef9b8f31a853e89a1f..3043f97c4a8569e5e061d1fd8e3943f953308437 100644 (file)
@@ -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. */