2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-02-07 23:38:21 -02:00
|
|
|
#include <assert.h>
|
2019-06-07 06:49:03 -03:00
|
|
|
#include <errno.h>
|
2021-05-14 15:34:31 -03:00
|
|
|
#include <fcntl.h>
|
2019-02-07 23:38:21 -02:00
|
|
|
#include <stdio.h>
|
2020-01-06 07:05:59 -03:00
|
|
|
#include <string.h>
|
2019-06-07 06:49:03 -03:00
|
|
|
#include <sys/stat.h>
|
2021-02-05 08:16:30 -03:00
|
|
|
#include <syscall.h>
|
2020-07-16 19:01:03 -03:00
|
|
|
#include <unistd.h>
|
2018-11-06 12:45:16 -02:00
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
mode_t umask(mode_t mask)
|
|
|
|
|
{
|
2018-12-21 00:02:06 -02:00
|
|
|
return syscall(SC_umask, mask);
|
2018-11-06 12:45:16 -02:00
|
|
|
}
|
|
|
|
|
|
2018-11-18 11:57:41 -02:00
|
|
|
int mkdir(const char* pathname, mode_t mode)
|
|
|
|
|
{
|
2020-01-06 07:05:59 -03:00
|
|
|
if (!pathname) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int rc = syscall(SC_mkdir, pathname, strlen(pathname), mode);
|
2018-11-18 11:57:41 -02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 01:55:08 -02:00
|
|
|
int chmod(const char* pathname, mode_t mode)
|
|
|
|
|
{
|
2020-01-06 07:05:59 -03:00
|
|
|
if (!pathname) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
int rc = syscall(SC_chmod, pathname, strlen(pathname), mode);
|
2019-01-29 01:55:08 -02:00
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-07 23:38:21 -02:00
|
|
|
int fchmod(int fd, mode_t mode)
|
|
|
|
|
{
|
2019-03-01 06:39:19 -03:00
|
|
|
int rc = syscall(SC_fchmod, fd, mode);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
2019-02-07 23:38:21 -02:00
|
|
|
}
|
2020-07-16 19:01:03 -03:00
|
|
|
|
|
|
|
|
int mkfifo(const char* pathname, mode_t mode)
|
|
|
|
|
{
|
|
|
|
|
return mknod(pathname, mode | S_IFIFO, 0);
|
|
|
|
|
}
|
2020-08-11 13:56:41 -03:00
|
|
|
|
2021-05-14 15:34:31 -03:00
|
|
|
static int do_stat(int dirfd, const char* path, struct stat* statbuf, bool follow_symlinks)
|
2020-08-11 13:56:41 -03:00
|
|
|
{
|
|
|
|
|
if (!path) {
|
|
|
|
|
errno = EFAULT;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2021-09-16 03:45:30 -03:00
|
|
|
Syscall::SC_stat_params params { { path, strlen(path) }, statbuf, dirfd, follow_symlinks };
|
2020-08-11 13:56:41 -03:00
|
|
|
int rc = syscall(SC_stat, ¶ms);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lstat(const char* path, struct stat* statbuf)
|
|
|
|
|
{
|
2021-05-14 15:34:31 -03:00
|
|
|
return do_stat(AT_FDCWD, path, statbuf, false);
|
2020-08-11 13:56:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int stat(const char* path, struct stat* statbuf)
|
|
|
|
|
{
|
2021-05-14 15:34:31 -03:00
|
|
|
return do_stat(AT_FDCWD, path, statbuf, true);
|
2020-08-11 13:56:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fstat(int fd, struct stat* statbuf)
|
|
|
|
|
{
|
|
|
|
|
int rc = syscall(SC_fstat, fd, statbuf);
|
|
|
|
|
__RETURN_WITH_ERRNO(rc, rc, -1);
|
|
|
|
|
}
|
2021-05-14 15:34:31 -03:00
|
|
|
|
|
|
|
|
int fstatat(int fd, const char* path, struct stat* statbuf, int flags)
|
|
|
|
|
{
|
|
|
|
|
return do_stat(fd, path, statbuf, !(flags & AT_SYMLINK_NOFOLLOW));
|
|
|
|
|
}
|
2018-11-06 12:45:16 -02:00
|
|
|
}
|