diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-18 10:07:51 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-18 10:07:51 +0200 |
commit | 2de8dae44c40d97e3947070353c1db709a35f92c (patch) | |
tree | 13a9827273a54b2a9235135c84eae735ced05009 /readmagiclink.c | |
parent | Add @since for definitions added in version 1.5 (diff) | |
parent | Add missing .c files (diff) | |
download | libsimple-2de8dae44c40d97e3947070353c1db709a35f92c.tar.gz libsimple-2de8dae44c40d97e3947070353c1db709a35f92c.tar.bz2 libsimple-2de8dae44c40d97e3947070353c1db709a35f92c.tar.xz |
Merge tag '1.6' into since
Version 1.6
Diffstat (limited to '')
-rw-r--r-- | readmagiclink.c | 61 |
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 |