Skip to content

Instantly share code, notes, and snippets.

@ph1ee
Last active September 10, 2016 02:51
Show Gist options
  • Select an option

  • Save ph1ee/05495695efbb57906f901d5ddba70ff7 to your computer and use it in GitHub Desktop.

Select an option

Save ph1ee/05495695efbb57906f901d5ddba70ff7 to your computer and use it in GitHub Desktop.
Check if stdout is redirected to /dev/null
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <stdlib.h>
int is_stdout_null() {
char path[PATH_MAX], link[PATH_MAX];
sprintf(path, "/proc/self/fd/%d", STDOUT_FILENO);
ssize_t r;
r = readlink(path, link, sizeof(link));
if (r < 0) {
return r;
}
link[r] = '\0';
return strcmp(link, "/dev/null");
}
int main(void) {
if (is_stdout_null() == 0) {
fprintf(stderr, "stdout is redirected to /dev/null");
}
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment