summaryrefslogtreecommitdiff
path: root/uisimulator/common/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'uisimulator/common/io.c')
-rw-r--r--uisimulator/common/io.c124
1 files changed, 117 insertions, 7 deletions
diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c
index 6335735f8f..48b888a027 100644
--- a/uisimulator/common/io.c
+++ b/uisimulator/common/io.c
@@ -20,6 +20,7 @@
20#include <stdio.h> 20#include <stdio.h>
21#include <stdlib.h> 21#include <stdlib.h>
22#include <string.h> 22#include <string.h>
23#include <stdarg.h>
23#include <sys/stat.h> 24#include <sys/stat.h>
24#ifdef __FreeBSD__ 25#ifdef __FreeBSD__
25#include <sys/param.h> 26#include <sys/param.h>
@@ -28,6 +29,10 @@
28#include <sys/vfs.h> 29#include <sys/vfs.h>
29#endif 30#endif
30 31
32#ifdef WIN32
33#include <windows.h>
34#endif
35
31#ifndef _MSC_VER 36#ifndef _MSC_VER
32#include <dirent.h> 37#include <dirent.h>
33#include <unistd.h> 38#include <unistd.h>
@@ -35,18 +40,27 @@
35#include "dir-win32.h" 40#include "dir-win32.h"
36#endif 41#endif
37 42
43#define MAX_PATH 260
44
38#include <fcntl.h> 45#include <fcntl.h>
39#include "debug.h" 46#include "debug.h"
40 47
41#define DIRFUNCTIONS_DEFINED /* prevent those prototypes */
42#define dirent sim_dirent
43#define DIR SIMDIR
44#include "../../firmware/include/dir.h"
45#undef dirent
46#undef DIR
47
48#define SIMULATOR_ARCHOS_ROOT "archos" 48#define SIMULATOR_ARCHOS_ROOT "archos"
49 49
50struct sim_dirent {
51 unsigned char d_name[MAX_PATH];
52 int attribute;
53 int size;
54 int startcluster;
55 unsigned short wrtdate; /* Last write date */
56 unsigned short wrttime; /* Last write time */
57};
58
59struct dirstruct {
60 void *dir; /* actually a DIR* dir */
61 char *name;
62} SIM_DIR;
63
50struct mydir { 64struct mydir {
51 DIR *dir; 65 DIR *dir;
52 char *name; 66 char *name;
@@ -113,6 +127,8 @@ struct sim_dirent *sim_readdir(MYDIR *dir)
113 dir->name, x11->d_name); 127 dir->name, x11->d_name);
114 stat(buffer, &s); /* get info */ 128 stat(buffer, &s); /* get info */
115 129
130#define ATTR_DIRECTORY 0x10
131
116 secret.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0; 132 secret.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0;
117 secret.size = s.st_size; 133 secret.size = s.st_size;
118 secret.wrtdate = (unsigned short)(s.st_mtime >> 16); 134 secret.wrtdate = (unsigned short)(s.st_mtime >> 16);
@@ -181,6 +197,7 @@ int sim_mkdir(const char *name, mode_t mode)
181 197
182 debugf("We create the real directory '%s'\n", buffer); 198 debugf("We create the real directory '%s'\n", buffer);
183#ifdef WIN32 199#ifdef WIN32
200 /* since we build with -DNOCYGWIN we have the plain win32 version */
184 return (mkdir)(buffer); 201 return (mkdir)(buffer);
185#else 202#else
186 return (mkdir)(buffer, 0666); 203 return (mkdir)(buffer, 0666);
@@ -260,3 +277,96 @@ void fat_size(unsigned int* size, unsigned int* free)
260 } 277 }
261#endif 278#endif
262} 279}
280
281int sim_fsync(int fd)
282{
283#ifdef WIN32
284 return _commit(fd);
285#else
286 return fsync(fd);
287#endif
288}
289
290#ifdef WIN32
291/* sim-win32 */
292typedef enum plugin_status (*plugin_fn)(void* api, void* param);
293#define dlopen(_x_, _y_) LoadLibrary(_x_)
294#define dlsym(_x_, _y_) (plugin_fn)GetProcAddress(_x_, _y_)
295#define dlclose(_x_) FreeLibrary(_x_)
296#define dlerror() "Unknown"
297#else
298/* sim-x11 */
299#include <dlfcn.h>
300#endif
301
302void *sim_plugin_load(char *plugin, int *fd)
303{
304 void* pd;
305 char path[256];
306 char buf[256];
307 int (*plugin_start)(void * api, void* param);
308
309 snprintf(path, sizeof path, "archos%s", plugin);
310
311 *fd = -1;
312
313 pd = dlopen(path, RTLD_NOW);
314 if (!pd) {
315 snprintf(buf, sizeof buf, "failed to load %s", plugin);
316 DEBUGF("dlopen(%s): %s\n",path,dlerror());
317 dlclose(pd);
318 return NULL;
319 }
320
321 plugin_start = dlsym(pd, "plugin_start");
322 if (!plugin_start) {
323 plugin_start = dlsym(pd, "_plugin_start");
324 if (!plugin_start) {
325 dlclose(pd);
326 return NULL;
327 }
328 }
329 *fd = pd; /* success */
330 return plugin_start;
331}
332
333void sim_plugin_close(int pd)
334{
335 dlclose(pd);
336}
337
338#ifndef WIN32
339/* the win32 version is in debug-win32.c */
340
341void debug_init(void)
342{
343 /* nothing to be done */
344}
345
346void debugf(const char *fmt, ...)
347{
348 va_list ap;
349 va_start( ap, fmt );
350 vfprintf( stderr, fmt, ap );
351 va_end( ap );
352}
353
354void ldebugf(const char* file, int line, const char *fmt, ...)
355{
356 va_list ap;
357 va_start( ap, fmt );
358 fprintf( stderr, "%s:%d ", file, line );
359 vfprintf( stderr, fmt, ap );
360 va_end( ap );
361}
362
363#endif
364
365int sim_ftruncate(int fd, off_t length)
366{
367#ifdef WIN32
368 return _chsize(fd, length);
369#else
370 return ftruncate(fd, length);
371#endif
372}