Kernel: Make a more convenient wrapper for basename() and dirname()

This commit is contained in:
apio 2022-11-12 10:39:25 +01:00
parent 5a1d15d2b5
commit 559d074ce4
3 changed files with 32 additions and 15 deletions

View File

@ -1,4 +1,7 @@
#pragma once
char* basename(char* path);
char* dirname(char* path);
char* dirname(char* path);
char* better_basename(const char* path);
char* better_dirname(const char* path);

View File

@ -208,36 +208,30 @@ int VFS::do_mkdir(const char* path, const char* name, int uid, int gid, mode_t m
int VFS::mkdir(const char* pathname)
{
char* bstr = strdup(pathname);
char* dstr = strdup(pathname);
char* base = basename(bstr);
char* dir = dirname(dstr);
char* base = better_basename(pathname);
char* dir = better_dirname(pathname);
kdbgln("mkdir(): creating %s in directory %s", base, dir);
int result = mkdir(dir, base);
kfree(bstr);
kfree(dstr);
kfree(base);
kfree(dir);
return result;
}
int VFS::do_mkdir(const char* pathname, int uid, int gid, mode_t mode)
{
char* bstr = strdup(pathname);
char* dstr = strdup(pathname);
char* base = basename(bstr);
char* dir = dirname(dstr);
char* base = better_basename(pathname);
char* dir = better_dirname(pathname);
kdbgln("mkdir(): creating %s in directory %s", base, dir);
int result = do_mkdir(dir, base, uid, gid, mode);
kfree(bstr);
kfree(dstr);
kfree(base);
kfree(dir);
return result;
}

View File

@ -1,4 +1,5 @@
#include "std/libgen.h"
#include "std/stdlib.h"
#include "std/string.h"
static char dot[] = ".";
@ -44,4 +45,23 @@ char* dirname(char* path)
else
*(end + 1) = 0;
return path;
}
char* better_basename(const char* path)
{
char* mut = strdup(path); // create a mutable copy of path
char* ptr = basename(mut); // get a pointer to the start of the base name
char* result = strdup(ptr); // create a copy of it, so the caller can free the returned pointer directly
kfree(mut); // free the original mutable copy
return result;
}
char* better_dirname(const char* path)
{
// same as above
char* mut = strdup(path);
char* ptr = dirname(mut);
char* result = strdup(ptr);
kfree(mut);
return result;
}