aboutsummaryrefslogtreecommitdiffstats
path: root/libpatch_is_devnull.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-01-09 22:04:24 +0100
committerMattias Andrée <maandree@kth.se>2024-01-09 22:04:24 +0100
commitae850b1ac755f471beac4dbfef4654fe3fbaaae9 (patch)
tree319e7f7f1b99cd1ee75e100f0a29b0f7c827ccea /libpatch_is_devnull.c
downloadlibpatch-master.tar.gz
libpatch-master.tar.bz2
libpatch-master.tar.xz
First commitHEADmaster
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'libpatch_is_devnull.c')
-rw-r--r--libpatch_is_devnull.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/libpatch_is_devnull.c b/libpatch_is_devnull.c
new file mode 100644
index 0000000..3a78ea6
--- /dev/null
+++ b/libpatch_is_devnull.c
@@ -0,0 +1,24 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#include <sys/sysmacros.h>
+
+
+int
+libpatch_is_devnull(int fd)
+{
+ struct stat fd_st;
+#ifndef __linux__
+ struct stat null_st;
+#endif
+ if (fstat(fd, &fd_st))
+ return -1;
+ if (!S_ISCHR(fd_st.st_mode))
+ return 0;
+#ifdef __linux__
+ return fd_st.st_rdev == makedev(1, 3);
+#else
+ if (stat("/dev/null", &null_st))
+ return -1;
+ return fd_st.st_rdev == null_st.st_rdev;
+#endif
+}