aboutsummaryrefslogtreecommitdiffstats
path: root/test.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2018-08-28 14:18:41 +0200
committerMattias Andrée <maandree@kth.se>2018-08-28 14:18:41 +0200
commitbad369a06217cd1588c289a75b6a1c17d44119b3 (patch)
tree626b3847268037f2621157a25bfbabb874bb88dc /test.c
parentMake it possible to force all memory allocation functions to fail in tests (diff)
downloadlibsimple-bad369a06217cd1588c289a75b6a1c17d44119b3.tar.gz
libsimple-bad369a06217cd1588c289a75b6a1c17d44119b3.tar.bz2
libsimple-bad369a06217cd1588c289a75b6a1c17d44119b3.tar.xz
Replace exit and fprintf
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'test.c')
-rw-r--r--test.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/test.c b/test.c
index ad5a4bc..edc3d15 100644
--- a/test.c
+++ b/test.c
@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#include "test.h"
+#include <sys/syscall.h>
#include <malloc.h>
#undef strndup
@@ -8,6 +9,13 @@
size_t alloc_fail_in = 0;
+int exit_real = 0;
+int exit_ok = 0;
+int exit_status;
+jmp_buf exit_jmp;
+char stderr_buf[8 << 10];
+size_t stderr_n = 0;
+int stderr_real = 0;
static int custom_malloc = 0;
@@ -222,3 +230,58 @@ free(void *ptr)
return;
assert(!munmap(info->real_beginning, info->real_size));
}
+
+
+void
+exit(int status)
+{
+ exit_status = status;
+ if (exit_real) {
+#ifdef SYS_exit_group
+ syscall(SYS_exit_group, status);
+#else
+ syscall(SYS_exit, status);
+#endif
+ }
+ assert(exit_ok);
+ longjmp(exit_jmp, 1);
+}
+
+
+int
+fprintf(FILE *restrict stream, const char *restrict format, ...)
+{
+ va_list ap;
+ va_start(ap, format);
+ return vfprintf(stream, format, ap);
+ va_end(ap);
+}
+
+
+int
+vfprintf(FILE *restrict stream, const char *restrict format, va_list ap)
+{
+ va_list ap2;
+ int r;
+ char *buf;
+ size_t n;
+
+ va_copy(ap2, ap);
+ r = vsnprintf(NULL, 0, format, ap2);
+ va_end(ap2);
+
+ if (r >= 0) {
+ n = (size_t)r;
+ buf = alloca(n + 1);
+ n = vsnprintf(buf, n + 1, format, ap);
+ if (fileno(stream) != STDERR_FILENO) {
+ fwrite(buf, 1, n, stream);
+ } else {
+ assert(stderr_n + n <= sizeof(stderr_buf));
+ memcpy(&stderr_buf[stderr_n], buf, n);
+ stderr_n += n;
+ }
+ }
+
+ return r;
+}