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{
41return -1;
42}
4344
int poll(struct pollfd *ufds, unsigned int nfds, int timeout)
45{
46return -1;
47}
4849
struct tm *gmtime_r(const time_t *timep, struct tm *result)
50{
51/* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
52memcpy(result, gmtime(timep), sizeof(struct tm));
53return result;
54}
5556
struct tm *localtime_r(const time_t *timep, struct tm *result)
57{
58/* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
59memcpy(result, localtime(timep), sizeof(struct tm));
60return result;
61}
6263
#undef getcwd
64char *mingw_getcwd(char *pointer, int len)
65{
66int i;
67char *ret = getcwd(pointer, len);
68if (!ret)
69return ret;
70for (i = 0; pointer[i]; i++)
71if (pointer[i] == '\\')
72pointer[i] = '/';
73return ret;
74}
7576
#undef rename
77int mingw_rename(const char *pold, const char *pnew)
78{
79/*
80* Try native rename() first to get errno right.
81* It is based on MoveFile(), which cannot overwrite existing files.
82*/
83if (!rename(pold, pnew))
84return 0;
85if (errno != EEXIST)
86return -1;
87if (MoveFileEx(pold, pnew, MOVEFILE_REPLACE_EXISTING))
88return 0;
89/* TODO: translate more errors */
90if (GetLastError() == ERROR_ACCESS_DENIED) {
91DWORD attrs = GetFileAttributes(pnew);
92if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY)) {
93errno = EISDIR;
94return -1;
95}
96}
97errno = EACCES;
98return -1;
99}
100101
struct passwd *getpwuid(int uid)
102{
103static char user_name[100];
104static struct passwd p;
105106
DWORD len = sizeof(user_name);
107if (!GetUserName(user_name, &len))
108return NULL;
109p.pw_name = user_name;
110p.pw_gecos = "unknown";
111p.pw_dir = NULL;
112return &p;
113}
114115
int setitimer(int type, struct itimerval *in, struct itimerval *out)
116{
117return -1;
118}
119120
int sigaction(int sig, struct sigaction *in, struct sigaction *out)
121{
122return -1;
123}