diff --git a/src/disk/mo.c b/src/disk/mo.c index a5adbb108..13715947a 100644 --- a/src/disk/mo.c +++ b/src/disk/mo.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #define HAVE_STDARG_H #include <86box/86box.h> @@ -42,7 +43,7 @@ #include #include #else -#include +#include #endif mo_drive_t mo_drives[MO_NUM]; @@ -1041,46 +1042,41 @@ mo_format(mo_t *dev) { unsigned long size; int ret; - int fd; + int fd = fileno(dev->drv->f); + int64_t liSize = 0; mo_log("MO %i: Formatting media...\n", dev->id); + if (fd == -1) { + mo_log("MO %i: Failed to get file descriptor.\n", dev->id); + return; + } + fseek(dev->drv->f, 0, SEEK_END); size = (uint32_t) ftello64(dev->drv->f); - HANDLE fh; - LARGE_INTEGER liSize; + errno = 0; + rewind(dev->drv->f); - fd = _fileno(dev->drv->f); - fh = (HANDLE)_get_osfhandle(fd); - - liSize.QuadPart = 0; - - ret = (int)SetFilePointerEx(fh, liSize, NULL, FILE_BEGIN); - - if (!ret) { + if (errno) { mo_log("MO %i: Failed seek to start of image file\n", dev->id); return; } - ret = (int)SetEndOfFile(fh); - - if (!ret) { + ret = ftruncate(fd, 0); + if (ret == -1) { mo_log("MO %i: Failed to truncate image file to 0\n", dev->id); return; } - liSize.QuadPart = size; - ret = (int)SetFilePointerEx(fh, liSize, NULL, FILE_BEGIN); - - if (!ret) { + liSize = size; + ret = fseek(dev->drv->f, 0, SEEK_END); + if (ret == -1) { mo_log("MO %i: Failed seek to end of image file\n", dev->id); return; } - - ret = (int)SetEndOfFile(fh); - - if (!ret) { + ret = ftruncate(fd, liSize); + if (ret == -1) { mo_log("MO %i: Failed to truncate image file to %llu\n", dev->id, size); return; }