Luna/libc/src/utime.cpp
apio a92077d311
All checks were successful
continuous-integration/drone/push Build is passing
kernel+libc: Add all variants of utime
2023-08-08 14:14:35 +02:00

56 lines
1.6 KiB
C++

#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <utime.h>
extern "C"
{
int utime(const char* path, const struct utimbuf* buf)
{
if (!buf) return utimensat(AT_FDCWD, path, nullptr, 0);
struct timespec times[2] = {
{ .tv_sec = buf->actime, .tv_nsec = 0 },
{ .tv_sec = buf->modtime, .tv_nsec = 0 },
};
return utimensat(AT_FDCWD, path, times, 0);
}
int utimes(const char* path, const struct timeval* buf)
{
if (!buf) return utimensat(AT_FDCWD, path, nullptr, 0);
struct timespec times[2] = {
{ .tv_sec = buf[0].tv_sec, .tv_nsec = buf[0].tv_usec * 1000 },
{ .tv_sec = buf[1].tv_sec, .tv_nsec = buf[1].tv_usec * 1000 },
};
return utimensat(AT_FDCWD, path, times, 0);
}
int futimes(int fd, const struct timeval* buf)
{
if (!buf) return utimensat(fd, "", nullptr, AT_EMPTY_PATH);
struct timespec times[2] = {
{ .tv_sec = buf[0].tv_sec, .tv_nsec = buf[0].tv_usec * 1000 },
{ .tv_sec = buf[1].tv_sec, .tv_nsec = buf[1].tv_usec * 1000 },
};
return utimensat(fd, "", times, AT_EMPTY_PATH);
}
int lutimes(const char* path, const struct timeval* buf)
{
if (!buf) return utimensat(AT_FDCWD, path, nullptr, AT_SYMLINK_NOFOLLOW);
struct timespec times[2] = {
{ .tv_sec = buf[0].tv_sec, .tv_nsec = buf[0].tv_usec * 1000 },
{ .tv_sec = buf[1].tv_sec, .tv_nsec = buf[1].tv_usec * 1000 },
};
return utimensat(AT_FDCWD, path, times, AT_SYMLINK_NOFOLLOW);
}
}