fxgxa: use standard fopen() for reading files [Windows]

This commit is contained in:
Lephenixnoir 2024-08-26 11:04:04 +02:00
parent 50997a8b75
commit b83796a382
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495

View file

@ -19,7 +19,7 @@ static void invert_header(void *gxa)
#define fail(msg, ...) { \ #define fail(msg, ...) { \
fprintf(stderr, "error: " msg ": %m\n", ##__VA_ARGS__); \ fprintf(stderr, "error: " msg ": %m\n", ##__VA_ARGS__); \
close(fd); \ fclose(fp); \
free(data); \ free(data); \
return NULL; \ return NULL; \
} }
@ -28,31 +28,31 @@ static void invert_header(void *gxa)
Allocates a buffer with @prepend leading bytes initialized to zero. */ Allocates a buffer with @prepend leading bytes initialized to zero. */
static void *load(const char *filename, size_t *size, int header, int footer) static void *load(const char *filename, size_t *size, int header, int footer)
{ {
int fd; FILE *fp;
struct stat statbuf;
void *data = NULL; void *data = NULL;
size_t filesize; long filesize;
fd = open(filename, O_RDONLY); fp = fopen(filename, "rb");
if(fd < 0) fail("cannot open %s", filename); if(!fp) fail("cannot open %s", filename);
int x = fstat(fd, &statbuf); fseek(fp, 0, SEEK_END);
if(x > 0) fail("cannot stat %s", filename); filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
filesize = statbuf.st_size; if(ftell(fp) != 0) fail("cannod rewind %s", filename);
data = malloc(header + filesize + footer); data = malloc(header + filesize + footer);
if(!data) fail("cannot load %s", filename); if(!data) fail("cannot load %s", filename);
size_t remaining = filesize; long remaining = filesize;
while(remaining > 0) while(remaining > 0)
{ {
size_t offset = header + filesize - remaining; long offset = header + filesize - remaining;
ssize_t y = read(fd, data + offset, remaining); long y = fread(data + offset, 1, remaining, fp);
if(y < 0) fail("cannot read from %s", filename); if(y <= 0) fail("cannot read from %s at offset %ld",
filename, offset);
remaining -= y; remaining -= y;
} }
close(fd); fclose(fp);
memset(data, 0, header); memset(data, 0, header);
memset(data + header + filesize, 0, footer); memset(data + header + filesize, 0, footer);