replace unistd file access with stdio to fix windows port

it seems read() unistd call does not return complete file
when running on windows built via mingw64 :-(
This commit is contained in:
j4nn 2025-12-21 02:25:59 +01:00
commit 914a83b781
2 changed files with 15 additions and 24 deletions

View file

@ -118,18 +118,13 @@ int load_pe(const char *fname, void *buff, int maxsize, uint64_t base_va,
int fd;
int res;
fd = open(fname, O_RDONLY);
if (fd < 0) {
PRNO("open pe %s file", fname);
FILE *f = fopen(fname, "rb");
if (f == NULL) {
PRNO("fopen pe %s", fname);
return -1;
}
res = read(fd, buff, maxsize);
if (res < 0) {
PRNO("read pe %s file", fname);
return -1;
}
close(fd);
res = fread(buff, 1, maxsize, f);
fclose(f);
parsed_pe *p = ParsePEFromFile(fname);

View file

@ -684,30 +684,26 @@ static int abl_mem_read(struct fbusb *dev, uint64_t addr, int size,
static int file_rd_buff(const char *fname, void *buffer, int size)
{
int res;
int fd = open(fname, O_RDONLY);
if (fd < 0) {
PRNO("file_rd_buff open %s", fname);
FILE *f = fopen(fname, "rb");
if (f == NULL) {
PRNO("file_rd_buff fopen %s", fname);
return -1;
}
res = read(fd, buffer, size);
if (res < 0)
PRNO("file_rd_buff read %s", fname);
close(fd);
res = fread(buffer, 1, size, f);
fclose(f);
return res;
}
static int file_wr_buff(const char *fname, const void *buffer, int size)
{
int res;
int fd = creat(fname, 0644);
if (fd < 0) {
PRNO("file_wr_buff creat %s", fname);
FILE *f = fopen(fname, "wb");
if (f == NULL) {
PRNO("file_wr_buff fopen %s", fname);
return -1;
}
res = write(fd, buffer, size);
if (res < 0)
PRNO("file_wr_buff write %s", fname);
close(fd);
res = fwrite(buffer, 1, size, f);
fclose(f);
return res == size ? 0 : -1;
}