From: Joel Rosdahl Date: Mon, 2 Aug 2010 20:16:22 +0000 (+0200) Subject: Extract calculation of the maximum path length into a local function X-Git-Tag: v3.1~106 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f883d1e4178e0cd464cb25041f47f484dbb6078;p=thirdparty%2Fccache.git Extract calculation of the maximum path length into a local function This also fixes a bug in x_readlink where maxlen was unsigned, which leads to problems when pathconf() returns -1. --- diff --git a/util.c b/util.c index 8e4ecbd6c..664c03581 100644 --- a/util.c +++ b/util.c @@ -778,6 +778,25 @@ value_units(const char *s) } #ifndef _WIN32 +static long +path_max(const char *path) +{ +#ifdef PATH_MAX + (void)path; + return PATH_MAX; +#elif defined(MAXPATHLEN) + (void)path; + return MAXPATHLEN; +#elif defined(_PC_PATH_MAX) + long maxlen = pathconf(path, _PC_PATH_MAX); + if (maxlen >= 4096) { + return maxlen; + } else { + return 4096; + } +#endif +} + /* a sane realpath() function, trying to cope with stupid path limits and a broken API @@ -785,16 +804,8 @@ value_units(const char *s) char * x_realpath(const char *path) { - int maxlen; + long maxlen = path_max(path); char *ret, *p; -#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; ret = x_malloc(maxlen); @@ -1122,7 +1133,7 @@ x_rename(const char *oldpath, const char *newpath) char * x_readlink(const char *path) { - size_t maxlen; + long maxlen = path_max(path); ssize_t len; char *buf; #ifdef PATH_MAX