]> git.sur5r.net Git - cc65/blob - libsrc/cbm/opendir.c
569c4918a34c975446af369262cf69e4b7d26706
[cc65] / libsrc / cbm / opendir.c
1 /*
2  * Ullrich von Bassewitz, 2012-05-30. Based on code by Groepaz.
3  */
4
5 #include <stdlib.h>
6 #include <string.h>
7 #include <dirent.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include "dir.h"
12
13
14
15 DIR* __fastcall__ opendir (const char*)
16 {                           
17     unsigned char buf[32];
18     DIR* dir = 0;
19     DIR d;
20
21     /* Setup file name and offset */
22     d.name[0] = '$';
23     d.name[1] = '\0';
24     d.off     = 0;
25
26     /* Open the directory on disk for reading */
27     d.fd = open (d.name, O_RDONLY);
28     if (d.fd >= 0) {
29
30         /* Skip the disk header */
31         if (_dirread (&d, buf, 32)) {
32
33             /* Allocate memory for the DIR structure returned */
34             dir = malloc (sizeof (*dir));
35
36             /* Copy the contents of d */
37             if (dir) {
38                 memcpy (dir, &d, sizeof (d));
39             } else {
40                 /* Set an appropriate error code */
41                 errno = ENOMEM;
42             }
43         }
44     }
45
46     /* Done */
47     return dir;
48 }
49
50
51