summaryrefslogtreecommitdiff
path: root/compat/mingw.c
diff options
context:
space:
mode:
Diffstat (limited to 'compat/mingw.c')
-rw-r--r--compat/mingw.c36
1 files changed, 34 insertions, 2 deletions
diff --git a/compat/mingw.c b/compat/mingw.c
index ab65f77..3071690 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -140,6 +140,22 @@ int mingw_open (const char *filename, int oflags, ...)
return fd;
}
+#undef fopen
+FILE *mingw_fopen (const char *filename, const char *otype)
+{
+ if (!strcmp(filename, "/dev/null"))
+ filename = "nul";
+ return fopen(filename, otype);
+}
+
+#undef freopen
+FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
+{
+ if (filename && !strcmp(filename, "/dev/null"))
+ filename = "nul";
+ return freopen(filename, otype, stream);
+}
+
/*
* The unit of FILETIME is 100-nanoseconds since January 1, 1601, UTC.
* Returns the 100-nanoseconds ("hekto nanoseconds") since the epoch.
@@ -259,8 +275,17 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
int fh, rc;
/* must have write permission */
- if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0)
- return -1;
+ DWORD attrs = GetFileAttributes(file_name);
+ if (attrs != INVALID_FILE_ATTRIBUTES &&
+ (attrs & FILE_ATTRIBUTE_READONLY)) {
+ /* ignore errors here; open() will report them */
+ SetFileAttributes(file_name, attrs & ~FILE_ATTRIBUTE_READONLY);
+ }
+
+ if ((fh = open(file_name, O_RDWR | O_BINARY)) < 0) {
+ rc = -1;
+ goto revert_attrs;
+ }
time_t_to_filetime(times->modtime, &mft);
time_t_to_filetime(times->actime, &aft);
@@ -270,6 +295,13 @@ int mingw_utime (const char *file_name, const struct utimbuf *times)
} else
rc = 0;
close(fh);
+
+revert_attrs:
+ if (attrs != INVALID_FILE_ATTRIBUTES &&
+ (attrs & FILE_ATTRIBUTE_READONLY)) {
+ /* ignore errors again */
+ SetFileAttributes(file_name, attrs);
+ }
return rc;
}