diff --git a/modules/SACT/sact.h b/modules/SACT/sact.h index 4d5b764..e25c4c9 100644 --- a/modules/SACT/sact.h +++ b/modules/SACT/sact.h @@ -63,7 +63,6 @@ typedef struct { // SACTEFAM を使ったマスク typedef struct { - int fd; // SACTEFAM.KLD のファイルディスクプリタ char *mapadr; // mmap された最初のアドレス off_t size; // mmap した大きさ int datanum; // SACTEFAM.KLD 中のマスクファイルの数 diff --git a/modules/SACT/sactamask.c b/modules/SACT/sactamask.c index 06f5dbf..1f329ea 100644 --- a/modules/SACT/sactamask.c +++ b/modules/SACT/sactamask.c @@ -28,10 +28,12 @@ #include #include #include -#include #include #include #include +#ifdef HAVE_MMAP +#include +#endif #include "portab.h" #include "system.h" @@ -76,16 +78,31 @@ int smask_init(char *path) { return NG; } +#ifdef HAVE_MMAP if (MAP_FAILED == (adr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))) { WARNING("mmap: %s\n", strerror(errno)); close(fd); return NG; } +#else + adr = malloc(sbuf.st_size); + size_t bytes = 0; + while (bytes < sbuf.st_size) { + ssize_t ret = read(fd, adr + bytes, sbuf.st_size - bytes); + if (ret <= 0) { + WARNING("read: %s\n", strerror(errno)); + close(fd); + free(adr); + return NG; + } + bytes += ret; + } +#endif + close(fd); am = &sact.am; am->mapadr = adr; am->size = sbuf.st_size; - am->fd = fd; am->datanum = LittleEndian_getDW(adr, 0); am->no = malloc(sizeof(int) * am->datanum); diff --git a/modules/lib/alk.c b/modules/lib/alk.c index 06e6484..52c7b93 100644 --- a/modules/lib/alk.c +++ b/modules/lib/alk.c @@ -29,10 +29,12 @@ #include #include #include -#include #include #include #include +#ifdef HAVE_MMAP +#include +#endif #include "portab.h" #include "system.h" @@ -56,23 +58,41 @@ alk_t *alk_new(char *path) { return NULL; } +#ifdef HAVE_MMAP if (MAP_FAILED == (adr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0))) { WARNING("mmap: %s\n", strerror(errno)); close(fd); return NULL; } +#else + adr = malloc(sbuf.st_size); + size_t bytes = 0; + while (bytes < sbuf.st_size) { + ssize_t ret = read(fd, adr + bytes, sbuf.st_size - bytes); + if (ret <= 0) { + WARNING("read: %s\n", strerror(errno)); + close(fd); + free(adr); + return NULL; + } + bytes += ret; + } +#endif + close(fd); if (0 != strncmp(adr, "ALK0", 4)) { - WARNING("mmap: %s\n", strerror(errno)); + WARNING("%s: not an ALK file\n", path); +#ifdef HAVE_MMAP munmap(adr, sbuf.st_size); - close(fd); +#else + free(adr); +#endif return NULL; } alk = calloc(1, sizeof(alk_t)); alk->mapadr = adr; alk->size = sbuf.st_size; - alk->fd = fd; alk->datanum = LittleEndian_getDW(adr, 4); alk->offset = malloc(sizeof(int) * alk->datanum); @@ -87,8 +107,11 @@ alk_t *alk_new(char *path) { int alk_free(alk_t *alk) { if (alk == NULL) return OK; +#ifdef HAVE_MMAP munmap(alk->mapadr, alk->size); - close(alk->fd); +#else + free(alk->mapadr); +#endif free(alk->offset); free(alk); diff --git a/modules/lib/alk.h b/modules/lib/alk.h index a808a28..9f2ed40 100644 --- a/modules/lib/alk.h +++ b/modules/lib/alk.h @@ -38,7 +38,6 @@ */ typedef struct { - int fd; /* .alk ファイルの file discpriter */ char *mapadr; /* mmap したファイルの先頭アドレス */ off_t size; /* mmap したバイト数(ファイルサイズ) */ int datanum; /* .alk ファイル中のファイル数 */