#endif
#if defined(HAVE_WINDOWS_H) && !defined(__CYGWIN__)
#include <windows.h>
-#define DIRSEP '\\'
-#else
-#define DIRSEP '/'
#endif
#include "tree.h"
p = t->buff + t->dirname_length;
t->path_length = t->dirname_length + name_length;
/* Add a separating '/' if it's needed. */
- if (t->dirname_length > 0 && p[-1] != '/' && p[-1] != '\\') {
- *p++ = DIRSEP;
+ if (t->dirname_length > 0 && p[-1] != '/') {
+ *p++ = '/';
t->path_length ++;
}
#if HAVE_STRNCPY_S
t->dirname_length = te->dirname_length;
if (t->buff) {
t->basename = t->buff + t->dirname_length;
- while (t->basename[0] == '/' || t->basename[0] == '\\')
+ while (t->basename[0] == '/')
t->basename++;
}
free(te->name);
}
const char *
-strip_components(const char *path, int elements)
+strip_components(const char *p, int elements)
{
- const char *p = path;
-
+ /* Skip as many elements as necessary. */
while (elements > 0) {
switch (*p++) {
case '/':
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ case '\\': /* Support \ path sep on Windows ONLY. */
+#endif
elements--;
- path = p;
break;
case '\0':
/* Path is too short, skip it. */
}
}
- while (*path == '/')
- ++path;
- if (*path == '\0')
- return (NULL);
-
- return (path);
+ /* Skip any / characters. This handles short paths that have
+ * additional / termination. This also handles the case where
+ * the logic above stops in the middle of a duplicate //
+ * sequence (which would otherwise get converted to an
+ * absolute path). */
+ for (;;) {
+ switch (*p) {
+ case '/':
+#if defined(_WIN32) && !defined(__CYGWIN__)
+ case '\\': /* Support \ path sep on Windows ONLY. */
+#endif
+ ++p;
+ break;
+ case '\0':
+ return (NULL);
+ default:
+ return (p);
+ }
+ }
}
/*