xref: /minix3/minix/lib/libc/sys/utimensat.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc #include <sys/cdefs.h>
2*433d6423SLionel Sambuc #include "namespace.h"
3*433d6423SLionel Sambuc #include <lib.h>
4*433d6423SLionel Sambuc 
5*433d6423SLionel Sambuc #include <sys/stat.h>
6*433d6423SLionel Sambuc #include <fcntl.h>
7*433d6423SLionel Sambuc #include <string.h>
8*433d6423SLionel Sambuc #include <limits.h>
9*433d6423SLionel Sambuc #include <errno.h>
10*433d6423SLionel Sambuc 
11*433d6423SLionel Sambuc /* Implement a very large but not complete subset of the utimensat()
12*433d6423SLionel Sambuc  * Posix:2008/XOpen-7 function.
13*433d6423SLionel Sambuc  * Are handled the following cases:
14*433d6423SLionel Sambuc  * . utimensat(AT_FDCWD, "/some/absolute/path", , )
15*433d6423SLionel Sambuc  * . utimensat(AT_FDCWD, "some/path", , )
16*433d6423SLionel Sambuc  * . utimensat(fd, "/some/absolute/path", , ) although fd is useless here
17*433d6423SLionel Sambuc  * Are not handled the following cases:
18*433d6423SLionel Sambuc  * . utimensat(fd, "some/path", , ) path to a file relative to some open fd
19*433d6423SLionel Sambuc  */
utimensat(int fd,const char * name,const struct timespec tv[2],int flags)20*433d6423SLionel Sambuc int utimensat(int fd, const char *name, const struct timespec tv[2],
21*433d6423SLionel Sambuc     int flags)
22*433d6423SLionel Sambuc {
23*433d6423SLionel Sambuc   message m;
24*433d6423SLionel Sambuc   static const struct timespec now[2] = { {0, UTIME_NOW}, {0, UTIME_NOW} };
25*433d6423SLionel Sambuc 
26*433d6423SLionel Sambuc   if (tv == NULL) tv = now;
27*433d6423SLionel Sambuc 
28*433d6423SLionel Sambuc   if (name == NULL) {
29*433d6423SLionel Sambuc 	errno = EINVAL;
30*433d6423SLionel Sambuc 	return -1;
31*433d6423SLionel Sambuc   }
32*433d6423SLionel Sambuc   if (name[0] == '\0') { /* POSIX requirement */
33*433d6423SLionel Sambuc 	errno = ENOENT;
34*433d6423SLionel Sambuc 	return -1;
35*433d6423SLionel Sambuc   }
36*433d6423SLionel Sambuc   if (fd != AT_FDCWD && name[0] != '/') { /* Not supported */
37*433d6423SLionel Sambuc 	errno = EINVAL;
38*433d6423SLionel Sambuc 	return -1;
39*433d6423SLionel Sambuc   }
40*433d6423SLionel Sambuc 
41*433d6423SLionel Sambuc   if ((unsigned)flags > SHRT_MAX) {
42*433d6423SLionel Sambuc 	errno = EINVAL;
43*433d6423SLionel Sambuc 	return -1;
44*433d6423SLionel Sambuc   }
45*433d6423SLionel Sambuc 
46*433d6423SLionel Sambuc   memset(&m, 0, sizeof(m));
47*433d6423SLionel Sambuc   m.m_vfs_utimens.len = strlen(name) + 1;
48*433d6423SLionel Sambuc   m.m_vfs_utimens.name = __UNCONST(name);
49*433d6423SLionel Sambuc   m.m_vfs_utimens.atime = tv[0].tv_sec;
50*433d6423SLionel Sambuc   m.m_vfs_utimens.mtime = tv[1].tv_sec;
51*433d6423SLionel Sambuc   m.m_vfs_utimens.ansec = tv[0].tv_nsec;
52*433d6423SLionel Sambuc   m.m_vfs_utimens.mnsec = tv[1].tv_nsec;
53*433d6423SLionel Sambuc   m.m_vfs_utimens.flags = flags;
54*433d6423SLionel Sambuc 
55*433d6423SLionel Sambuc   return(_syscall(VFS_PROC_NR, VFS_UTIMENS, &m));
56*433d6423SLionel Sambuc }
57