libc: Add syscall wrappers for unlinkat() and openat()
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-05-05 18:50:35 +02:00
parent d9d8f7cdc7
commit 349ba0acb1
Signed by: apio
GPG Key ID: B8A7D06E42258954
4 changed files with 27 additions and 4 deletions

View File

@ -16,6 +16,9 @@ extern "C"
/* Open a file path and return a file descriptor to it. */ /* Open a file path and return a file descriptor to it. */
int open(const char* path, int flags, ...); int open(const char* path, int flags, ...);
/* Open a file path relative to a file descriptor and return a file descriptor to it. */
int openat(int dirfd, const char* path, int flags, ...);
/* Create a file and return a file descriptor to it. */ /* Create a file and return a file descriptor to it. */
int creat(const char* path, mode_t mode); int creat(const char* path, mode_t mode);

View File

@ -115,6 +115,9 @@ extern "C"
/* Remove a name from the filesystem. */ /* Remove a name from the filesystem. */
int unlink(const char* path); int unlink(const char* path);
/* Remove a name or directory from the filesystem. */
int unlinkat(int dirfd, const char* path, int flags);
/* Remove a directory from the filesystem. */ /* Remove a directory from the filesystem. */
int rmdir(const char* path); int rmdir(const char* path);

View File

@ -19,9 +19,22 @@ extern "C"
__errno_return(rc, int); __errno_return(rc, int);
} }
int openat(int dirfd, const char* path, int flags, ...)
{
va_list ap;
va_start(ap, flags);
mode_t mode = (mode_t)va_arg(ap, int);
long rc = syscall(SYS_openat, dirfd, path, flags, mode);
va_end(ap);
__errno_return(rc, int);
}
int creat(const char* path, mode_t mode) int creat(const char* path, mode_t mode)
{ {
return open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); return openat(AT_FDCWD, path, O_WRONLY | O_CREAT | O_TRUNC, mode);
} }
int fcntl(int fd, int cmd, ...) int fcntl(int fd, int cmd, ...)

View File

@ -370,14 +370,18 @@ extern "C"
int unlink(const char* path) int unlink(const char* path)
{ {
long rc = syscall(SYS_unlinkat, AT_FDCWD, path, 0); return unlinkat(AT_FDCWD, path, 0);
}
int unlinkat(int dirfd, const char* path, int flags)
{
long rc = syscall(SYS_unlinkat, dirfd, path, flags);
__errno_return(rc, int); __errno_return(rc, int);
} }
int rmdir(const char* path) int rmdir(const char* path)
{ {
long rc = syscall(SYS_unlinkat, AT_FDCWD, path, AT_REMOVEDIR); return unlinkat(AT_FDCWD, path, AT_REMOVEDIR);
__errno_return(rc, int);
} }
int gethostname(char* buf, size_t len) int gethostname(char* buf, size_t len)