Compare commits
No commits in common. "dd358eca290cc792fbecfd7b9bbe237a023d3fa2" and "6953a28ce8b59f6fcd1a8166d655a448b8678a85" have entirely different histories.
dd358eca29
...
6953a28ce8
@ -6,13 +6,8 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Returns the last component of a path. This function is allowed to modify the string passed to it, so it should
|
char* basename(char*); // Not implemented.
|
||||||
* probably be a copy of another string. */
|
char* dirname(char*); // Not implemented.
|
||||||
char* basename(char* path);
|
|
||||||
|
|
||||||
/* Returns the parent directory of a path. This function is allowed to modify the string passed to it, so it should
|
|
||||||
* probably be a copy of another string. */
|
|
||||||
char* dirname(char* path);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -1,50 +1,15 @@
|
|||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
#include <string.h>
|
#include <luna.h>
|
||||||
|
|
||||||
static char dot[] = ".";
|
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
{
|
{
|
||||||
char* basename(char* path)
|
char* basename(char*)
|
||||||
{
|
{
|
||||||
// If path is NULL, or the string's length is 0, return .
|
NOT_IMPLEMENTED("basename");
|
||||||
if (!path) return dot;
|
|
||||||
size_t len = strlen(path);
|
|
||||||
if (!len) return dot;
|
|
||||||
|
|
||||||
// Strip trailing slashes.
|
|
||||||
char* it = path + len - 1;
|
|
||||||
while (*it == '/' && it != path) { it--; }
|
|
||||||
*(it + 1) = 0;
|
|
||||||
if (it == path) return path;
|
|
||||||
|
|
||||||
// Return path from the first character if there are no more slashes, or from the first character after the last
|
|
||||||
// slash.
|
|
||||||
char* beg = strrchr(path, '/');
|
|
||||||
if (!beg) return path;
|
|
||||||
return beg + 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char* dirname(char* path)
|
char* dirname(char*)
|
||||||
{
|
{
|
||||||
// If path is NULL, or the string's length is 0, return .
|
NOT_IMPLEMENTED("dirname");
|
||||||
if (!path) return dot;
|
|
||||||
size_t len = strlen(path);
|
|
||||||
if (!len) return dot;
|
|
||||||
|
|
||||||
// Strip trailing slashes.
|
|
||||||
char* it = path + len - 1;
|
|
||||||
while (*it == '/' && it != path) { it--; }
|
|
||||||
*(char*)(it + 1) = 0;
|
|
||||||
if (it == path) return path;
|
|
||||||
|
|
||||||
// Search for the last slash. If there is none, return .
|
|
||||||
// Otherwise, we end the string there and return.
|
|
||||||
char* end = strrchr(path, '/');
|
|
||||||
if (!end) return dot;
|
|
||||||
if (end != path) *end = 0;
|
|
||||||
else
|
|
||||||
*(end + 1) = 0;
|
|
||||||
return path;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -136,9 +136,8 @@ extern "C"
|
|||||||
char* strrchr(const char* str, int c)
|
char* strrchr(const char* str, int c)
|
||||||
{
|
{
|
||||||
const char* s = str + strlen(str);
|
const char* s = str + strlen(str);
|
||||||
while (s != str && *s != (char)c) s--;
|
while (s != str && *str != (char)c) str--;
|
||||||
if (s != str) return const_cast<char*>(s);
|
if (s != str) return const_cast<char*>(s);
|
||||||
if (*s == (char)c) return const_cast<char*>(s);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user