headers/bsd: Add sys/queue.h.
[haiku.git] / src / system / libroot / posix / utime.c
blob5fa8d264029a4cdc0f122a2a26de50824624283c
1 /*
2 * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <utime.h>
9 #include <errno.h>
10 #include <time.h>
12 #include <NodeMonitor.h>
14 #include <errno_private.h>
15 #include <syscalls.h>
18 #define RETURN_AND_SET_ERRNO(err) \
19 if (err < 0) { \
20 __set_errno(err); \
21 return -1; \
22 } \
23 return err;
26 int
27 utime(const char *path, const struct utimbuf *times)
29 struct stat stat;
30 status_t status;
32 if (times != NULL) {
33 stat.st_atim.tv_sec = times->actime;
34 stat.st_mtim.tv_sec = times->modtime;
35 stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = 0;
36 } else {
37 bigtime_t now = real_time_clock_usecs();
38 stat.st_atim.tv_sec = stat.st_mtim.tv_sec = now / 1000000;
39 stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = (now % 1000000) * 1000;
42 status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
43 B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME);
45 RETURN_AND_SET_ERRNO(status);