Last active
September 10, 2016 02:51
-
-
Save ph1ee/05495695efbb57906f901d5ddba70ff7 to your computer and use it in GitHub Desktop.
Check if stdout is redirected to /dev/null
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
| #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