Skip to content

Instantly share code, notes, and snippets.

@rgov
Created March 24, 2019 15:51
Show Gist options
  • Save rgov/cbee899187e6db489ef9a8460e8e6167 to your computer and use it in GitHub Desktop.
Save rgov/cbee899187e6db489ef9a8460e8e6167 to your computer and use it in GitHub Desktop.
write to stdout after closing it?
/* 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