diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-05-08 15:16:02 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-05-08 15:16:02 +0000 |
commit | 1c3217909b444b86f87f976925f05ac05555cc6d (patch) | |
tree | a88403f37f9ea9cab683c74ff0dd05e50cd45ac9 /firmware/common | |
parent | 6a63f911fef19eae9aaa96b20dbe57aab63399d9 (diff) | |
download | rockbox-1c3217909b444b86f87f976925f05ac05555cc6d.tar.gz rockbox-1c3217909b444b86f87f976925f05ac05555cc6d.zip |
Added lseek()
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@518 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/common')
-rw-r--r-- | firmware/common/file.c | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c index 41aaea570a..a5d9443aeb 100644 --- a/firmware/common/file.c +++ b/firmware/common/file.c | |||
@@ -100,7 +100,7 @@ int open(char* pathname, int flags) | |||
100 | return -1; | 100 | return -1; |
101 | } | 101 | } |
102 | 102 | ||
103 | openfiles[fd].cacheoffset = 0; | 103 | openfiles[fd].cacheoffset = -1; |
104 | openfiles[fd].fileoffset = 0; | 104 | openfiles[fd].fileoffset = 0; |
105 | openfiles[fd].busy = TRUE; | 105 | openfiles[fd].busy = TRUE; |
106 | return fd; | 106 | return fd; |
@@ -127,7 +127,7 @@ int read(int fd, void* buf, int count) | |||
127 | count = openfiles[fd].size - openfiles[fd].fileoffset; | 127 | count = openfiles[fd].size - openfiles[fd].fileoffset; |
128 | 128 | ||
129 | /* any head bytes? */ | 129 | /* any head bytes? */ |
130 | if ( openfiles[fd].cacheoffset ) { | 130 | if ( openfiles[fd].cacheoffset != -1 ) { |
131 | int headbytes; | 131 | int headbytes; |
132 | int offs = openfiles[fd].cacheoffset; | 132 | int offs = openfiles[fd].cacheoffset; |
133 | if ( count <= SECTOR_SIZE - openfiles[fd].cacheoffset ) { | 133 | if ( count <= SECTOR_SIZE - openfiles[fd].cacheoffset ) { |
@@ -138,7 +138,7 @@ int read(int fd, void* buf, int count) | |||
138 | } | 138 | } |
139 | else { | 139 | else { |
140 | headbytes = SECTOR_SIZE - openfiles[fd].cacheoffset; | 140 | headbytes = SECTOR_SIZE - openfiles[fd].cacheoffset; |
141 | openfiles[fd].cacheoffset = 0; | 141 | openfiles[fd].cacheoffset = -1; |
142 | } | 142 | } |
143 | 143 | ||
144 | /* eof? */ | 144 | /* eof? */ |
@@ -169,7 +169,7 @@ int read(int fd, void* buf, int count) | |||
169 | count=0; | 169 | count=0; |
170 | } | 170 | } |
171 | 171 | ||
172 | openfiles[fd].cacheoffset = 0; | 172 | openfiles[fd].cacheoffset = -1; |
173 | } | 173 | } |
174 | } | 174 | } |
175 | 175 | ||
@@ -195,6 +195,70 @@ int read(int fd, void* buf, int count) | |||
195 | return nread; | 195 | return nread; |
196 | } | 196 | } |
197 | 197 | ||
198 | int lseek(int fd, int offset, int whence) | ||
199 | { | ||
200 | int pos; | ||
201 | int newsector; | ||
202 | int oldsector; | ||
203 | int sectoroffset; | ||
204 | int rc; | ||
205 | |||
206 | if ( !openfiles[fd].busy ) { | ||
207 | errno = EBADF; | ||
208 | return -1; | ||
209 | } | ||
210 | |||
211 | switch ( whence ) { | ||
212 | case SEEK_SET: | ||
213 | pos = offset; | ||
214 | break; | ||
215 | |||
216 | case SEEK_CUR: | ||
217 | pos = openfiles[fd].fileoffset + offset; | ||
218 | break; | ||
219 | |||
220 | case SEEK_END: | ||
221 | pos = openfiles[fd].size - offset; | ||
222 | break; | ||
223 | |||
224 | default: | ||
225 | errno = EINVAL; | ||
226 | return -1; | ||
227 | } | ||
228 | if ( (pos < 0) || | ||
229 | (pos > openfiles[fd].size) ) { | ||
230 | errno = EINVAL; | ||
231 | return -1; | ||
232 | } | ||
233 | |||
234 | /* new sector? */ | ||
235 | newsector = pos / SECTOR_SIZE; | ||
236 | oldsector = openfiles[fd].fileoffset / SECTOR_SIZE; | ||
237 | sectoroffset = pos % SECTOR_SIZE; | ||
238 | |||
239 | if ( (newsector != oldsector) || | ||
240 | ((openfiles[fd].cacheoffset==-1) && sectoroffset) ) { | ||
241 | if ( newsector != oldsector ) { | ||
242 | rc = fat_seek(&(openfiles[fd].fatfile), newsector); | ||
243 | if ( rc < 0 ) { | ||
244 | errno = EIO; | ||
245 | return -1; | ||
246 | } | ||
247 | } | ||
248 | rc = fat_read(&(openfiles[fd].fatfile), 1, | ||
249 | &(openfiles[fd].cache)); | ||
250 | if ( rc < 0 ) { | ||
251 | errno = EIO; | ||
252 | return -1; | ||
253 | } | ||
254 | } | ||
255 | |||
256 | openfiles[fd].cacheoffset = sectoroffset; | ||
257 | openfiles[fd].fileoffset = pos; | ||
258 | |||
259 | return pos; | ||
260 | } | ||
261 | |||
198 | /* | 262 | /* |
199 | * local variables: | 263 | * local variables: |
200 | * eval: (load-file "../rockbox-mode.el") | 264 | * eval: (load-file "../rockbox-mode.el") |