xref: /haiku/src/system/libroot/posix/utime.c (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
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 <syscalls.h>
15 
16 
17 #define RETURN_AND_SET_ERRNO(err) \
18 	if (err < 0) { \
19 		errno = err; \
20 		return -1; \
21 	} \
22 	return err;
23 
24 
25 int
26 utime(const char *path, const struct utimbuf *times)
27 {
28 	struct stat stat;
29 	status_t status;
30 
31 	if (times != NULL) {
32 		stat.st_atim.tv_sec = times->actime;
33 		stat.st_mtim.tv_sec = times->modtime;
34 		stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = 0;
35 	} else {
36 		bigtime_t now = real_time_clock_usecs();
37 		stat.st_atim.tv_sec = stat.st_mtim.tv_sec = now / 1000000;
38 		stat.st_atim.tv_nsec = stat.st_mtim.tv_nsec = (now % 1000000) * 1000;
39 	}
40 
41 	status = _kern_write_stat(-1, path, true, &stat, sizeof(struct stat),
42 		B_STAT_MODIFICATION_TIME | B_STAT_ACCESS_TIME);
43 
44 	RETURN_AND_SET_ERRNO(status);
45 }
46 
47