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