From ff2a6f1b3aefd413bcf31ea7a54597edfbde9adc Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 1 Aug 2010 14:08:42 +0200 Subject: [PATCH] Add x_readlink() utility function --- ccache.h | 1 + util.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/ccache.h b/ccache.h index f9aaf8798..3a650a740 100644 --- 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 7fb7f5435..3bc554226 100644 --- 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; +} -- 2.47.3