From 76b8e0a26b8c62b9659313c78756daf914baddd9 Mon Sep 17 00:00:00 2001 From: John Hein Date: Tue, 24 Jan 2017 23:30:58 -0700 Subject: [PATCH] Normalize the path to remove duplicate / separators rather than possibly altering the path with realpath(3). Leave sym links as is in path components. This is also cheaper than realpath(3), and works on platforms that don't have realpath(3). Note: if this is accepted, the check for realpath in configure.ac can be removed, and some docs that mention realpath will be adjusted. --- libpkgconf/path.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/libpkgconf/path.c b/libpkgconf/path.c index 4d59b33..acb1cc1 100644 --- a/libpkgconf/path.c +++ b/libpkgconf/path.c @@ -231,12 +231,39 @@ pkgconf_path_free(pkgconf_list_t *dirlist) } } +static char * +normpath(const char *path) +{ + if (!path) + return NULL; + + char *copy = strdup(path); + if (NULL == copy) + return NULL; + char *ptr = copy; + + for (int ii = 0; copy[ii]; ii++) + { + *ptr++ = path[ii]; + if ('/' == path[ii]) + { + ii++; + while ('/' == path[ii]) + ii++; + ii--; + } + } + *ptr = '\0'; + + return copy; +} + /* * !doc * * .. c:function:: bool pkgconf_path_relocate(char *buf, size_t buflen) * - * Relocates a path, possibly calling realpath() or cygwin_conv_path() on it. + * Relocates a path, possibly calling normpath() or cygwin_conv_path() on it. * * :param char* buf: The path to relocate. * :param size_t buflen: The buffer length the path is contained in. @@ -267,10 +294,10 @@ pkgconf_path_relocate(char *buf, size_t buflen) if (*ti == '\\') *ti = '/'; } -#elif defined(HAVE_REALPATH) +#else char *tmpbuf; - if ((tmpbuf = realpath(buf, NULL)) != NULL) + if ((tmpbuf = normpath(buf)) != NULL) { size_t tmpbuflen = strlen(tmpbuf); if (tmpbuflen > buflen) @@ -282,9 +309,6 @@ pkgconf_path_relocate(char *buf, size_t buflen) pkgconf_strlcpy(buf, tmpbuf, buflen); free(tmpbuf); } -#else - (void) buf; - (void) buflen; #endif return true;