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, then
  Windows's open fails with EACCES; this is changed to EISDIR.

Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
This commit is contained in:
Johannes Sixt
2007-11-15 22:22:47 +01:00
parent a5ac001acc
commit fab21181f4
2 changed files with 22 additions and 3 deletions

View File

@@ -3,6 +3,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;
}
static inline time_t filetime_to_time_t(const FILETIME *ft)
{
long long winTime = ((long long)ft->dwHighDateTime << 32) + ft->dwLowDateTime;

View File

@@ -500,9 +500,8 @@ static inline int git_unlink(const char *pathname) {
}
#define unlink git_unlink
#define open(P, F, M...) \
(__builtin_constant_p(*(P)) && !strcmp(P, "/dev/null") ? \
open("nul", F, ## M) : open(P, F, ## M))
int mingw_open (const char *filename, int oflags, ...);
#define open mingw_open
#include <time.h>
struct tm *gmtime_r(const time_t *timep, struct tm *result);