void *x_fmmap(const char *fname, off_t *size, const char *errstr);
int x_munmap(void *addr, size_t length);
int x_rename(const char *oldpath, const char *newpath);
+char *x_readlink(const char *path);
/* ------------------------------------------------------------------------- */
/* stats.c */
#endif
return rename(oldpath, newpath);
}
+
+/* Like readlink() but returns the string or NULL on failure. Caller frees. */
+char *
+x_readlink(const char *path)
+{
+ size_t maxlen;
+ ssize_t len;
+ char *buf;
+#ifdef PATH_MAX
+ maxlen = PATH_MAX;
+#elif defined(MAXPATHLEN)
+ maxlen = MAXPATHLEN;
+#elif defined(_PC_PATH_MAX)
+ maxlen = pathconf(path, _PC_PATH_MAX);
+#endif
+ if (maxlen < 4096) maxlen = 4096;
+
+ buf = x_malloc(maxlen);
+ len = readlink(path, buf, maxlen-1);
+ if (len == -1) {
+ free(buf);
+ return NULL;
+ }
+ buf[len] = 0;
+ return buf;
+}