Skip to content

Commit

Permalink
Normalize the path to remove duplicate / separators rather than
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
John Hein committed Jan 25, 2017
1 parent badcd8d commit 4cb900d
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions libpkgconf/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 4cb900d

Please sign in to comment.