Skip to content

Commit

Permalink
Windows: Implement a wrapper of the open() function.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Johannes Sixt committed Jun 23, 2008
1 parent 23326d1 commit 3e4a1ba
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
20 changes: 20 additions & 0 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

unsigned int _CRT_fmode = _O_BINARY;

#undef open
int mingw_open (const char *filename, int oflags, ...)
{
va_list args;
unsigned mode;
va_start(args, oflags);
mode = va_arg(args, int);
va_end(args);

if (!strcmp(filename, "/dev/null"))
filename = "nul";
int fd = open(filename, oflags, mode);
if (fd < 0 && (oflags & O_CREAT) && errno == EACCES) {
DWORD attrs = GetFileAttributes(filename);
if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
errno = EISDIR;
}
return fd;
}

unsigned int sleep (unsigned int seconds)
{
Sleep(seconds*1000);
Expand Down
3 changes: 3 additions & 0 deletions compat/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ int sigaction(int sig, struct sigaction *in, struct sigaction *out);
* replacements of existing functions
*/

int mingw_open (const char *filename, int oflags, ...);
#define open mingw_open

char *mingw_getcwd(char *pointer, int len);
#define getcwd mingw_getcwd

Expand Down

0 comments on commit 3e4a1ba

Please sign in to comment.