shithub: rgbds

Download patch

ref: db1eb8fbcbcd06bcbfa7f6244f06ae2807f1e94d
parent: 95f347dc6a646b82d9e988bf36a011c8f01f7082
author: ISSOtm <[email protected]>
date: Fri Mar 27 19:36:11 EDT 2020

Revert "Prevent RGBASM from outputting corrupted files"

This reverts commit 06fe27c51601e1822b9b3212f350c21609bdf9e9.

According to Microsoft's documentation
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/tmpfile?view=vs-2019
`tmpfile` attempts to create the temporary file at the **root** folder
This seems to assume that the user has admin rights; might be a compat
thing, idk, but it breaks on people's computers.
(CI didn't catch it, annoyingly.)

Reverting to make RGBASM usable on most Windows computers.
(Sanely-configured ones, at least.)
Another solution to #446 needs to be figured out, yay...

--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -381,12 +381,13 @@
  */
 void out_WriteObject(void)
 {
+	FILE *f;
 	struct Section *pSect;
 	struct Assertion *assert = assertions;
-	FILE *f = tmpfile(); /* Avoids producing a corrupted file on error */
 
+	f = fopen(tzObjectname, "wb");
 	if (!f)
-		err(1, "Couldn't create temporary file");
+		err(1, "Couldn't write file '%s'", tzObjectname);
 
 	/* Also write exported symbols that weren't written above */
 	sym_ForEach(registerExportedSymbol, NULL);
@@ -413,31 +414,6 @@
 		assert = assert->next;
 	}
 
-	/* We're finished writing the file; now, copy it to the final one */
-	FILE *objFile = fopen(tzObjectname, "wb");
-	long size = ftell(f);
-	char buf[1024];
-
-	rewind(f);
-	while (size) {
-		long blockSize = size < sizeof(buf) ? size : sizeof(buf);
-
-		if (fread(buf, blockSize, 1, f) < 1
-		 || fwrite(buf, blockSize, 1, objFile) < 1) {
-			char const *errmsg =
-				ferror(f) || ferror(objFile) ? strerror(errno)
-							     : "end of file";
-
-			fclose(objFile);
-			fclose(f);
-			remove(tzObjectname);
-			errx(1, "Failed to write file \"%s\": %s", tzObjectname,
-			     errmsg);
-		}
-		size -= blockSize;
-	}
-
-	fclose(objFile);
 	fclose(f);
 }