summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorUlf Hermann <[email protected]>2017-04-06 16:35:41 +0200
committerUlf Hermann <[email protected]>2017-05-08 12:43:02 +0000
commit5c20064a9aff24b5e1120349e718c48bb7d9a028 (patch)
treefe33451124c23b95be08070ec6d9ca747bc71b1d
parentbcc680f1dde4bb0a47deb1015617731595f9a15c (diff)
Make ranlib work on windows
Change-Id: I51b9bb88dc86c3378451e07ade19b21526b9eb0c Reviewed-by: Christian Kandeler <[email protected]>
-rw-r--r--src/ranlib.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/ranlib.c b/src/ranlib.c
index 22aac28f..cd52219d 100644
--- a/src/ranlib.c
+++ b/src/ranlib.c
@@ -207,6 +207,9 @@ handle_file (const char *fname)
/* If the file contains no symbols we need not do anything. */
int status = 0;
+ char tmpfname[strlen (fname) + 7];
+ tmpfname[0] = '\0';
+
if (symtab.symsnamelen != 0
/* We have to rewrite the file also if it initially had an index
but now does not need one anymore. */
@@ -214,7 +217,6 @@ handle_file (const char *fname)
{
/* Create a new, temporary file in the same directory as the
original file. */
- char tmpfname[strlen (fname) + 7];
strcpy (stpcpy (tmpfname, fname), "XXXXXX");
int newfd = mkstemp (tmpfname);
if (unlikely (newfd == -1))
@@ -268,8 +270,7 @@ handle_file (const char *fname)
/* Never complain about fchown failing. */
({asm ("" :: "r" (fchown (newfd, st.st_uid, st.st_gid))); }),
#endif
- close (newfd) != 0) ||
- (newfd = -1, rename (tmpfname, fname) != 0))
+ close (newfd) != 0) || (newfd = -1, false))
goto nonew_unlink;
}
}
@@ -280,6 +281,28 @@ handle_file (const char *fname)
close (fd);
+ if (status == 0 && tmpfname[0])
+ {
+ char swapfname[strlen (fname) + 7];
+ strcpy (stpcpy (swapfname, fname), "XXXXXX");
+ mktemp (swapfname);
+ if (rename (fname, swapfname) != 0)
+ {
+ error (0, errno, gettext ("cannot move original file out of the way"));
+ status = 1;
+ }
+ if (rename (tmpfname, fname) != 0)
+ {
+ error (0, errno, gettext ("cannot move new file into place"));
+ if (rename (swapfname, fname) != 0)
+ error (0, errno, gettext ("cannot rescue original file"));
+ status = 1;
+ }
+ else
+ unlink (swapfname);
+ }
+
+ unlink (tmpfname);
return status;
}