Skip to content
This repository has been archived by the owner on Feb 18, 2023. It is now read-only.

Commit

Permalink
test: replace freopen with dup/dup2 (#157)
Browse files Browse the repository at this point in the history
* test: replace freopen with dup/dup2

* fix win32
  • Loading branch information
sreimers authored Nov 20, 2022
1 parent ee01845 commit 4dccd6e
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (C) 2010 Creytiv.com
*/
#define _DEFAULT_SOURCE 1
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Expand Down Expand Up @@ -32,6 +33,9 @@
#define read _read
#define write _write
#define close _close
#define dup _dup
#define dup2 _dup2
#define fileno _fileno
#endif


Expand Down Expand Up @@ -260,14 +264,20 @@ static char datapath[256] = "./data";


static uint32_t timeout_override;
static int dup_stdout;
static int dup_stderr;

enum test_mode test_mode = TEST_NONE;


static void hide_output(void)
{
(void)freopen("stdout.out", "w", stdout);
(void)freopen("stderr.out", "w", stderr);
dup_stdout = dup(fileno(stdout));
dup_stderr = dup(fileno(stderr));
int fd_out = open("stdout.out", O_WRONLY | O_CREAT);
int fd_err = open("stderr.out", O_WRONLY | O_CREAT);
(void)dup2(fd_out, fileno(stdout));
(void)dup2(fd_err, fileno(stderr));
}


Expand All @@ -280,12 +290,12 @@ static void restore_output(int err)
fflush(stderr);

/* Restore stdout/stderr */
fs_stdio_restore();
(void)dup2(dup_stdout, fileno(stdout));
(void)dup2(dup_stderr, fileno(stderr));

if (!err)
goto out;


f = fopen("stdout.out", "r");
if (!f)
goto out;
Expand Down

0 comments on commit 4dccd6e

Please sign in to comment.