xref: /haiku/src/system/libroot/posix/sys/utimes.c (revision 6a9eee584523d4c7429d6c7f19d2fa525519c14e)
1 /*
2  * Copyright 2006-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <sys/time.h>
8 
9 #include <errno.h>
10 
11 #include <NodeMonitor.h>
12 
13 #include <syscalls.h>
14 
15 
16 #define RETURN_AND_SET_ERRNO(err) \
17 	if (err < 0) { \
18 		errno = err; \
19 		return -1; \
20 	} \
21 	return err;
22 
23 
24 int
25 utimes(const char* path, const struct timeval times[2])
26 {
27 	struct stat stat;
28 	status_t status;
29 
30 	if (times != NULL) {
31 		stat.st_atim.tv_sec = times[0].tv_sec;
32 		stat.st_atim.tv_nsec = times[0].tv_usec * 1000;
33 
34 		stat.st_mtim.tv_sec = times[1].tv_sec;
35 		stat.st_mtim.tv_nsec = times[1].tv_usec * 1000;
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;
40 	}
41 
42 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
43 		B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME);
44 
45 	RETURN_AND_SET_ERRNO(status);
46 }
47 
48