diff --git a/DiskImage.cpp b/DiskImage.cpp index 4748719..08470b2 100644 --- a/DiskImage.cpp +++ b/DiskImage.cpp @@ -1280,7 +1280,7 @@ void TDiskImage::readUDI(int hfile, bool ronly) } //----------------------------------------------------------------------------- -void TDiskImage::writeTRD(int hfile) +void TDiskImage::writeTRD(fileTYPE *hfile) { VGFIND_SECTOR vgfs; @@ -1295,13 +1295,13 @@ void TDiskImage::writeTRD(int hfile) { if (FindSector(trk, side, sec + 1, &vgfs)) { - write(hfile, vgfs.SectorPointer, 256); + FileWriteAdv(hfile, vgfs.SectorPointer, 256); if ((!vgfs.CRCOK) || (!vgfs.vgfa.CRCOK)) printf("Warning: sector %d on track %d, side %d with BAD CRC!\n", sec + 1, trk, side); if (vgfs.SectorLength != 256) printf("Warning: sector %d on track %d, side %d is non 256 bytes!\n", sec + 1, trk, side); } else { - write(hfile, nullbuf, 256); + FileWriteAdv(hfile, nullbuf, 256); printf("DANGER! Sector %d on track %d, side %d not found!\n", sec + 1, trk, side); } } @@ -3014,19 +3014,10 @@ int x2trd(const char *name, fileTYPE *f) return 0; } - img->writeTRD(f->fd); + img->writeTRD(f); delete(img); - struct stat64 st; - int ret = fstat64(f->fd, &st); - if (ret < 0) - { - printf("x2trd(fstat) error: %d.\n", ret); - FileClose(f); - return 0; - } - - f->size = st.st_size; + f->size = FileGetSize(f); FileSeekLBA(f, 0); printf("x2trd: vtrd size=%llu.\n", f->size); diff --git a/DiskImage.h b/DiskImage.h index d819de8..b3cd9fd 100644 --- a/DiskImage.h +++ b/DiskImage.h @@ -80,7 +80,7 @@ public: void Open(const char *filename, bool ReadOnly); - void writeTRD(int hfile); + void writeTRD(fileTYPE *hfile); void readSCL(int hfile, bool readonly); void readFDI(int hfile, bool readonly); @@ -189,4 +189,4 @@ int x2trd(const char *name, fileTYPE *f); int x2trd_ext_supp(const char *name); //----------------------------------------------------------------------------- -#endif \ No newline at end of file +#endif diff --git a/file_io.cpp b/file_io.cpp index 542cfc9..0f8c61b 100644 --- a/file_io.cpp +++ b/file_io.cpp @@ -36,10 +36,10 @@ static char full_path[1200]; void FileClose(fileTYPE *file) { - if (file->fd > 0) + if (file->filp) { - //printf("closing %d\n", file->fd); - close(file->fd); + //printf("closing %p\n", file->filp); + fclose(file->filp); if (file->type == 1) { if (file->name[0] == '/') @@ -49,7 +49,7 @@ void FileClose(fileTYPE *file) file->type = 0; } } - file->fd = -1; + file->filp = nullptr; } int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute) @@ -71,11 +71,19 @@ int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute) char *p = strrchr(full_path, '/'); strcpy(file->name, (mode == -1) ? full_path : p+1); - file->fd = (mode == -1) ? shm_open("/vtrd", O_CREAT | O_RDWR | O_TRUNC, 0777) : open(full_path, mode, 0777); - if (file->fd <= 0) + int fd = (mode == -1) ? shm_open("/vtrd", O_CREAT | O_RDWR | O_TRUNC, 0777) : open(full_path, mode, 0777); + if (fd <= 0) { - if(!mute) printf("FileOpenEx(open) File:%s, error: %d.\n", full_path, file->fd); - file->fd = -1; + if(!mute) printf("FileOpenEx(open) File:%s, error: %s.\n", full_path, strerror(errno)); + return 0; + } + + const char *fmode = mode & O_RDWR ? "w+" : "r"; + file->filp = fdopen(fd, fmode); + if (!file->filp) + { + if(!mute) printf("FileOpenEx(fdopen) File:%s, error: %s.\n", full_path, strerror(errno)); + close(fd); return 0; } @@ -89,7 +97,7 @@ int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute) else { struct stat64 st; - int ret = fstat64(file->fd, &st); + int ret = fstat64(fd, &st); if (ret < 0) { if (!mute) printf("FileOpenEx(fstat) File:%s, error: %d.\n", full_path, ret); @@ -108,10 +116,10 @@ int FileOpenEx(fileTYPE *file, const char *name, int mode, char mute) __off64_t FileGetSize(fileTYPE *file) { - if (file->fd <= 0) return 0; + if (!file->filp) return 0; struct stat64 st; - int ret = fstat64(file->fd, &st); + int ret = fstat64(fileno(file->filp), &st); return (ret < 0) ? 0 : st.st_size; } @@ -122,11 +130,11 @@ int FileOpen(fileTYPE *file, const char *name, char mute) int FileNextSector(fileTYPE *file) { - __off64_t newoff = lseek64(file->fd, file->offset + 512, SEEK_SET); + __off64_t newoff = lseek64(fileno(file->filp), file->offset + 512, SEEK_SET); if (newoff != file->offset + 512) { //printf("Fail to seek to next sector. File: %s.\n", file->name); - lseek64(file->fd, file->offset, SEEK_SET); + lseek64(fileno(file->filp), file->offset, SEEK_SET); return 0; } @@ -136,7 +144,7 @@ int FileNextSector(fileTYPE *file) int FileSeek(fileTYPE *file, __off64_t offset, int origin) { - __off64_t newoff = lseek64(file->fd, offset, origin); + __off64_t newoff = lseek64(fileno(file->filp), offset, origin); if(newoff<0) { printf("Fail to seek the file.\n"); @@ -174,7 +182,7 @@ int FileReadEx(fileTYPE *file, void *pBuffer, int nSize) { for (int i = 0; i < nSize; i++) { - int ret = read(file->fd, tmpbuff, 512); + int ret = fread(tmpbuff, 512, 1, file->filp); if (ret < 0) { printf("FileRead error(%d).\n", ret); @@ -188,7 +196,7 @@ int FileReadEx(fileTYPE *file, void *pBuffer, int nSize) } else { - int ret = read(file->fd, pBuffer, nSize * 512); + int ret = fread(pBuffer, nSize, 512, file->filp); if (ret < 0) { printf("FileRead error(%d).\n", ret); @@ -208,7 +216,7 @@ int FileWrite(fileTYPE *file, void *pBuffer) return 0; } - int ret = write(file->fd, pBuffer, 512); + int ret = fwrite(pBuffer, 512, 1, file->filp); if (ret < 0) { printf("FileWrite error(%d).\n", ret); @@ -221,7 +229,7 @@ int FileWrite(fileTYPE *file, void *pBuffer) // Read with offset advancing int FileReadAdv(fileTYPE *file, void *pBuffer, int length) { - ssize_t ret = read(file->fd, pBuffer, length); + ssize_t ret = fread(pBuffer, length, 1, file->filp); if (ret < 0) { printf("FileReadAdv error(%d).\n", ret); @@ -240,7 +248,7 @@ int FileReadSec(fileTYPE *file, void *pBuffer) // Write with offset advancing int FileWriteAdv(fileTYPE *file, void *pBuffer, int length) { - int ret = write(file->fd, pBuffer, length); + int ret = fwrite(pBuffer, length, 1, file->filp); if (ret < 0) { printf("FileWriteAdv error(%d).\n", ret); diff --git a/file_io.h b/file_io.h index f1bf899..e34a43b 100644 --- a/file_io.h +++ b/file_io.h @@ -1,6 +1,7 @@ #ifndef _FAT16_H_INCLUDED #define _FAT16_H_INCLUDED +#include #include #include #include @@ -8,7 +9,7 @@ typedef struct { - int fd; + FILE *filp; int mode; int type; __off64_t size; diff --git a/support/sharpmz/sharpmz.cpp b/support/sharpmz/sharpmz.cpp index 1568bc5..4cb8c30 100644 --- a/support/sharpmz/sharpmz.cpp +++ b/support/sharpmz/sharpmz.cpp @@ -65,28 +65,13 @@ int sharpmz_file_write(fileTYPE *file, const char *fileName) sprintf(fullPath, "%s/%s/%s", getRootDir(), SHARPMZ_CORE_NAME, fileName); - file->mode = O_WRONLY | O_CREAT | O_TRUNC | O_SYNC | S_IRWXU | S_IRWXG | S_IRWXO; - file->type = 0; - - file->fd = open(fullPath, file->mode); - if (file->fd <= 0) + const int mode = O_WRONLY | O_CREAT | O_TRUNC | O_SYNC | S_IRWXU | S_IRWXG | S_IRWXO; + ret = FileOpenEx(file, fullPath, mode); + if (!ret) { - sharpmz_debugf("sharpmz_file_write (open) - File:%s, error: %d.\n", fullPath, file->fd); - file->fd = -1; - return 0; + sharpmz_debugf("sharpmz_file_write (FileOpenEx) - File:%s, error: %d.\n", fullPath, ret); } - ret = fstat64(file->fd, &st); - if (ret < 0) - { - sharpmz_debugf("sharpmz_file_write (stat) - File:%s, error: %d.\n", fullPath, ret); - FileClose(file); - return 0; - } - - file->size = st.st_size; - file->offset = 0; - // Success. return 1; } @@ -1437,7 +1422,7 @@ int sharpmz_file_read(fileTYPE *file, void *pBuffer, int nSize) return 0; } - int ret = read(file->fd, pBuffer, nSize); + int ret = FileReadAdv(file, pBuffer, nSize); if (ret < 0) { sharpmz_debugf("file_read error(%d).\n", ret);