xref: /haiku/src/system/libroot/posix/utime.c (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
1 /*
2  * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <utime.h>
8 
9 #include <errno.h>
10 #include <time.h>
11 
12 #include <NodeMonitor.h>
13 
14 #include <errno_private.h>
15 #include <syscalls.h>
16 #include <syscall_utils.h>
17 
18 
19 int
20 utime(const char *path, const struct utimbuf *times)
21 {
22 	struct stat stat;
23 	status_t status;
24 
25 	if (times != NULL) {
26 		stat.st_atim.tv_sec = times->actime;
27 		stat.st_mtim.tv_sec = times->modtime;
28 		stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = 0;
29 	} else {
30 		bigtime_t now = real_time_clock_usecs();
31 		stat.st_atim.tv_sec = stat.st_mtim.tv_sec = now / 1000000;
32 		stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = (now % 1000000) * 1000;
33 	}
34 
35 	status = _kern_write_stat(AT_FDCWD, path, true, &stat, sizeof(struct stat),
36 		B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME);
37 
38 	RETURN_AND_SET_ERRNO(status);
39 }
40 
41