From 3d88e4b6c9d3bb7b5eb75575049ed6d06f47bcd9 Mon Sep 17 00:00:00 2001 From: Nguyễn Thái Ngọc Duy Date: Wed, 14 Apr 2010 06:51:11 +0200 Subject: win32: Replace open/fopen to support /dev/null --- include/mingw.h | 4 ++++ win32/mingw.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/include/mingw.h b/include/mingw.h index 57fa6177d..ba73c362e 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -120,6 +120,8 @@ NOIMPL(sigfillset,int *mask UNUSED_PARAM); #define fseeko(f,o,w) fseek(f,o,w) int fdprintf(int fd, const char *format, ...); +FILE* mingw_fopen(const char *filename, const char *mode); +#define fopen mingw_fopen /* * ANSI emulation wrappers @@ -269,6 +271,7 @@ IMPL(fsync,int,0,int fd UNUSED_PARAM); NOIMPL(kill,pid_t pid UNUSED_PARAM, int sig UNUSED_PARAM); int link(const char *oldpath, const char *newpath); NOIMPL(mknod,const char *name UNUSED_PARAM, mode_t mode UNUSED_PARAM, dev_t device UNUSED_PARAM); +int mingw_open (const char *filename, int oflags, ...); int pipe(int filedes[2]); NOIMPL(readlink,const char *path UNUSED_PARAM, char *buf UNUSED_PARAM, size_t bufsiz UNUSED_PARAM); NOIMPL(setgid,gid_t gid UNUSED_PARAM); @@ -282,6 +285,7 @@ NOIMPL(vfork,void); #define getcwd mingw_getcwd #define lchown(a,b,c) chown(a,b,c) +#define open mingw_open #define unlink mingw_unlink /* diff --git a/win32/mingw.c b/win32/mingw.c index 5604fdda5..cc266462f 100644 --- a/win32/mingw.c +++ b/win32/mingw.c @@ -115,6 +115,40 @@ static int err_win_to_posix(DWORD winerr) return error; } +#undef open +int mingw_open (const char *filename, int oflags, ...) +{ + va_list args; + unsigned mode; + int fd; + + va_start(args, oflags); + mode = va_arg(args, int); + va_end(args); + + if (oflags & O_NONBLOCK) { + errno = ENOSYS; + return -1; + } + if (!strcmp(filename, "/dev/null")) + filename = "nul"; + 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; +} + +#undef fopen +FILE *mingw_fopen (const char *filename, const char *mode) +{ + if (!strcmp(filename, "/dev/null")) + filename = "nul"; + return fopen(filename, mode); +} + unsigned int sleep (unsigned int seconds) { Sleep(seconds*1000); -- cgit v1.2.3-55-g6feb