Skip to content

Instantly share code, notes, and snippets.

@emersion
Created November 16, 2018 13:10
Show Gist options
  • Save emersion/9153b14478b5531c56b577e7104f4c6c to your computer and use it in GitHub Desktop.
Save emersion/9153b14478b5531c56b577e7104f4c6c to your computer and use it in GitHub Desktop.
#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