libusbgx: tests: Use RTLD to access original fputs()
authorKrzysztof Opasiak <k.opasiak@samsung.com>
Wed, 12 Nov 2014 15:54:13 +0000 (16:54 +0100)
committerKrzysztof Opasiak <k.opasiak@samsung.com>
Tue, 22 Dec 2015 20:41:57 +0000 (21:41 +0100)
Sometimes we would like to print something to stderr
or stdout from tests so our wrapper should use standard
fputs() definition for this function.

Signed-off-by: Krzysztof Opasiak <k.opasiak@samsung.com>
tests/usbg-io-wrappers.c

index e20f149..8a0e94e 100644 (file)
@@ -1,3 +1,4 @@
+#define _GNU_SOURCE
 #include <dirent.h>
 #include <stdio.h>
 #include <stdarg.h>
@@ -6,6 +7,11 @@
 #include <string.h>
 #include <stddef.h>
 #include <cmocka.h>
+#include <dlfcn.h>
+
+typedef int (*fputs_f_type)(const char *, FILE *);
+typedef int (*fflush_f_type)(FILE *);
+typedef fflush_f_type ferror_f_type;
 
 /**
  * @brief Simulates opening file
@@ -146,9 +152,10 @@ int fputs(const char *s, FILE *stream)
 {
        /* Cmocka (or anything else) may want to print some errors.
         * Especially when running fputs itself */
-       if (stream == stderr) {
-               printf("%s", s);
-               return 0;
+       if (stream == stderr || stream == stdout) {
+               fputs_f_type orig_fputs;
+               orig_fputs = (fputs_f_type)dlsym(RTLD_NEXT, "fputs");
+               return orig_fputs(s, stream);
        }
 
        check_expected(stream);
@@ -161,10 +168,22 @@ int fputs(const char *s, FILE *stream)
  */
 int fflush(FILE *stream)
 {
+       if (stream == stderr || stream == stdout) {
+               fflush_f_type orig_fflush;
+               orig_fflush = (fflush_f_type)dlsym(RTLD_NEXT, "fflush");
+               return orig_fflush(stream);
+       }
+
        return 0;
 }
 
 int ferror(FILE *stream)
 {
+       if (stream == stderr || stream == stdout) {
+               ferror_f_type orig_ferror;
+               orig_ferror = (ferror_f_type)dlsym(RTLD_NEXT, "ferror");
+               return orig_ferror(stream);
+       }
+
        return 0;
 }