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
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user