* map-icalls.h: Flush (add utimes_bad, lutimes, futimes).
[mono.git] / support / sys-time.c
1 /*
2  * <sys/stat.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004 Jonathan Pryor
8  */
9
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <string.h>
13
14 #include "map.h"
15 #include "mph.h"
16
17 G_BEGIN_DECLS
18
19 struct Mono_Posix_Timeval {
20         /* time_t */      mph_time_t  tv_sec;   /* seconds */
21         /* suseconds_t */ gint64      tv_usec;  /* microseconds */
22 };
23
24 struct Mono_Posix_Timezone {
25         int tz_minuteswest;  /* minutes W of Greenwich */
26         int tz_dsttime;      /* ignored */
27 };
28
29 gint32
30 Mono_Posix_Syscall_gettimeofday (
31         struct Mono_Posix_Timeval *tv,
32         void *tz)
33 {
34         struct timeval _tv;
35         struct timezone _tz;
36         int r;
37
38         r = gettimeofday (&_tv, &_tz);
39
40         if (r == 0) {
41                 if (tv) {
42                         tv->tv_sec  = _tv.tv_sec;
43                         tv->tv_usec = _tv.tv_usec;
44                 }
45                 if (tz) {
46                         struct Mono_Posix_Timezone *tz_ = (struct Mono_Posix_Timezone *) tz;
47                         tz_->tz_minuteswest = _tz.tz_minuteswest;
48                         tz_->tz_dsttime     = 0;
49                 }
50         }
51
52         return r;
53 }
54
55 gint32
56 Mono_Posix_Syscall_settimeofday (
57         struct Mono_Posix_Timeval *tv,
58         struct Mono_Posix_Timezone *tz)
59 {
60         struct timeval _tv   = {0};
61         struct timeval *ptv  = NULL;
62         struct timezone _tz  = {0};
63         struct timezone *ptz = NULL;
64         int r;
65
66         if (tv) {
67                 _tv.tv_sec  = tv->tv_sec;
68                 _tv.tv_usec = tv->tv_usec;
69                 ptv = &_tv;
70         }
71         if (tz) {
72                 _tz.tz_minuteswest = tz->tz_minuteswest;
73                 _tz.tz_dsttime = 0;
74                 ptz = &_tz;
75         }
76
77         r = settimeofday (ptv, ptz);
78
79         return r;
80 }
81
82 /* Remove this at some point in the future */
83 gint32
84 Mono_Posix_Syscall_utimes_bad (const char *filename,
85         struct Mono_Posix_Timeval *tv)
86 {
87         struct timeval _tv;
88         struct timeval *ptv = NULL;
89
90         if (tv) {
91                 _tv.tv_sec  = tv->tv_sec;
92                 _tv.tv_usec = tv->tv_usec;
93                 ptv = &_tv;
94         }
95
96         return utimes (filename, ptv);
97 }
98
99 static inline struct timeval*
100 copy_utimes (struct timeval* to, struct Mono_Posix_Timeval *from)
101 {
102         if (from) {
103                 to[0].tv_sec  = from->tv_sec;
104                 to[0].tv_usec = from->tv_usec;
105                 to[1].tv_sec  = from->tv_sec;
106                 to[1].tv_usec = from->tv_usec;
107                 return to;
108         }
109
110         return NULL;
111 }
112
113 gint32
114 Mono_Posix_Syscall_utimes(const char *filename, struct Mono_Posix_Timeval *tv)
115 {
116         struct timeval _tv[2];
117         struct timeval *ptv;
118
119         ptv = copy_utimes (_tv, tv);
120
121         return utimes (filename, ptv);
122 }
123
124 gint32
125 Mono_Posix_Syscall_lutimes(const char *filename, struct Mono_Posix_Timeval *tv)
126 {
127         struct timeval _tv[2];
128         struct timeval *ptv;
129
130         ptv = copy_utimes (_tv, tv);
131
132         return lutimes (filename, ptv);
133 }
134
135 gint32
136 Mono_Posix_Syscall_futimes(int fd, struct Mono_Posix_Timeval *tv)
137 {
138         struct timeval _tv[2];
139         struct timeval *ptv;
140
141         ptv = copy_utimes (_tv, tv);
142
143         return futimes (fd, ptv);
144 }
145
146 G_END_DECLS
147
148 /*
149  * vim: noexpandtab
150  */