diff --git a/src/tests/add-ons/kernel/file_systems/fs_shell/path_util.cpp b/src/tests/add-ons/kernel/file_systems/fs_shell/path_util.cpp new file mode 100644 index 0000000000..ce848c12a3 --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fs_shell/path_util.cpp @@ -0,0 +1,70 @@ +/* + * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. + * Distributed under the terms of the MIT License. + */ + +#include "compat.h" + +#include +#include + +#include "path_util.h" + +status_t +get_last_path_component(const char *path, char *buffer, int bufferLen) +{ + int len = strlen(path); + if (len == 0) + return FS_EINVAL; + + // eat trailing '/' + while (len > 0 && path[len - 1] == '/') + len--; + + if (len == 0) { + // path is `/' + len = 1; + } else { + // find previous '/' + int pos = len - 1; + while (pos > 0 && path[pos] != '/') + pos--; + if (path[pos] == '/') + pos++; + + path += pos; + len -= pos; + } + + if (len >= bufferLen) + return FS_NAME_TOO_LONG; + + memcpy(buffer, path, len); + buffer[len] = '\0'; + return FS_OK; +} + +char * +make_path(const char *dir, const char *entry) +{ + // get the len + int dirLen = strlen(dir); + int entryLen = strlen(entry); + bool insertSeparator = (dir[dirLen - 1] != '/'); + int pathLen = dirLen + entryLen + (insertSeparator ? 1 : 0) + 1; + + // allocate the path + char *path = (char*)malloc(pathLen); + if (!path) + return NULL; + + // compose the path + strcpy(path, dir); + if (insertSeparator) + strcat(path + dirLen, "/"); + strcat(path + dirLen, entry); + + return path; +} + + diff --git a/src/tests/add-ons/kernel/file_systems/fs_shell/path_util.h b/src/tests/add-ons/kernel/file_systems/fs_shell/path_util.h new file mode 100644 index 0000000000..f6480704f8 --- /dev/null +++ b/src/tests/add-ons/kernel/file_systems/fs_shell/path_util.h @@ -0,0 +1,20 @@ +/* + * Copyright 2005, Ingo Weinhold, bonefish@users.sf.net. + * Distributed under the terms of the MIT License. + */ + +#ifndef FS_SHELL_PATH_UTIL_H +#define FS_SHELL_PATH_UTIL_H + +#ifdef __cplusplus +extern "C" { +#endif + +status_t get_last_path_component(const char *path, char *buffer, int bufferLen); +char *make_path(const char *dir, const char *entry); + +#ifdef __cplusplus +} +#endif + +#endif // FS_SHELL_PATH_UTIL_H