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.
pull/116/head
John Hein 2017-01-24 23:30:58 -07:00 committed by William Pitcock
parent d558e30ab3
commit 76b8e0a26b
1 changed files with 30 additions and 6 deletions

View File

@ -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 * !doc
* *
* .. c:function:: bool pkgconf_path_relocate(char *buf, size_t buflen) * .. 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 char* buf: The path to relocate.
* :param size_t buflen: The buffer length the path is contained in. * :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 == '\\') if (*ti == '\\')
*ti = '/'; *ti = '/';
} }
#elif defined(HAVE_REALPATH) #else
char *tmpbuf; char *tmpbuf;
if ((tmpbuf = realpath(buf, NULL)) != NULL) if ((tmpbuf = normpath(buf)) != NULL)
{ {
size_t tmpbuflen = strlen(tmpbuf); size_t tmpbuflen = strlen(tmpbuf);
if (tmpbuflen > buflen) if (tmpbuflen > buflen)
@ -282,9 +309,6 @@ pkgconf_path_relocate(char *buf, size_t buflen)
pkgconf_strlcpy(buf, tmpbuf, buflen); pkgconf_strlcpy(buf, tmpbuf, buflen);
free(tmpbuf); free(tmpbuf);
} }
#else
(void) buf;
(void) buflen;
#endif #endif
return true; return true;