]> git.sur5r.net Git - cc65/blob - libsrc/cbm/opendir.c
goto.c warning fix for implicit truncation
[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 (register const char* name)
15 {
16     unsigned char buf[2];
17     DIR* dir = 0;
18     DIR d;
19
20     /* Setup the actual file name that is sent to the disk. We accept "0:",
21     ** "1:" and "." as directory names.
22     */
23     d.name[0] = '$';
24     if (name == 0 || name[0] == '\0' || (name[0] == '.' && name[1] == '\0')) {
25         d.name[1] = '\0';
26     } else if ((name[0] == '0' || name[0] == '1') && name[1] == ':' && name[2] == '\0') {
27         d.name[1] = name[0];
28         d.name[2] = '\0';
29     } else {
30         errno = EINVAL;
31         goto exitpoint;
32     }
33
34     /* Set the offset of the first entry */
35     d.off = sizeof (buf);
36
37     /* Open the directory on disk for reading */
38     d.fd = open (d.name, O_RDONLY);
39     if (d.fd >= 0) {
40
41         /* Skip the load address */         
42         if (_dirread (&d, buf, sizeof (buf))) {
43
44             /* Allocate memory for the DIR structure returned */
45             dir = malloc (sizeof (*dir));
46
47             /* Copy the contents of d */
48             if (dir) {
49                 memcpy (dir, &d, sizeof (d));
50             } else {
51                 /* Set an appropriate error code */
52                 errno = ENOMEM;
53             }
54         }
55     }
56
57 exitpoint:
58     /* Done */
59     return dir;
60 }
61
62
63