END_TEST
/*******************************************************************************
+ * path_first/last_separator
+ */
+
+static struct {
+ char *path;
+ int len;
+ int first;
+ int last;
+} separator_data[] = {
+ {NULL, -1, -1, -1},
+ {"", -1, -1, -1},
+ {".", -1, -1, -1},
+ {"..", -1, -1, -1},
+#ifdef WIN32
+ {"C:\\", -1, 2, 2},
+ {"C:/", -1, 2, 2},
+ {"X:\\\\", -1, 2, 3},
+ {"d:\\f", -1, 2, 2},
+ {"d:\\f", 2, -1, -1},
+ {"C:\\foo\\", -1, 2, 6},
+ {"foo\\bar", -1, 3, 3},
+ {"foo\\\\bar", -1, 3, 4},
+ {"\\foo\\bar", -1, 0, 4},
+ {"\\\\foo\\bar", -1, 0, 5},
+ {"\\\\foo\\bar", 4, 0, 1},
+ {"foo\\bar/baz", -1, 3, 7},
+#endif /* WIN32 */
+ {"/", -1, 0, 0},
+ {"//", -1, 0, 1},
+ {"foo", -1, -1, -1},
+ {"f/", -1, 1, 1},
+ {"foo/", -1, 3, 3},
+ {"foo/", 2, -1, -1},
+ {"foo//", -1, 3, 4},
+ {"/foo", -1, 0, 0},
+ {"/foo/", -1, 0, 4},
+ {"/foo/", 3, 0, 0},
+ {"//foo/", -1, 0, 5},
+ {"foo/bar", -1, 3, 3},
+ {"foo/bar", 1, -1, -1},
+ {"foo/bar", 2, -1, -1},
+ {"foo/bar", 3, -1, -1},
+ {"foo/bar", 4, 3, 3},
+ {"foo/bar", 5, 3, 3},
+ {"foo//bar", -1, 3, 4},
+ {"/foo/bar", -1, 0, 4},
+ {"/foo/bar/", -1, 0, 8},
+ {"/foo/bar/", 0, -1, -1},
+ {"/foo/bar/", 1, 0, 0},
+ {"/foo/bar/", 2, 0, 0},
+ {"/foo/bar/", 3, 0, 0},
+ {"/foo/bar/", 4, 0, 0},
+ {"/foo/bar/", 5, 0, 4},
+ {"/foo/bar/", 7, 0, 4},
+ {"/foo/bar/", 8, 0, 4},
+ {"/foo/bar/", 9, 0, 8},
+};
+
+START_TEST(test_path_first_separator)
+{
+ char *pos;
+
+ pos = path_first_separator(separator_data[_i].path, separator_data[_i].len);
+ if (separator_data[_i].first >= 0)
+ {
+ ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].first);
+ }
+ else
+ {
+ ck_assert(!pos);
+ }
+}
+END_TEST
+
+START_TEST(test_path_last_separator)
+{
+ char *pos;
+
+ pos = path_last_separator(separator_data[_i].path, separator_data[_i].len);
+ if (separator_data[_i].last >= 0)
+ {
+ ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].last);
+ }
+ else
+ {
+ ck_assert(!pos);
+ }
+}
+END_TEST
+
+/*******************************************************************************
* path_dirname/basename/absolute
*/
{"..", ".", "..", FALSE},
#ifdef WIN32
{"C:\\", "C:", "C:", TRUE},
+ {"C:/", "C:", "C:", TRUE},
{"X:\\\\", "X:", "X:", TRUE},
{"foo", ".", "foo", FALSE},
{"f\\", ".", "f", FALSE},
{"foo\\\\", ".", "foo", FALSE},
{"d:\\f", "d:", "f", TRUE},
{"C:\\f\\", "C:", "f", TRUE},
+ {"C:\\f\\", "C:", "f", TRUE},
{"C:\\foo", "C:", "foo", TRUE},
{"C:\\foo\\", "C:", "foo", TRUE},
+ {"C:\\foo/", "C:", "foo", TRUE},
{"foo\\bar", "foo", "bar", FALSE},
{"foo\\\\bar", "foo", "bar", FALSE},
{"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
{"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
{"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
- {"\\foo\\bar", "\\foo", "bar", FALSE},
+ {"C:\\foo/bar\\baz", "C:\\foo/bar", "baz", TRUE},
+ {"C:/foo/bar/baz", "C:/foo/bar", "baz", TRUE},
+ {"\\foo\\bar", "\\foo", "bar", TRUE},
{"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
-#else /* !WIN32 */
+#endif /* WIN32 */
{"/", "/", "/", TRUE},
{"//", "/", "/", TRUE},
{"foo", ".", "foo", FALSE},
{"/foo/bar", "/foo", "bar", TRUE},
{"/foo/bar/", "/foo", "bar", TRUE},
{"/foo/bar/baz", "/foo/bar", "baz", TRUE},
-#endif
};
START_TEST(test_path_dirname)
tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
suite_add_tcase(s, tc);
+ tc = tcase_create("path_first/last_separator");
+ tcase_add_loop_test(tc, test_path_first_separator, 0, countof(separator_data));
+ tcase_add_loop_test(tc, test_path_last_separator, 0, countof(separator_data));
+ suite_add_tcase(s, tc);
+
tc = tcase_create("path_dirname");
tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
suite_add_tcase(s, tc);
#include <unistd.h>
#include <sys/stat.h>
-/**
- * Described in header.
+/*
+ * Described in header
*/
-char* path_dirname(const char *path)
+char *path_first_separator(const char *path, int len)
+{
+ if (!path)
+ {
+ return NULL;
+ }
+ if (len < 0)
+ {
+ len = strlen(path);
+ }
+ for (; len; path++, len--)
+ {
+ if (path_is_separator(*path))
+ {
+ return (char*)path;
+ }
+ }
+ return NULL;
+}
+
+/*
+ * Described in header
+ */
+char *path_last_separator(const char *path, int len)
+{
+ if (!path)
+ {
+ return NULL;
+ }
+ if (len < 0)
+ {
+ len = strlen(path);
+ }
+ while (len)
+ {
+ if (path_is_separator(path[--len]))
+ {
+ return (char*)&path[len];
+ }
+ }
+ return NULL;
+}
+
+/*
+ * Described in header
+ */
+char *path_dirname(const char *path)
{
char *pos;
- pos = path ? strrchr(path, DIRECTORY_SEPARATOR[0]) : NULL;
+ pos = path_last_separator(path, -1);
if (pos && !pos[1])
- { /* if path ends with slashes we have to look beyond them */
- while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
- { /* skip trailing slashes */
+ { /* if path ends with separators, we have to look beyond them */
+ while (pos > path && path_is_separator(*pos))
+ { /* skip trailing separators */
pos--;
}
- pos = memrchr(path, DIRECTORY_SEPARATOR[0], pos - path + 1);
+ pos = path_last_separator(path, pos - path + 1);
}
if (!pos)
{
#endif
return strdup(".");
}
- while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
- { /* skip superfluous slashes */
+ while (pos > path && path_is_separator(*pos))
+ { /* skip superfluous separators */
pos--;
}
return strndup(path, pos - path + 1);
}
-/**
- * Described in header.
+/*
+ * Described in header
*/
-char* path_basename(const char *path)
+char *path_basename(const char *path)
{
char *pos, *trail = NULL;
{
return strdup(".");
}
- pos = strrchr(path, DIRECTORY_SEPARATOR[0]);
+ pos = path_last_separator(path, -1);
if (pos && !pos[1])
- { /* if path ends with slashes we have to look beyond them */
- while (pos > path && *pos == DIRECTORY_SEPARATOR[0])
- { /* skip trailing slashes */
+ { /* if path ends with separators, we have to look beyond them */
+ while (pos > path && path_is_separator(*pos))
+ { /* skip trailing separators */
pos--;
}
- if (pos == path && *pos == DIRECTORY_SEPARATOR[0])
- { /* contains only slashes */
- return strdup(DIRECTORY_SEPARATOR);
+ if (pos == path && path_is_separator(*pos))
+ { /* contains only separators */
+ return strndup(pos, 1);
}
trail = pos + 1;
- pos = memrchr(path, DIRECTORY_SEPARATOR[0], trail - path);
+ pos = path_last_separator(path, trail - path);
}
pos = pos ? pos + 1 : (char*)path;
return trail ? strndup(pos, trail - pos) : strdup(pos);
}
-/**
- * Described in header.
+/*
+ * Described in header
*/
bool path_absolute(const char *path)
{
{ /* drive letter */
return TRUE;
}
-#else /* !WIN32 */
- if (path[0] == DIRECTORY_SEPARATOR[0])
+#endif /* WIN32 */
+ if (path_is_separator(path[0]))
{
return TRUE;
}
-#endif
return FALSE;
}
-/**
- * Described in header.
+/*
+ * Described in header
*/
bool mkdir_p(const char *path, mode_t mode)
{
int len;
- char *pos, full[PATH_MAX];
+ char *pos, sep, full[PATH_MAX];
pos = full;
if (!path || *path == '\0')
{
DBG1(DBG_LIB, "path string %s too long", path);
return FALSE;
}
- /* ensure that the path ends with a '/' */
- if (full[len-1] != '/')
+ /* ensure that the path ends with a separator */
+ if (!path_is_separator(full[len-1]))
{
- full[len++] = '/';
+ full[len++] = DIRECTORY_SEPARATOR[0];
full[len] = '\0';
}
- /* skip '/' at the beginning */
- while (*pos == '/')
+ /* skip separators at the beginning */
+ while (path_is_separator(*pos))
{
pos++;
}
- while ((pos = strchr(pos, '/')))
+ while ((pos = path_first_separator(pos, -1)))
{
+ sep = *pos;
*pos = '\0';
if (access(full, F_OK) < 0)
{
return FALSE;
}
}
- *pos = '/';
+ *pos = sep;
pos++;
}
return TRUE;