xref: /titanic_51/usr/src/lib/libc/port/sys/utimesys.c (revision 3b862e9a9ce59d5dbf0177b9eb293109fde6bf36)
1*3b862e9aSRoger A. Faulkner /*
2*3b862e9aSRoger A. Faulkner  * CDDL HEADER START
3*3b862e9aSRoger A. Faulkner  *
4*3b862e9aSRoger A. Faulkner  * The contents of this file are subject to the terms of the
5*3b862e9aSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*3b862e9aSRoger A. Faulkner  * You may not use this file except in compliance with the License.
7*3b862e9aSRoger A. Faulkner  *
8*3b862e9aSRoger A. Faulkner  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*3b862e9aSRoger A. Faulkner  * or http://www.opensolaris.org/os/licensing.
10*3b862e9aSRoger A. Faulkner  * See the License for the specific language governing permissions
11*3b862e9aSRoger A. Faulkner  * and limitations under the License.
12*3b862e9aSRoger A. Faulkner  *
13*3b862e9aSRoger A. Faulkner  * When distributing Covered Code, include this CDDL HEADER in each
14*3b862e9aSRoger A. Faulkner  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*3b862e9aSRoger A. Faulkner  * If applicable, add the following below this CDDL HEADER, with the
16*3b862e9aSRoger A. Faulkner  * fields enclosed by brackets "[]" replaced with your own identifying
17*3b862e9aSRoger A. Faulkner  * information: Portions Copyright [yyyy] [name of copyright owner]
18*3b862e9aSRoger A. Faulkner  *
19*3b862e9aSRoger A. Faulkner  * CDDL HEADER END
20*3b862e9aSRoger A. Faulkner  */
21*3b862e9aSRoger A. Faulkner 
22*3b862e9aSRoger A. Faulkner /*
23*3b862e9aSRoger A. Faulkner  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24*3b862e9aSRoger A. Faulkner  * Use is subject to license terms.
25*3b862e9aSRoger A. Faulkner  */
26*3b862e9aSRoger A. Faulkner 
27*3b862e9aSRoger A. Faulkner #include "lint.h"
28*3b862e9aSRoger A. Faulkner #include <string.h>
29*3b862e9aSRoger A. Faulkner #include <utime.h>
30*3b862e9aSRoger A. Faulkner #include <fcntl.h>
31*3b862e9aSRoger A. Faulkner #include <sys/stat.h>
32*3b862e9aSRoger A. Faulkner #include <sys/time.h>
33*3b862e9aSRoger A. Faulkner #include <sys/syscall.h>
34*3b862e9aSRoger A. Faulkner 
35*3b862e9aSRoger A. Faulkner int
36*3b862e9aSRoger A. Faulkner futimens(int fd, const timespec_t times[2])
37*3b862e9aSRoger A. Faulkner {
38*3b862e9aSRoger A. Faulkner 	return (syscall(SYS_utimesys, 0, fd, times));
39*3b862e9aSRoger A. Faulkner }
40*3b862e9aSRoger A. Faulkner 
41*3b862e9aSRoger A. Faulkner int
42*3b862e9aSRoger A. Faulkner utimensat(int fd, const char *path, const timespec_t times[2], int flag)
43*3b862e9aSRoger A. Faulkner {
44*3b862e9aSRoger A. Faulkner 	return (syscall(SYS_utimesys, 1, fd, path, times, flag));
45*3b862e9aSRoger A. Faulkner }
46*3b862e9aSRoger A. Faulkner 
47*3b862e9aSRoger A. Faulkner #pragma weak _utime = utime
48*3b862e9aSRoger A. Faulkner int
49*3b862e9aSRoger A. Faulkner utime(const char *path, const struct utimbuf *times)
50*3b862e9aSRoger A. Faulkner {
51*3b862e9aSRoger A. Faulkner 	struct utimbuf ltimes;
52*3b862e9aSRoger A. Faulkner 	timespec_t ts[2];
53*3b862e9aSRoger A. Faulkner 	timespec_t *tsp;
54*3b862e9aSRoger A. Faulkner 
55*3b862e9aSRoger A. Faulkner 	if (times == NULL) {
56*3b862e9aSRoger A. Faulkner 		tsp = NULL;
57*3b862e9aSRoger A. Faulkner 	} else {
58*3b862e9aSRoger A. Faulkner 		/* use uucopy() to behave like a system call */
59*3b862e9aSRoger A. Faulkner 		if (uucopy(times, &ltimes, sizeof (ltimes)) != 0)
60*3b862e9aSRoger A. Faulkner 			return (-1);	/* uucopy() set errno to EFAULT */
61*3b862e9aSRoger A. Faulkner 		ts[0].tv_sec = ltimes.actime;
62*3b862e9aSRoger A. Faulkner 		ts[0].tv_nsec = 0;
63*3b862e9aSRoger A. Faulkner 		ts[1].tv_sec = ltimes.modtime;
64*3b862e9aSRoger A. Faulkner 		ts[1].tv_nsec = 0;
65*3b862e9aSRoger A. Faulkner 		tsp = ts;
66*3b862e9aSRoger A. Faulkner 	}
67*3b862e9aSRoger A. Faulkner 	return (utimensat(AT_FDCWD, path, tsp, 0));
68*3b862e9aSRoger A. Faulkner }
69*3b862e9aSRoger A. Faulkner 
70*3b862e9aSRoger A. Faulkner int
71*3b862e9aSRoger A. Faulkner utimes(const char *path, const struct timeval times[2])
72*3b862e9aSRoger A. Faulkner {
73*3b862e9aSRoger A. Faulkner 	struct timeval ltimes[2];
74*3b862e9aSRoger A. Faulkner 	timespec_t ts[2];
75*3b862e9aSRoger A. Faulkner 	timespec_t *tsp;
76*3b862e9aSRoger A. Faulkner 
77*3b862e9aSRoger A. Faulkner 	if (times == NULL) {
78*3b862e9aSRoger A. Faulkner 		tsp = NULL;
79*3b862e9aSRoger A. Faulkner 	} else {
80*3b862e9aSRoger A. Faulkner 		/* use uucopy() to behave like a system call */
81*3b862e9aSRoger A. Faulkner 		if (uucopy(times, ltimes, sizeof (ltimes)) != 0)
82*3b862e9aSRoger A. Faulkner 			return (-1);	/* uucopy() set errno to EFAULT */
83*3b862e9aSRoger A. Faulkner 		ts[0].tv_sec = ltimes[0].tv_sec;
84*3b862e9aSRoger A. Faulkner 		ts[0].tv_nsec = ltimes[0].tv_usec * 1000;
85*3b862e9aSRoger A. Faulkner 		ts[1].tv_sec = ltimes[1].tv_sec;
86*3b862e9aSRoger A. Faulkner 		ts[1].tv_nsec = ltimes[1].tv_usec * 1000;
87*3b862e9aSRoger A. Faulkner 		tsp = ts;
88*3b862e9aSRoger A. Faulkner 	}
89*3b862e9aSRoger A. Faulkner 	return (utimensat(AT_FDCWD, path, tsp, 0));
90*3b862e9aSRoger A. Faulkner }
91*3b862e9aSRoger A. Faulkner 
92*3b862e9aSRoger A. Faulkner #pragma weak _futimesat = futimesat
93*3b862e9aSRoger A. Faulkner int
94*3b862e9aSRoger A. Faulkner futimesat(int fd, const char *path, const struct timeval times[2])
95*3b862e9aSRoger A. Faulkner {
96*3b862e9aSRoger A. Faulkner 	struct timeval ltimes[2];
97*3b862e9aSRoger A. Faulkner 	timespec_t ts[2];
98*3b862e9aSRoger A. Faulkner 	timespec_t *tsp;
99*3b862e9aSRoger A. Faulkner 
100*3b862e9aSRoger A. Faulkner 	if (times == NULL) {
101*3b862e9aSRoger A. Faulkner 		tsp = NULL;
102*3b862e9aSRoger A. Faulkner 	} else {
103*3b862e9aSRoger A. Faulkner 		/* use uucopy() to behave like a system call */
104*3b862e9aSRoger A. Faulkner 		if (uucopy(times, ltimes, sizeof (ltimes)) != 0)
105*3b862e9aSRoger A. Faulkner 			return (-1);	/* uucopy() set errno to EFAULT */
106*3b862e9aSRoger A. Faulkner 		ts[0].tv_sec = ltimes[0].tv_sec;
107*3b862e9aSRoger A. Faulkner 		ts[0].tv_nsec = ltimes[0].tv_usec * 1000;
108*3b862e9aSRoger A. Faulkner 		ts[1].tv_sec = ltimes[1].tv_sec;
109*3b862e9aSRoger A. Faulkner 		ts[1].tv_nsec = ltimes[1].tv_usec * 1000;
110*3b862e9aSRoger A. Faulkner 		tsp = ts;
111*3b862e9aSRoger A. Faulkner 	}
112*3b862e9aSRoger A. Faulkner 
113*3b862e9aSRoger A. Faulkner 	if (path == NULL)
114*3b862e9aSRoger A. Faulkner 		return (futimens(fd, tsp));
115*3b862e9aSRoger A. Faulkner 
116*3b862e9aSRoger A. Faulkner 	return (utimensat(fd, path, tsp, 0));
117*3b862e9aSRoger A. Faulkner }
118