Files
haiku-beta6/src/tools/fs_shell/path_util.cpp
T
Ingo Weinhold 4f7504e31e * The command functions return actual error codes now.
* Added commands ln, mkdir, mkindex, rm.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@20882 a95241bf-73f2-0310-859d-f6bbb57e9c96
2007-04-28 13:13:30 +00:00

76 lines
1.3 KiB
C++

/*
* Copyright 2005-2007, Ingo Weinhold, [email protected].
* Distributed under the terms of the MIT License.
*/
#include "path_util.h"
#include <stdlib.h>
#include <string.h>
#include "fssh_errors.h"
namespace FSShell {
fssh_status_t
get_last_path_component(const char *path, char *buffer, int bufferLen)
{
int len = strlen(path);
if (len == 0)
return FSSH_B_BAD_VALUE;
// 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 FSSH_B_NAME_TOO_LONG;
memcpy(buffer, path, len);
buffer[len] = '\0';
return FSSH_B_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;
}
} // namespace FSShell