Merge pull request #5714 from alexischr/update_bockbuild
[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-2006 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 gint32
20 Mono_Posix_Syscall_gettimeofday (
21         struct Mono_Posix_Timeval *tv,
22         void *tz)
23 {
24         struct timeval _tv;
25         struct timezone _tz;
26         int r;
27
28         r = gettimeofday (&_tv, &_tz);
29
30         if (r == 0) {
31                 if (tv) {
32                         tv->tv_sec  = _tv.tv_sec;
33                         tv->tv_usec = _tv.tv_usec;
34                 }
35                 if (tz) {
36                         struct Mono_Posix_Timezone *tz_ = (struct Mono_Posix_Timezone *) tz;
37                         tz_->tz_minuteswest = _tz.tz_minuteswest;
38                         tz_->tz_dsttime     = 0;
39                 }
40         }
41
42         return r;
43 }
44
45 gint32
46 Mono_Posix_Syscall_settimeofday (
47         struct Mono_Posix_Timeval *tv,
48         struct Mono_Posix_Timezone *tz)
49 {
50 #if defined(__HAIKU__)
51         /* FIXME: Haiku doesn't support this either, consider
52            using set_real_time_clock instead? */
53         return -1;
54 #else
55         struct timeval _tv   = {0};
56         struct timeval *ptv  = NULL;
57         struct timezone _tz  = {0};
58         struct timezone *ptz = NULL;
59         int r;
60
61         if (tv) {
62                 _tv.tv_sec  = tv->tv_sec;
63                 _tv.tv_usec = tv->tv_usec;
64                 ptv = &_tv;
65         }
66         if (tz) {
67                 _tz.tz_minuteswest = tz->tz_minuteswest;
68                 _tz.tz_dsttime = 0;
69                 ptz = &_tz;
70         }
71
72         r = settimeofday (ptv, ptz);
73
74         return r;
75 #endif
76 }
77
78 static inline struct timeval*
79 copy_utimes (struct timeval* to, struct Mono_Posix_Timeval *from)
80 {
81         if (from) {
82                 to[0].tv_sec  = from[0].tv_sec;
83                 to[0].tv_usec = from[0].tv_usec;
84                 to[1].tv_sec  = from[1].tv_sec;
85                 to[1].tv_usec = from[1].tv_usec;
86                 return to;
87         }
88
89         return NULL;
90 }
91
92 gint32
93 Mono_Posix_Syscall_utimes(const char *filename, struct Mono_Posix_Timeval *tv)
94 {
95         struct timeval _tv[2];
96         struct timeval *ptv;
97
98         ptv = copy_utimes (_tv, tv);
99
100         return utimes (filename, ptv);
101 }
102
103 #ifdef HAVE_LUTIMES
104 gint32
105 Mono_Posix_Syscall_lutimes(const char *filename, struct Mono_Posix_Timeval *tv)
106 {
107         struct timeval _tv[2];
108         struct timeval *ptv;
109
110         ptv = copy_utimes (_tv, tv);
111
112         return lutimes (filename, ptv);
113 }
114 #endif /* def HAVE_LUTIMES */
115
116 #if HAVE_FUTIMES
117 gint32
118 Mono_Posix_Syscall_futimes(int fd, struct Mono_Posix_Timeval *tv)
119 {
120         struct timeval _tv[2];
121         struct timeval *ptv;
122
123         ptv = copy_utimes (_tv, tv);
124
125         return futimes (fd, ptv);
126 }
127 #endif  /* def HAVE_FUTIMES */
128
129 G_END_DECLS
130
131 /*
132  * vim: noexpandtab
133  */