@matheusbsbs / matheus-bagliano / commits / 3e4a1ba07b
Fork 0

Windows: Implement a wrapper of the open() function.

The wrapper does two things: - Requests to open /dev/null are redirected to open the nul pseudo file. - A request to open a file that currently exists as a directory on Windows fails with EACCES; this is changed to EISDIR. Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>

Johannes Sixt committed Nov 15, 2007 at 22:22 UTC 3e4a1ba07b65506c36f7f40fa76fcee26c400a5c
2 files changed +23
compat/mingw.c
+20
@@ -2,6 +2,26 @@
2
3 unsigned int _CRT_fmode = _O_BINARY;
4
5 +#undef open
6 +int 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 +
25 unsigned int sleep (unsigned int seconds)
26 {
27 Sleep(seconds*1000);
compat/mingw.h
+3
@@ -137,6 +137,9 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out);
137 * replacements of existing functions
138 */
139
140 +int mingw_open (const char *filename, int oflags, ...);
141 +#define open mingw_open
142 +
143 char *mingw_getcwd(char *pointer, int len);
144 #define getcwd mingw_getcwd
145