rendered paste bodyIndex: Makefile.in
===================================================================
--- Makefile.in (revision 4839)
+++ Makefile.in (working copy)
@@ -60,8 +60,8 @@
JMA_CO=@JMA_FILES@
MAIN_AO=@DEBUGGER_A@ endmem.o init.o vcache.o ztime.o
-MAIN_CO=cfg.o @DEBUGGER_C@ initc.o input.o md.o patch.o ui.o version.o zloader.o\
- zmovie.o zpath.o zstate.o ztimec.o
+MAIN_CO=cfg.o @DEBUGGER_C@ initc.o input.o md.o patch.o ui.o version.o zdir.o\
+ zloader.o zmovie.o zpath.o zstate.o ztimec.o
MMLIB_CO=@MMLIB_FILES@
Index: gui/guifuncs.c
===================================================================
--- gui/guifuncs.c (revision 4839)
+++ gui/guifuncs.c (working copy)
@@ -46,7 +46,6 @@
#ifndef _MSC_VER
#include <stdint.h>
-#include <dirent.h>
#include <unistd.h>
#endif
@@ -56,6 +55,7 @@
#include "../input.h"
#include "../asm_call.h"
#include "../zloader.h"
+#include "../zdir.h"
#define BIT(X) (1 << (X))
@@ -889,21 +889,21 @@
#define INFO_LEN (0xFF - 0xC0)
#define INAME_LEN 21
-static const char *get_rom_name(const char *filename, char *namebuffer, struct stat *filestats)
+static const char *get_rom_name(struct dirent_info *entry, char *namebuffer)
{
int InfoScore(char *);
unsigned int sum(unsigned char *array, unsigned int size);
- char *last_dot = strrchr(filename, '.');
+ char *last_dot = strrchr(entry->name, '.');
if (!last_dot || (strcasecmp(last_dot, ".zip") && strcasecmp(last_dot, ".gz") && strcasecmp(last_dot, ".jma")))
{
- if ((filestats->st_size >= 0x8000) && (filestats->st_size <= 0x600000+HEADER_SIZE))
+ if ((entry->size >= 0x8000) && (entry->size <= 0x600000+HEADER_SIZE))
{
- FILE *fp = fopen_dir(ZRomPath, filename, "rb");
+ FILE *fp = fopen_dir(ZRomPath, entry->name, "rb");
if (fp)
{
unsigned char HeaderBuffer[HEADER_SIZE];
- int HeaderSize = 0, HasHeadScore = 0, NoHeadScore = 0, HeadRemain = filestats->st_size & 0x7FFF;
+ int HeaderSize = 0, HasHeadScore = 0, NoHeadScore = 0, HeadRemain = entry->size & 0x7FFF;
bool EHi = false;
switch(HeadRemain)
@@ -945,7 +945,7 @@
HeaderSize = HasHeadScore > NoHeadScore ? HEADER_SIZE : 0;
- if (filestats->st_size - HeaderSize >= 0x500000)
+ if (entry->size - HeaderSize >= 0x500000)
{
fseek(fp, 0x40FFC0 + HeaderSize, SEEK_SET);
fread(HeaderBuffer, 1, INFO_LEN, fp);
@@ -958,7 +958,7 @@
if (!EHi)
{
- if (filestats->st_size - HeaderSize >= 0x10000)
+ if (entry->size - HeaderSize >= 0x10000)
{
char LoHead[INFO_LEN], HiHead[INFO_LEN];
int LoScore, HiScore;
@@ -973,10 +973,10 @@
strncpy(namebuffer, LoScore > HiScore ? LoHead : HiHead, INAME_LEN);
- if (filestats->st_size - HeaderSize >= 0x20000)
+ if (entry->size - HeaderSize >= 0x20000)
{
int IntLScore;
- fseek(fp, (filestats->st_size - HeaderSize) / 2 + 0x7FC0 + HeaderSize, SEEK_SET);
+ fseek(fp, (entry->size - HeaderSize) / 2 + 0x7FC0 + HeaderSize, SEEK_SET);
fread(LoHead, 1, INFO_LEN, fp);
IntLScore = InfoScore(LoHead) / 2;
@@ -1006,7 +1006,7 @@
}
else //Compressed archive
{
- return(filename);
+ return(entry->name);
}
namebuffer[21] = 0;
return(namebuffer);
@@ -1142,31 +1142,30 @@
if ((dir = opendir(ZRomPath)))
{
- struct stat stat_buffer;
- struct dirent *entry;
+ struct dirent_info *entry;
- while ((entry = readdir(dir)))
+ while ((entry = readdir_info(dir)))
{
- if ((*entry->d_name != '.') && !stat_dir(ZRomPath, entry->d_name, &stat_buffer))
+ if (*entry->name != '.')
{
- if (S_ISDIR(stat_buffer.st_mode))
+ if (S_ISDIR(entry->mode))
{
if (lists&LIST_DN)
{
- add_list(&d_names, entry->d_name);
+ add_list(&d_names, entry->name);
}
}
- else if (!snes_ext_match || snes_extension_match(entry->d_name))
+ else if (!snes_ext_match || snes_extension_match(entry->name))
{
if (_USE_LFN && (lists&LIST_LFN))
{
- add_list(&lf_names, entry->d_name);
+ add_list(&lf_names, entry->name);
}
if (lists&LIST_IN)
{
char namebuffer[22];
- add_list(&i_names, get_rom_name(entry->d_name, namebuffer, &stat_buffer));
+ add_list(&i_names, get_rom_name(entry, namebuffer));
}
#ifdef __MSDOS__
@@ -1174,11 +1173,11 @@
{
if (!_USE_LFN) //_USE_LFN won't be true when running under pure DOS
{
- add_list(&et_names, entry->d_name);
+ add_list(&et_names, entry->name);
}
else
{
- char *sfn = realpath_sfn_dir(ZRomPath, entry->d_name, 0);
+ char *sfn = realpath_sfn_dir(ZRomPath, entry->name, 0);
if (sfn)
{
add_list(&et_names, basename(sfn));
@@ -1187,7 +1186,7 @@
else
{
char sfn[13];
- _lfn_gen_short_fname(entry->d_name, sfn);
+ _lfn_gen_short_fname(entry->name, sfn);
add_list(&et_names, sfn);
}
}
Index: zdir.c
===================================================================
--- zdir.c (revision 0)
+++ zdir.c (revision 0)
@@ -0,0 +1,153 @@
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include "zpath.h"
+#include "zdir.h"
+
+#ifndef __UNIXSDL__
+
+#ifdef __MSDOS__
+#include <dos.h>
+#define _findfirst(regex, info) findfirst(regex, info, FA_HIDDEN|FA_DIREC)
+#define _findnext(handle, info) findnext(info)
+#define _findclose(handle)
+#define S_IWRITE S_IWUSR
+#define S_IREAD S_IRUSR
+#else
+#define ff_name name
+#define ff_fsize size
+#define ff_attrib attrib
+#endif
+
+//Note, these are faster than the built in DJGPP/MinGW ones
+z_DIR *z_opendir(const char *path)
+{
+ z_DIR *dir = 0;
+ if (path && *path)
+ {
+ char search[PATH_SIZE];
+ strcpy(search, path);
+ strcatslash(search);
+ strcat(search, "*");
+
+ dir = malloc(sizeof(z_DIR));
+ if (dir)
+ {
+ dir->find_first_handle = _findfirst(search, &dir->fileinfo);
+ if (dir->find_first_handle == -1)
+ {
+ //ENOENT set by findfirst already
+ free(dir);
+ dir = 0;
+ }
+ }
+ else
+ {
+ errno = ENOMEM;
+ }
+ }
+ else
+ {
+ errno = EINVAL;
+ }
+
+ return(dir);
+}
+
+struct z_dirent *z_readdir(z_DIR *dir)
+{
+ struct z_dirent *entry = 0;
+ if (dir->find_first_handle != -1)
+ {
+ entry = &dir->entry;
+ strcpy(entry->d_name, dir->fileinfo.ff_name);
+ if (_findnext(dir->find_first_handle, &dir->fileinfo) == -1)
+ {
+ _findclose(dir->find_first_handle);
+ dir->find_first_handle = -1;
+ }
+ }
+ else
+ {
+ errno = EBADF;
+ }
+
+ return(entry);
+}
+
+int z_closedir(z_DIR *dir)
+{
+ int result = 0;
+
+ if (dir)
+ {
+ if (dir->find_first_handle != -1)
+ {
+ _findclose(dir->find_first_handle);
+ }
+ free(dir);
+ }
+ else
+ {
+ result = -1;
+ errno = EBADF;
+ }
+
+ return(result);
+}
+
+struct dirent_info *readdir_info(z_DIR *dir)
+{
+ static struct dirent_info info;
+ struct dirent_info *infop = 0;
+
+ if (dir->find_first_handle != -1)
+ {
+ strcpy(dir->entry.d_name, dir->fileinfo.ff_name);
+
+ info.name = dir->entry.d_name;
+ info.size = dir->fileinfo.ff_fsize;
+ info.mode = S_IREAD;
+ if (!(dir->fileinfo.ff_attrib & _A_RDONLY)) { info.mode |= S_IWRITE; }
+ if (dir->fileinfo.ff_attrib & _A_SUBDIR) { info.mode |= S_IFDIR; }
+ else { info.mode |= S_IFREG; }
+ infop = &info;
+
+ if (_findnext(dir->find_first_handle, &dir->fileinfo) == -1)
+ {
+ _findclose(dir->find_first_handle);
+ dir->find_first_handle = -1;
+ }
+ }
+ else
+ {
+ errno = EBADF;
+ }
+
+ return(infop);
+}
+
+#else
+
+struct dirent_info *readdir_info(z_DIR *dir)
+{
+ static struct dirent_info info;
+ struct dirent_info *infop = 0;
+
+ struct dirent *entry = readdir(dir);
+ if (entry)
+ {
+ struct stat stat_buffer;
+ if (!fstatat(dirfd(dir), entry->d_name, &stat_buffer, 0))
+ {
+ info.name = entry->d_name;
+ info.size = stat_buffer.st_size;
+ info.mode = stat_buffer.st_mode;
+ infop = &info;
+ }
+ }
+ return(infop);
+}
+#endif
+
Index: zmovie.c
===================================================================
--- zmovie.c (revision 4839)
+++ zmovie.c (working copy)
@@ -42,14 +42,12 @@
#include <io.h>
#define ftruncate chsize
#include "win/safelib.h"
-#include "win/lib.h"
#endif
#define DIR_SLASH "\\"
#define WRITE_BINARY "wb"
#define NULL_FILE "nul"
#endif
#ifndef _MSC_VER
-#include <dirent.h>
#include <unistd.h>
#endif
#include "gblvars.h"
@@ -60,6 +58,7 @@
#include "zpath.h"
#include "zmovie.h"
#include "cpu/dspwrap.h"
+#include "zdir.h"
#define NUMCONV_FR2
#define NUMCONV_FR3
Index: makefile.ms
===================================================================
--- makefile.ms (revision 4839)
+++ makefile.ms (working copy)
@@ -205,7 +205,7 @@
MAINOBJ=endmem${OE} init${OE} initc${OE} patch${OE} ui${OE} vcache${OE}\
version${OE} zmovie${OE} zstate${OE} zloader${OE} cfg${OE} md${OE}\
- input${OE} zpath${OE} ztimec${OE} ztime${OE}
+ input${OE} zdir${OE} zpath${OE} ztimec${OE} ztime${OE}
ifneq (${DEBUGGER},no)
MAINOBJ+= debugger${OE} debugasm${OE}
@@ -318,11 +318,12 @@
${MMLIB_D}/mm.h ${ZIP_D}/zunzip.h
vcache${OE}: vcache.asm macros.mac
version${OE}: version.c
+zdir${OE}: zdir.h zpath.h
zloader${OE}: zloader.c asm_call.h cfg${OE} input${OE} zloader.h zpath.h \
${JMA_D}/zsnesjma.h ${ZIP_D}/zunzip.h
zmovie${OE}: zmovie.c asm_call.h cfg${OE} gblvars.h input${OE} md${OE} \
numconv.h zmovie.h zpath.h ${CPU_D}/dspwrap.h ${JMA_D}/zsnesjma.h \
- ${WIN_D}/lib.h ${WIN_D}/safelib.h ${ZIP_D}/zunzip.h
+ zdir.h ${WIN_D}/safelib.h ${ZIP_D}/zunzip.h
zpath${OE}: zpath.c cfg${OE} zpath.h ${DOS_D}/lib.h ${JMA_D}/zsnesjma.h \
${WIN_D}/lib.h ${WIN_D}/safelib.h ${ZIP_D}/zunzip.h ${CONFLOC}
zstate${OE}: zstate.c asm_call.h cfg${OE} gblvars.h input${OE} numconv.h \
Index: win/lib.c
===================================================================
--- win/lib.c (revision 4839)
+++ win/lib.c (working copy)
@@ -30,88 +30,6 @@
//This file contains library functions that can be found on other OSs
-
-#ifdef _MSC_VER
-
-DIR *opendir(const char *path)
-{
- DIR *dir = 0;
- if (path && *path)
- {
- char search[MAX_PATH];
- strcpy(search, path);
- strcatslash(search);
- strcat(search, "*");
-
- dir = malloc(sizeof(DIR));
- if (dir)
- {
- dir->find_first_handle = _findfirst(search, &dir->fileinfo);
- if (dir->find_first_handle == -1)
- {
- //ENOENT set by findfirst already
- free(dir);
- dir = 0;
- }
- }
- else
- {
- errno = ENOMEM;
- }
- }
- else
- {
- errno = EINVAL;
- }
-
- return(dir);
-}
-
-struct dirent *readdir(DIR *dir)
-{
- struct dirent *entry = 0;
- if (dir->find_first_handle != -1)
- {
- entry = &dir->entry;
- strcpy(entry->d_name, dir->fileinfo.name);
- if (_findnext(dir->find_first_handle, &dir->fileinfo) == -1)
- {
- _findclose(dir->find_first_handle);
- dir->find_first_handle = -1;
- }
- }
- else
- {
- errno = EBADF;
- }
-
- return(entry);
-}
-
-int closedir(DIR *dir)
-{
- int result = 0;
-
- if (dir)
- {
- if (dir->find_first_handle != -1)
- {
- _findclose(dir->find_first_handle);
- }
- free(dir);
- }
- else
- {
- result = -1;
- errno = EBADF;
- }
-
- return(result);
-}
-
-#endif
-
-
char *realpath(const char *path, char *resolved_path)
{
char *ret = 0;
Index: win/lib.h
===================================================================
--- win/lib.h (revision 4839)
+++ win/lib.h (working copy)
@@ -28,23 +28,6 @@
#ifdef _MSC_VER
#define strcasecmp stricmp
#define strncasecmp strnicmp
-
-struct dirent
-{
- char d_name[256];
-};
-
-typedef struct
-{
- intptr_t find_first_handle;
- struct _finddata_t fileinfo;
- struct dirent entry;
-} DIR;
-
-DIR *opendir(const char *path);
-struct dirent *readdir(DIR *dir);
-int closedir(DIR *dir);
-
#endif
char *realpath(const char *path, char *resolved_path);
Index: zdir.h
===================================================================
--- zdir.h (revision 0)
+++ zdir.h (revision 0)
@@ -0,0 +1,54 @@
+#ifndef ZDIR_H
+#define ZDIR_H
+
+struct dirent_info
+{
+ char *name;
+ mode_t mode;
+ off_t size;
+};
+
+#ifndef __UNIXSDL__
+#include <dir.h>
+
+#ifdef __MSDOS__
+#include <stdint.h>
+#define _finddata_t ffblk
+#else
+#include <windows.h>
+#endif
+
+//Avoid clashing with DJGPP and MinGW extras
+
+struct z_dirent
+{
+ char d_name[256];
+};
+
+typedef struct
+{
+ intptr_t find_first_handle;
+ struct _finddata_t fileinfo;
+ struct z_dirent entry;
+} z_DIR;
+
+z_DIR *z_opendir(const char *path);
+struct z_dirent *z_readdir(z_DIR *dir);
+int z_closedir(z_DIR *dir);
+
+#ifndef NO_ZDIR_TYPEDEF
+#define dirent z_dirent
+typedef z_DIR DIR;
+#define opendir z_opendir
+#define readdir z_readdir
+#define closedir z_closedir
+#endif
+
+#else
+#include <dirent.h>
+typedef DIR z_DIR;
+#endif
+
+struct dirent_info *readdir_info(z_DIR *dir);
+
+#endif