Created
November 16, 2018 13:10
-
-
Save emersion/9153b14478b5531c56b577e7104f4c6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fcntl.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
int main() { | |
printf("before\n"); | |
int dupfd = dup(STDOUT_FILENO); | |
if (dupfd < 0) { | |
return 1; | |
} | |
int newfd = open("/tmp/out", O_RDWR | O_CREAT | O_TRUNC); | |
if (newfd < 0) { | |
return 2; | |
} | |
if (dup2(newfd, STDOUT_FILENO) < 0) { | |
return 3; | |
} | |
if (close(newfd) != 0) { | |
return 4; | |
} | |
printf("hello\n"); | |
if (dup2(dupfd, STDOUT_FILENO) < 0) { | |
return 5; | |
} | |
close(dupfd); | |
printf("after\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment