summaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
Diffstat (limited to 'win32')
-rw-r--r--win32/win32.c48
-rw-r--r--win32/win32.h6
2 files changed, 54 insertions, 0 deletions
diff --git a/win32/win32.c b/win32/win32.c
index 7d589ec593..d7f12d3f0b 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -2432,3 +2432,51 @@ win32_getenv(const char *name)
return curitem;
}
+
+int
+myrename(const char *oldpath, const char *newpath)
+{
+ int res = 0;
+ int oldatts;
+ int newatts;
+
+ oldatts = GetFileAttributes(oldpath);
+ newatts = GetFileAttributes(newpath);
+
+ if (oldatts == -1) {
+ printf("file to move doesn't exist");
+ return -1;
+ }
+
+ if (newatts != -1 && newatts & FILE_ATTRIBUTE_READONLY)
+ SetFileAttributesA(newpath, newatts & ~ FILE_ATTRIBUTE_READONLY);
+
+ if (!MoveFile(oldpath, newpath))
+ res = -1;
+
+ if (res == 0 || (GetLastError() != ERROR_ALREADY_EXISTS
+ && GetLastError() != ERROR_FILE_EXISTS))
+ goto done;
+
+ if (IsWinNT()) {
+ if (MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING))
+ res = 0;
+ } else {
+ for (;;) {
+ if (!DeleteFile(newpath) && GetLastError() != ERROR_FILE_NOT_FOUND)
+ break;
+ else if (MoveFile(oldpath, newpath)) {
+ res = 0;
+ break;
+ }
+ }
+ }
+
+done:
+ if (res)
+ errno = GetLastError();
+ else
+ SetFileAttributes(newpath, oldatts);
+
+ return res;
+}
diff --git a/win32/win32.h b/win32/win32.h
index add6f14250..9e6e153489 100644
--- a/win32/win32.h
+++ b/win32/win32.h
@@ -207,6 +207,7 @@ extern struct protoent * mygetprotobynumber(int);
extern struct servent * mygetservbyname(char *, char *);
extern struct servent * mygetservbyport(int, char *);
extern char *win32_getenv(const char *);
+extern int myrename(const char *, const char *);
extern int chown(const char *, int, int);
extern int link(char *, char *);
@@ -408,4 +409,9 @@ extern char *mystrerror(int);
#endif
#define getenv win32_getenv
+#ifdef rename
+#undef rename
+#endif
+#define rename myrename
+
#endif