1#include "../git-compat-util.h"
23
unsigned int _CRT_fmode = _O_BINARY;
45
#undef open
6int mingw_open (const char *filename, int oflags, ...)
7{
8va_list args;
9unsigned mode;
10va_start(args, oflags);
11mode = va_arg(args, int);
12va_end(args);
1314
if (!strcmp(filename, "/dev/null"))
15filename = "nul";
16int fd = open(filename, oflags, mode);
17if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) {
18DWORD attrs = GetFileAttributes(filename);
19if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
20errno = EISDIR;
21}
22return fd;
23}
2425
unsigned int sleep (unsigned int seconds)
26{
27Sleep(seconds*1000);
28return 0;
29}
3031
int mkstemp(char *template)
32{
33char *filename = mktemp(template);
34if (filename == NULL)
35return -1;
36return open(filename, O_RDWR | O_CREAT, 0600);
37}
3839
int gettimeofday(struct timeval *tv, void *tz)
40{
41SYSTEMTIME st;
42struct tm tm;
43GetSystemTime(&st);
44tm.tm_year = st.wYear-1900;
45tm.tm_mon = st.wMonth-1;
46tm.tm_mday = st.wDay;
47tm.tm_hour = st.wHour;
48tm.tm_min = st.wMinute;
49tm.tm_sec = st.wSecond;
50tv->tv_sec = tm_to_time_t(&tm);
51if (tv->tv_sec < 0)
52return -1;
53tv->tv_usec = st.wMilliseconds*1000;
54return 0;
55}
5657
int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
58{
59return -1;
60}
6162
struct tm *gmtime_r(const time_t *timep, struct tm *result)
63{
64/* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
65memcpy(result, gmtime(timep), sizeof(struct tm));
66return result;
67}
6869
struct tm *localtime_r(const time_t *timep, struct tm *result)
70{
71/* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
72memcpy(result, localtime(timep), sizeof(struct tm));
73return result;
74}
7576
#undef getcwd
77char *mingw_getcwd(char *pointer, int len)
78{
79int i;
80char *ret = getcwd(pointer, len);
81if (!ret)
82return ret;
83for (i = 0; pointer[i]; i++)
84if (pointer[i] == '\\')
85pointer[i] = '/';
86return ret;
87}
8889
#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*/
96if (!rename(pold, pnew))
97return 0;
98if (errno != EEXIST)
99return -1;
100if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
101return 0;
102/* TODO: translate more errors */
103if (GetLastError() == ERROR_ACCESS_DENIED) {
104DWORD attrs = GetFileAttributes(pnew);
105if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
106errno = EISDIR;
107return -1;
108}
109}
110errno = EACCES;
111return -1;
112}
113114
struct passwd *getpwuid(int uid)
115{
116static char user_name[100];
117static struct passwd p;
118119
DWORD len = sizeof(user_name);
120if (!GetUserName(user_name, &len))
121return NULL;
122p.pw_name = user_name;
123p.pw_gecos = "unknown";
124p.pw_dir = NULL;
125return &p;
126}
127128
int setitimer(int type, struct itimerval *in, struct itimerval *out)
129{
130return -1;
131}
132133
int sigaction(int sig, struct sigaction *in, struct sigaction *out)
134{
135return -1;
136}