]> git.sur5r.net Git - cc65/blob - libsrc/cbm/seekdir.c
Used a library-reference method to calibrate lightpen drivers.
[cc65] / libsrc / cbm / seekdir.c
1 /*
2  * Ullrich von Bassewitz, 2012-06-03. Based on code by Groepaz.
3  */
4
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include "dir.h"
9
10
11
12 void __fastcall__ seekdir (register DIR* dir, long offs)
13 {
14     unsigned      o;
15     unsigned char count;
16     unsigned char buf[128];
17
18     /* Make sure we have a reasonable value for offs */
19     if (offs > 0x1000) {
20         errno = EINVAL;
21         return;
22     }
23
24     /* Close the directory file descriptor */
25     close (dir->fd);
26
27     /* Reopen it using the old name */
28     dir->fd = open (dir->name, O_RDONLY);
29     if (dir->fd < 0) {
30         /* Oops! */
31         return;
32     }
33
34     /* Skip until we've reached the target offset in the directory */
35     o = dir->off = offs;
36     while (o) {
37
38         /* Determine size of next chunk to read */
39         if (o > sizeof (buf)) {  
40             count = sizeof (buf);
41             o -= sizeof (buf);
42         } else {
43             count = offs;
44             o = 0;
45         }
46
47         /* Skip */
48         if (!_dirread (dir, buf, count)) {
49             return;
50         }
51     }
52 }
53
54
55