3ffff75ba657f6989c8edde0e948569e9d60636d
1#include "../git-compat-util.h"
2
3unsigned int _CRT_fmode = _O_BINARY;
4
5#undef open
6int mingw_open (const char *filename, int oflags, ...)
7{
8 va_list args;
9 unsigned mode;
10 va_start(args, oflags);
11 mode = va_arg(args, int);
12 va_end(args);
13
14 if (!strcmp(filename, "/dev/null"))
15 filename = "nul";
16 int fd = open(filename, oflags, mode);
17 if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) {
18 DWORD attrs = GetFileAttributes(filename);
19 if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
20 errno = EISDIR;
21 }
22 return fd;
23}
24
25unsigned int sleep (unsigned int seconds)
26{
27 Sleep(seconds*1000);
28 return 0;
29}
30
31int mkstemp(char *template)
32{
33 char *filename = mktemp(template);
34 if (filename == NULL)
35 return -1;
36 return open(filename, O_RDWR | O_CREAT, 0600);
37}
38
39int gettimeofday(struct timeval *tv, void *tz)
40{
41 SYSTEMTIME st;
42 struct tm tm;
43 GetSystemTime(&st);
44 tm.tm_year = st.wYear-1900;
45 tm.tm_mon = st.wMonth-1;
46 tm.tm_mday = st.wDay;
47 tm.tm_hour = st.wHour;
48 tm.tm_min = st.wMinute;
49 tm.tm_sec = st.wSecond;
50 tv->tv_sec = tm_to_time_t(&tm);
51 if (tv->tv_sec < 0)
52 return -1;
53 tv->tv_usec = st.wMilliseconds*1000;
54 return 0;
55}
56
57int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
58{
59 return -1;
60}
61
62struct tm *gmtime_r(const time_t *timep, struct tm *result)
63{
64 /* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
65 memcpy(result, gmtime(timep), sizeof(struct tm));
66 return result;
67}
68
69struct tm *localtime_r(const time_t *timep, struct tm *result)
70{
71 /* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
72 memcpy(result, localtime(timep), sizeof(struct tm));
73 return result;
74}
75
76#undef getcwd
77char *mingw_getcwd(char *pointer, int len)
78{
79 int i;
80 char *ret = getcwd(pointer, len);
81 if (!ret)
82 return ret;
83 for (i = 0; pointer[i]; i++)
84 if (pointer[i] == '\\')
85 pointer[i] = '/';
86 return ret;
87}
88
89#undef rename
90int mingw_rename(const char *pold, const char *pnew)
91{
92 /*
93 * Try native rename() first to get errno right.
94 * It is based on MoveFile(), which cannot overwrite existing files.
95 */
96 if (!rename(pold, pnew))
97 return 0;
98 if (errno != EEXIST)
99 return -1;
100 if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
101 return 0;
102 /* TODO: translate more errors */
103 if (GetLastError() == ERROR_ACCESS_DENIED) {
104 DWORD attrs = GetFileAttributes(pnew);
105 if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
106 errno = EISDIR;
107 return -1;
108 }
109 }
110 errno = EACCES;
111 return -1;
112}
113
114struct passwd *getpwuid(int uid)
115{
116 static char user_name[100];
117 static struct passwd p;
118
119 DWORD len = sizeof(user_name);
120 if (!GetUserName(user_name, &len))
121 return NULL;
122 p.pw_name = user_name;
123 p.pw_gecos = "unknown";
124 p.pw_dir = NULL;
125 return &p;
126}
127
128static HANDLE timer_event;
129static HANDLE timer_thread;
130static int timer_interval;
131static int one_shot;
132static sig_handler_t timer_fn = SIG_DFL;
133
134/* The timer works like this:
135 * The thread, ticktack(), is a trivial routine that most of the time
136 * only waits to receive the signal to terminate. The main thread tells
137 * the thread to terminate by setting the timer_event to the signalled
138 * state.
139 * But ticktack() interrupts the wait state after the timer's interval
140 * length to call the signal handler.
141 */
142
143static __stdcall unsigned ticktack(void *dummy)
144{
145 while (WaitForSingleObject(timer_event, timer_interval) == WAIT_TIMEOUT) {
146 if (timer_fn == SIG_DFL)
147 die("Alarm");
148 if (timer_fn != SIG_IGN)
149 timer_fn(SIGALRM);
150 if (one_shot)
151 break;
152 }
153 return 0;
154}
155
156static int start_timer_thread(void)
157{
158 timer_event = CreateEvent(NULL, FALSE, FALSE, NULL);
159 if (timer_event) {
160 timer_thread = (HANDLE) _beginthreadex(NULL, 0, ticktack, NULL, 0, NULL);
161 if (!timer_thread )
162 return errno = ENOMEM,
163 error("cannot start timer thread");
164 } else
165 return errno = ENOMEM,
166 error("cannot allocate resources for timer");
167 return 0;
168}
169
170static void stop_timer_thread(void)
171{
172 if (timer_event)
173 SetEvent(timer_event); /* tell thread to terminate */
174 if (timer_thread) {
175 int rc = WaitForSingleObject(timer_thread, 1000);
176 if (rc == WAIT_TIMEOUT)
177 error("timer thread did not terminate timely");
178 else if (rc != WAIT_OBJECT_0)
179 error("waiting for timer thread failed: %lu",
180 GetLastError());
181 CloseHandle(timer_thread);
182 }
183 if (timer_event)
184 CloseHandle(timer_event);
185 timer_event = NULL;
186 timer_thread = NULL;
187}
188
189static inline int is_timeval_eq(const struct timeval *i1, const struct timeval *i2)
190{
191 return i1->tv_sec == i2->tv_sec && i1->tv_usec == i2->tv_usec;
192}
193
194int setitimer(int type, struct itimerval *in, struct itimerval *out)
195{
196 static const struct timeval zero;
197 static int atexit_done;
198
199 if (out != NULL)
200 return errno = EINVAL,
201 error("setitimer param 3 != NULL not implemented");
202 if (!is_timeval_eq(&in->it_interval, &zero) &&
203 !is_timeval_eq(&in->it_interval, &in->it_value))
204 return errno = EINVAL,
205 error("setitimer: it_interval must be zero or eq it_value");
206
207 if (timer_thread)
208 stop_timer_thread();
209
210 if (is_timeval_eq(&in->it_value, &zero) &&
211 is_timeval_eq(&in->it_interval, &zero))
212 return 0;
213
214 timer_interval = in->it_value.tv_sec * 1000 + in->it_value.tv_usec / 1000;
215 one_shot = is_timeval_eq(&in->it_interval, &zero);
216 if (!atexit_done) {
217 atexit(stop_timer_thread);
218 atexit_done = 1;
219 }
220 return start_timer_thread();
221}
222
223int sigaction(int sig, struct sigaction *in, struct sigaction *out)
224{
225 if (sig != SIGALRM)
226 return errno = EINVAL,
227 error("sigaction only implemented for SIGALRM");
228 if (out != NULL)
229 return errno = EINVAL,
230 error("sigaction: param 3 != NULL not implemented");
231
232 timer_fn = in->sa_handler;
233 return 0;
234}
235
236#undef signal
237sig_handler_t mingw_signal(int sig, sig_handler_t handler)
238{
239 if (sig != SIGALRM)
240 return signal(sig, handler);
241 sig_handler_t old = timer_fn;
242 timer_fn = handler;
243 return old;
244}