Created
March 24, 2019 15:51
-
-
Save rgov/cbee899187e6db489ef9a8460e8e6167 to your computer and use it in GitHub Desktop.
write to stdout after closing it?
This file contains hidden or 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
/* doesn't work */ | |
#include <fcntl.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
int main() { | |
close(STDOUT_FILENO); | |
if (open("/dev/null", O_WRONLY) != STDOUT_FILENO) { | |
fprintf(stderr, "Could not open /dev/null as fd %d", STDOUT_FILENO); | |
return 1; | |
} | |
int out2 = open("/dev/stdout", O_WRONLY); | |
if (out2 == -1) { | |
perror("Could not open /dev/stdout:"); | |
return 1; | |
} | |
dprintf(out2, "Hello world."); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment