Kernel: Make a more convenient wrapper for basename() and dirname()
This commit is contained in:
parent
5a1d15d2b5
commit
559d074ce4
@ -1,4 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
char* basename(char* path);
|
char* basename(char* path);
|
||||||
char* dirname(char* path);
|
char* dirname(char* path);
|
||||||
|
|
||||||
|
char* better_basename(const char* path);
|
||||||
|
char* better_dirname(const char* path);
|
@ -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)
|
int VFS::mkdir(const char* pathname)
|
||||||
{
|
{
|
||||||
char* bstr = strdup(pathname);
|
char* base = better_basename(pathname);
|
||||||
char* dstr = strdup(pathname);
|
char* dir = better_dirname(pathname);
|
||||||
|
|
||||||
char* base = basename(bstr);
|
|
||||||
char* dir = dirname(dstr);
|
|
||||||
|
|
||||||
kdbgln("mkdir(): creating %s in directory %s", base, dir);
|
kdbgln("mkdir(): creating %s in directory %s", base, dir);
|
||||||
|
|
||||||
int result = mkdir(dir, base);
|
int result = mkdir(dir, base);
|
||||||
|
|
||||||
kfree(bstr);
|
kfree(base);
|
||||||
kfree(dstr);
|
kfree(dir);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int VFS::do_mkdir(const char* pathname, int uid, int gid, mode_t mode)
|
int VFS::do_mkdir(const char* pathname, int uid, int gid, mode_t mode)
|
||||||
{
|
{
|
||||||
char* bstr = strdup(pathname);
|
char* base = better_basename(pathname);
|
||||||
char* dstr = strdup(pathname);
|
char* dir = better_dirname(pathname);
|
||||||
|
|
||||||
char* base = basename(bstr);
|
|
||||||
char* dir = dirname(dstr);
|
|
||||||
|
|
||||||
kdbgln("mkdir(): creating %s in directory %s", base, dir);
|
kdbgln("mkdir(): creating %s in directory %s", base, dir);
|
||||||
|
|
||||||
int result = do_mkdir(dir, base, uid, gid, mode);
|
int result = do_mkdir(dir, base, uid, gid, mode);
|
||||||
|
|
||||||
kfree(bstr);
|
kfree(base);
|
||||||
kfree(dstr);
|
kfree(dir);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#include "std/libgen.h"
|
#include "std/libgen.h"
|
||||||
|
#include "std/stdlib.h"
|
||||||
#include "std/string.h"
|
#include "std/string.h"
|
||||||
|
|
||||||
static char dot[] = ".";
|
static char dot[] = ".";
|
||||||
@ -44,4 +45,23 @@ char* dirname(char* path)
|
|||||||
else
|
else
|
||||||
*(end + 1) = 0;
|
*(end + 1) = 0;
|
||||||
return path;
|
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;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user