aboutsummaryrefslogtreecommitdiffstats
path: root/src/misc.c
diff options
context:
space:
mode:
authorMartin Pärtel <martin.partel@gmail.com>2012-09-14 16:59:49 +0300
committerMartin Pärtel <martin.partel@gmail.com>2012-09-14 16:59:49 +0300
commitf6161698827488cc21f3f3726d5af0cb2a89ce4b (patch)
treeb7ebdfdfe8f9a70905a0878415e768f66200d953 /src/misc.c
parent66f00111c4e886e244152e460ada62164c975d4d (diff)
downloadbindfs-f6161698827488cc21f3f3726d5af0cb2a89ce4b.tar.gz
Implemented my_dirname and added unit tests for it.
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index c130b86..bb838c7 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -82,6 +82,32 @@ const char *my_basename(const char *path)
return path;
}
+const char *my_dirname(char *path)
+{
+ if (strcmp(path, ".") == 0) {
+ return "..";
+ } else if (strcmp(path, "/") == 0) {
+ return "/";
+ } else {
+ int len = strlen(path);
+ char *p = path + len - 1;
+ while (p > path) {
+ if (*p == '/') {
+ break;
+ }
+ --p;
+ }
+ if (p > path) {
+ *p = '\0';
+ return path;
+ } else if (*path == '/') {
+ return "/";
+ } else {
+ return ".";
+ }
+ }
+}
+
void grow_array_impl(void **array, int* capacity, int member_size)
{
int new_cap = *capacity;