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