diff options
author | Linus Nielsen Feltzing <linus@haxx.se> | 2004-06-10 13:29:52 +0000 |
---|---|---|
committer | Linus Nielsen Feltzing <linus@haxx.se> | 2004-06-10 13:29:52 +0000 |
commit | a6142ab7ab58f69a3f1a034db4bdf1eff24d3dd6 (patch) | |
tree | ef00c3ec8074ccb080b221c7d1dd4b3d03c8fd87 /uisimulator/common/io.c | |
parent | 5fc1b64ae051e454d2b3bf3a20be5d88937e55e7 (diff) | |
download | rockbox-a6142ab7ab58f69a3f1a034db4bdf1eff24d3dd6.tar.gz rockbox-a6142ab7ab58f69a3f1a034db4bdf1eff24d3dd6.zip |
Finally, the archos directory sandbox works in the same way for both X11 and win32 simulators. Unfortunately, this breaks the VC++ compatibility. Also, the plugin API now supports DEBUGF. Last, but not least, we have a new plugin, vbrfix.rock.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4726 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'uisimulator/common/io.c')
-rw-r--r-- | uisimulator/common/io.c | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/uisimulator/common/io.c b/uisimulator/common/io.c new file mode 100644 index 0000000000..745e417fa6 --- /dev/null +++ b/uisimulator/common/io.c | |||
@@ -0,0 +1,225 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 Daniel Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include <stdio.h> | ||
21 | #include <stdlib.h> | ||
22 | #include <string.h> | ||
23 | #include <sys/stat.h> | ||
24 | #ifdef __FreeBSD__ | ||
25 | #include <sys/param.h> | ||
26 | #include <sys/mount.h> | ||
27 | #elif !defined(WIN32) | ||
28 | #include <sys/vfs.h> | ||
29 | #endif | ||
30 | #include <dirent.h> | ||
31 | #include <unistd.h> | ||
32 | |||
33 | #include <fcntl.h> | ||
34 | #include "debug.h" | ||
35 | |||
36 | #define DIRFUNCTIONS_DEFINED /* prevent those prototypes */ | ||
37 | #define dirent sim_dirent | ||
38 | #define DIR SIMDIR | ||
39 | #include "../../firmware/include/dir.h" | ||
40 | #undef dirent | ||
41 | #undef DIR | ||
42 | |||
43 | #define SIMULATOR_ARCHOS_ROOT "archos" | ||
44 | |||
45 | struct mydir { | ||
46 | DIR *dir; | ||
47 | char *name; | ||
48 | }; | ||
49 | |||
50 | typedef struct mydir MYDIR; | ||
51 | |||
52 | MYDIR *sim_opendir(const char *name) | ||
53 | { | ||
54 | char buffer[256]; /* sufficiently big */ | ||
55 | DIR *dir; | ||
56 | |||
57 | if(name[0] == '/') { | ||
58 | sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name); | ||
59 | dir=(DIR *)opendir(buffer); | ||
60 | } | ||
61 | else | ||
62 | dir=(DIR *)opendir(name); | ||
63 | |||
64 | if(dir) { | ||
65 | MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR)); | ||
66 | my->dir = dir; | ||
67 | my->name = (char *)strdup(name); | ||
68 | |||
69 | return my; | ||
70 | } | ||
71 | /* failed open, return NULL */ | ||
72 | return (MYDIR *)0; | ||
73 | } | ||
74 | |||
75 | struct sim_dirent *sim_readdir(MYDIR *dir) | ||
76 | { | ||
77 | char buffer[512]; /* sufficiently big */ | ||
78 | static struct sim_dirent secret; | ||
79 | struct stat s; | ||
80 | struct dirent *x11 = (readdir)(dir->dir); | ||
81 | |||
82 | if(!x11) | ||
83 | return (struct sim_dirent *)0; | ||
84 | |||
85 | strcpy(secret.d_name, x11->d_name); | ||
86 | |||
87 | /* build file name */ | ||
88 | sprintf(buffer, SIMULATOR_ARCHOS_ROOT "%s/%s", | ||
89 | dir->name, x11->d_name); | ||
90 | stat(buffer, &s); /* get info */ | ||
91 | |||
92 | secret.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0; | ||
93 | secret.size = s.st_size; | ||
94 | |||
95 | return &secret; | ||
96 | } | ||
97 | |||
98 | void sim_closedir(MYDIR *dir) | ||
99 | { | ||
100 | free(dir->name); | ||
101 | (closedir)(dir->dir); | ||
102 | |||
103 | free(dir); | ||
104 | } | ||
105 | |||
106 | |||
107 | int sim_open(const char *name, int opts) | ||
108 | { | ||
109 | char buffer[256]; /* sufficiently big */ | ||
110 | |||
111 | if(name[0] == '/') { | ||
112 | sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name); | ||
113 | |||
114 | debugf("We open the real file '%s'\n", buffer); | ||
115 | return (open)(buffer, opts); | ||
116 | } | ||
117 | return (open)(name, opts); | ||
118 | } | ||
119 | |||
120 | int sim_close(int fd) | ||
121 | { | ||
122 | return (close)(fd); | ||
123 | } | ||
124 | |||
125 | int sim_creat(const char *name, mode_t mode) | ||
126 | { | ||
127 | char buffer[256]; /* sufficiently big */ | ||
128 | (void)mode; | ||
129 | if(name[0] == '/') { | ||
130 | sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name); | ||
131 | |||
132 | debugf("We create the real file '%s'\n", buffer); | ||
133 | return (creat)(buffer, 0666); | ||
134 | } | ||
135 | return (creat)(name, 0666); | ||
136 | } | ||
137 | |||
138 | int sim_mkdir(const char *name, mode_t mode) | ||
139 | { | ||
140 | char buffer[256]; /* sufficiently big */ | ||
141 | (void)mode; | ||
142 | |||
143 | sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name); | ||
144 | |||
145 | debugf("We create the real directory '%s'\n", buffer); | ||
146 | #ifdef WIN32 | ||
147 | return (mkdir)(buffer); | ||
148 | #else | ||
149 | return (mkdir)(buffer, 0666); | ||
150 | #endif | ||
151 | } | ||
152 | |||
153 | int sim_rmdir(const char *name) | ||
154 | { | ||
155 | char buffer[256]; /* sufficiently big */ | ||
156 | if(name[0] == '/') { | ||
157 | sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name); | ||
158 | |||
159 | debugf("We remove the real directory '%s'\n", buffer); | ||
160 | return (rmdir)(buffer); | ||
161 | } | ||
162 | return (rmdir)(name); | ||
163 | } | ||
164 | |||
165 | int sim_remove(const char *name) | ||
166 | { | ||
167 | char buffer[256]; /* sufficiently big */ | ||
168 | |||
169 | if(name[0] == '/') { | ||
170 | sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name); | ||
171 | |||
172 | debugf("We remove the real file '%s'\n", buffer); | ||
173 | return (remove)(buffer); | ||
174 | } | ||
175 | return (remove)(name); | ||
176 | } | ||
177 | |||
178 | int sim_rename(const char *oldpath, const char* newpath) | ||
179 | { | ||
180 | char buffer1[256]; | ||
181 | char buffer2[256]; | ||
182 | |||
183 | if(oldpath[0] == '/') { | ||
184 | sprintf(buffer1, "%s%s", SIMULATOR_ARCHOS_ROOT, oldpath); | ||
185 | sprintf(buffer2, "%s%s", SIMULATOR_ARCHOS_ROOT, newpath); | ||
186 | |||
187 | debugf("We rename the real file '%s' to '%s'\n", buffer1, buffer2); | ||
188 | return (rename)(buffer1, buffer2); | ||
189 | } | ||
190 | return -1; | ||
191 | } | ||
192 | |||
193 | int sim_filesize(int fd) | ||
194 | { | ||
195 | int old = lseek(fd, 0, SEEK_CUR); | ||
196 | int size = lseek(fd, 0, SEEK_END); | ||
197 | lseek(fd, old, SEEK_SET); | ||
198 | |||
199 | return(size); | ||
200 | } | ||
201 | |||
202 | void fat_size(unsigned int* size, unsigned int* free) | ||
203 | { | ||
204 | #ifdef WIN32 | ||
205 | *size = 2049; | ||
206 | *free = 1037; | ||
207 | #else | ||
208 | struct statfs fs; | ||
209 | |||
210 | if (!statfs(".", &fs)) { | ||
211 | DEBUGF("statfs: bsize=%d blocks=%d free=%d\n", | ||
212 | fs.f_bsize, fs.f_blocks, fs.f_bfree); | ||
213 | if (size) | ||
214 | *size = fs.f_blocks * (fs.f_bsize / 1024); | ||
215 | if (free) | ||
216 | *free = fs.f_bfree * (fs.f_bsize / 1024); | ||
217 | } | ||
218 | else { | ||
219 | if (size) | ||
220 | *size = 0; | ||
221 | if (free) | ||
222 | *free = 0; | ||
223 | } | ||
224 | #endif | ||
225 | } | ||