Welcome to little lamb

Code » limb » commit 357eb0d

djbunix.h: Add fd_same()

author Olivier Brunel
2023-06-01 14:05:52 UTC
committer Olivier Brunel
2023-07-05 07:39:26 UTC
parent d9d6bf2e8d45f1015006cef9d1ae40daf2dad118

djbunix.h: Add fd_same()

src/doc/djbunix.h.0.md +3 -0
src/doc/djbunix.h/fd_same.3.md +33 -0
src/liblimb/djbunix.h/fd_same.c +15 -0
src/liblimb/include/limb/djbunix.h +2 -0

diff --git a/src/doc/djbunix.h.0.md b/src/doc/djbunix.h.0.md
index e255dfa..78b3e1a 100644
--- a/src/doc/djbunix.h.0.md
+++ b/src/doc/djbunix.h.0.md
@@ -29,6 +29,9 @@ The following functions/macros are defined :
 :: Same as [fd_mkdirpat](3) but relative path are based on current working
 :: directory.
 
+: [fd_same](3)
+:: Return whether or not two file descriptors reference the same file.
+
 : [mkdirpat](3)
 :: Create a path of directories.
 
diff --git a/src/doc/djbunix.h/fd_same.3.md b/src/doc/djbunix.h/fd_same.3.md
new file mode 100644
index 0000000..a639d6e
--- /dev/null
+++ b/src/doc/djbunix.h/fd_same.3.md
@@ -0,0 +1,33 @@
+% limb manual
+% fd_same(3)
+
+# NAME
+
+fd_same - return whether or not two file descriptors reference the same file
+
+# SYNOPSIS
+
+    #include <limb/djbunix.h>
+
+```pre hl
+int fd_same(int <em>fd1</em>, int <em>fd2</em>)
+```
+
+# DESCRIPTION
+
+The `fd_same`() function returns whether or not the file descriptors `fd1` and
+`fd2` reference the same file.
+
+This is determined by calling [fstat](3) for both and comparing the value of
+members `st_dev` and `st_ino` of their respective *struct stat*.
+
+# RETURN VALUE
+
+On success, the `fd_same`() function returns 1 when both file descriptors
+reference the same file, and 0 when they don't.
+Otherwise, it returns -1 and sets `errno` to indicate the error.
+
+# ERRORS
+
+The `fd_same`() function may fail and set errno for any of the errors specified
+for [fstat](3).
diff --git a/src/liblimb/djbunix.h/fd_same.c b/src/liblimb/djbunix.h/fd_same.c
new file mode 100644
index 0000000..a257541
--- /dev/null
+++ b/src/liblimb/djbunix.h/fd_same.c
@@ -0,0 +1,15 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <limb/djbunix.h>
+
+int
+fd_same(int fd1, int fd2)
+{
+    struct stat st1, st2;
+
+    if (fstat(fd1, &st1) < 0 || fstat(fd2, &st2) < 0)
+        return -1;
+
+    return (st1.st_dev == st2.st_dev) && (st1.st_ino == st2.st_ino);
+}
diff --git a/src/liblimb/include/limb/djbunix.h b/src/liblimb/include/limb/djbunix.h
index 385546c..b204d41 100644
--- a/src/liblimb/include/limb/djbunix.h
+++ b/src/liblimb/include/limb/djbunix.h
@@ -19,6 +19,8 @@ extern int open_parsed_name(const char *name, open_fn open);
 #define open_write_close(file,data,dlen)    open_write_closeat(AT_FDCWD, file, data, dlen)
 #define open_writev_close(file,v,n)         open_writev_closeat(AT_FDCWD, file, v, n)
 
+extern int fd_same(int fd1, int fd2);
+
 extern int fd_mkdirpat(int fd, const char *name, mode_t mode);
 extern int fd_mkdirp(const char *name, mode_t mode);
 extern int mkdirpat(int fd, const char *name, mode_t mode);