utils: Provide a path_absolute() function to check path for non-relativeness

The usually used trivial '/' check won't work on Windows platforms.
This commit is contained in:
Martin Willi
2014-06-04 15:53:09 +02:00
parent 2496eaffde
commit 67b3bcd13d
3 changed files with 88 additions and 38 deletions
+27
View File
@@ -288,6 +288,33 @@ char* path_basename(const char *path)
return trail ? strndup(pos, trail - pos) : strdup(pos);
}
/**
* Described in header.
*/
bool path_absolute(const char *path)
{
if (!path)
{
return FALSE;
}
#ifdef WIN32
if (strpfx(path, "\\\\"))
{ /* UNC */
return TRUE;
}
if (strlen(path) && isalpha(path[0]) && path[1] == ':')
{ /* drive letter */
return TRUE;
}
#else /* !WIN32 */
if (path[0] == DIRECTORY_SEPARATOR[0])
{
return TRUE;
}
#endif
return FALSE;
}
/**
* Described in header.
*/
+8
View File
@@ -568,6 +568,14 @@ char *path_dirname(const char *path);
*/
char *path_basename(const char *path);
/**
* Check if a given path is absolute.
*
* @param path path to check
* @return TRUE if absolute, FALSE if relative
*/
bool path_absolute(const char *path);
/**
* Creates a directory and all required parent directories.
*