You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
833 B

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define r_stdin 0
#define r_stdout 1
#define r_stderr 2
#define LIKELY(e) __builtin_expect(!!(e), 1)
#define UNLIKELY(e) __builtin_expect(!!(e), 0)
static inline int dupall(int from)
{
if(UNLIKELY(dup2(from, r_stdin) < 0)) {
perror("failed to dup2() stdin to sink");
return 1;
}
if(UNLIKELY(dup2(from, r_stdout) < 0)) {
perror("failed to dup2() stdout to sink");
return 2;
}
#ifdef REPLACE_STDERR
if(UNLIKELY(dup2(from, r_stderr) < 0)) {
perror("failed to dup2() stderr to sink");
#else
if(UNLIKELY(close(r_stderr) != 0)) {
perror("failed to close stderr");
#endif
return 3;
}
return 0;
}
int main(void)
{
int null = open("/dev/null", O_RDWR);
if(UNLIKELY(null<0)) {
perror("failed to open /dev/null");
return 1;
}
return dupall(null);
}