]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add x_readlink() utility function
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 1 Aug 2010 12:08:42 +0000 (14:08 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 1 Aug 2010 15:20:33 +0000 (17:20 +0200)
ccache.h
util.c

index f9aaf879835df473ca5b4a822c0025b93e609cf0..3a650a740dc389510bc56ad4975f022933902cec 100644 (file)
--- a/ccache.h
+++ b/ccache.h
@@ -123,6 +123,7 @@ void update_mtime(const char *path);
 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 */
diff --git a/util.c b/util.c
index 7fb7f54350d3785f4bcd6ddf7ecf2ed65677ec0c..3bc5542266c64b15fcb121ef9d02f7efc819500a 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1114,3 +1114,29 @@ int x_rename(const char *oldpath, const char *newpath)
 #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;
+}