mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 18:52:07 +00:00
@@ -408,13 +408,12 @@ static void registerExportedSymbol(struct sSymbol *symbol, void *arg)
|
|||||||
*/
|
*/
|
||||||
void out_WriteObject(void)
|
void out_WriteObject(void)
|
||||||
{
|
{
|
||||||
FILE *f;
|
|
||||||
struct Section *pSect;
|
struct Section *pSect;
|
||||||
struct Assertion *assert = assertions;
|
struct Assertion *assert = assertions;
|
||||||
|
FILE *f = tmpfile(); /* Avoids producing a corrupted file on error */
|
||||||
|
|
||||||
f = fopen(tzObjectname, "wb");
|
|
||||||
if (!f)
|
if (!f)
|
||||||
err(1, "Couldn't write file '%s'", tzObjectname);
|
err(1, "Couldn't create temporary file");
|
||||||
|
|
||||||
/* Also write exported symbols that weren't written above */
|
/* Also write exported symbols that weren't written above */
|
||||||
sym_ForEach(registerExportedSymbol, NULL);
|
sym_ForEach(registerExportedSymbol, NULL);
|
||||||
@@ -441,6 +440,31 @@ void out_WriteObject(void)
|
|||||||
assert = assert->next;
|
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);
|
fclose(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user