aboutsummaryrefslogtreecommitdiffstats
path: root/readmagiclink.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-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