getservbyport_r: fix wrong result if getnameinfo fails with EAI_OVERFLOW
[musl] / src / unistd / readlink.c
index f6b1635..32f4537 100644 (file)
@@ -1,7 +1,19 @@
 #include <unistd.h>
+#include <fcntl.h>
 #include "syscall.h"
 
-int readlink(const char *path, char *buf, size_t bufsize)
+ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize)
 {
-       return syscall3(__NR_readlink, (long)path, (long)buf, bufsize);
+       char dummy[1];
+       if (!bufsize) {
+               buf = dummy;
+               bufsize = 1;
+       }
+#ifdef SYS_readlink
+       int r = __syscall(SYS_readlink, path, buf, bufsize);
+#else
+       int r = __syscall(SYS_readlinkat, AT_FDCWD, path, buf, bufsize);
+#endif
+       if (buf == dummy && r > 0) r = 0;
+       return __syscall_ret(r);
 }