aboutsummaryrefslogtreecommitdiffstats
path: root/readmagiclink.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2024-07-01 20:11:29 +0200
committerMattias Andrée <maandree@kth.se>2024-07-01 20:46:56 +0200
commitb550a5ba0261f3b6aba8988e37f1005102544568 (patch)
tree77d996b805cf8561847872636b4c94039d329756 /readmagiclink.c
parentFix libsimple_abspath (diff)
downloadlibsimple-b550a5ba0261f3b6aba8988e37f1005102544568.tar.gz
libsimple-b550a5ba0261f3b6aba8988e37f1005102544568.tar.bz2
libsimple-b550a5ba0261f3b6aba8988e37f1005102544568.tar.xz
Add readlink and readmagiclink functions
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'readmagiclink.c')
-rw-r--r--readmagiclink.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/readmagiclink.c b/readmagiclink.c
new file mode 100644
index 0000000..160dd96
--- /dev/null
+++ b/readmagiclink.c
@@ -0,0 +1,61 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+#ifndef TEST
+
+
+char *
+libsimple_readmagiclink(const char *path)
+{
+ int fd;
+ struct stat st;
+ char *target1;
+ char *target2;
+
+ fd = open(path, O_PATH | O_NOFOLLOW);
+ if (fd < 0)
+ return NULL;
+
+ target1 = libsimple_efreadlink(fd);
+ if (!strends(target1, " (deleted)")) {
+ close(fd);
+ return target1;
+ }
+
+ if (fstat(fd, &st)) {
+ free(target1);
+ close(fd);
+ return NULL;
+ }
+
+ target2 = libsimple_efreadlink(fd);
+ if (!strends(target2, " (deleted)")) {
+ free(target1);
+ close(fd);
+ return target2;
+ }
+
+ close(fd);
+
+ if (strcmp(target1, target2)) {
+ free(target2);
+ return target1;
+ } else {
+ free(target1);
+ if (st.st_nlink)
+ return target2;
+ target2[strlen(target2) - (sizeof(" (deleted)") - 1U)] = '\0';
+ return target2;
+ }
+}
+
+
+#else
+#include "test.h"
+
+int
+main(void) /* TODO test */
+{
+ return 0;
+}
+
+#endif