X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libsrc%2Fcbm%2Fopendir.c;h=fe7fecb20a942e8fa2ff69c369267e9efef7fa11;hb=6a92d8b987ef98b97fced19533573c9e74b208a9;hp=cf8cfb631b645123680e65bff080c08583bf9adc;hpb=08b4ed1035a46d9b36dc10469100a38c97005287;p=cc65 diff --git a/libsrc/cbm/opendir.c b/libsrc/cbm/opendir.c index cf8cfb631..fe7fecb20 100644 --- a/libsrc/cbm/opendir.c +++ b/libsrc/cbm/opendir.c @@ -1,6 +1,9 @@ +/* + * Ullrich von Bassewitz, 2012-05-30. Based on code by Groepaz. + */ + #include #include -#include #include #include #include @@ -8,25 +11,35 @@ -DIR* __fastcall__ opendir (const char*) +DIR* __fastcall__ opendir (register const char* name) { - unsigned char buffer[8+16+1+7]; - int count; - DIR d; + unsigned char buf[2]; DIR* dir = 0; + DIR d; - /* Setup file name and offset */ + /* Setup the actual file name that is sent to the disk. We accept "0:", + * "1:" and "." as directory names. + */ d.name[0] = '$'; - d.name[1] = '\0'; - d.offs = 0; + if (name == 0 || name[0] == '\0' || (name[0] == '.' && name[1] == '\0')) { + d.name[1] = '\0'; + } else if ((name[0] == '0' || name[0] == '1') && name[1] == ':' && name[2] == '\0') { + d.name[1] = name[0]; + d.name[2] = '\0'; + } else { + errno = EINVAL; + goto exitpoint; + } + + /* Set the offset of the first entry */ + d.off = sizeof (buf); /* Open the directory on disk for reading */ d.fd = open (d.name, O_RDONLY); if (d.fd >= 0) { - /* Skip the disk header */ - count = read (d.fd, buffer, sizeof (buffer)); - if (count == sizeof (buffer)) { + /* Skip the load address */ + if (_dirread (&d, buf, sizeof (buf))) { /* Allocate memory for the DIR structure returned */ dir = malloc (sizeof (*dir)); @@ -38,12 +51,10 @@ DIR* __fastcall__ opendir (const char*) /* Set an appropriate error code */ errno = ENOMEM; } - } else if (count >= 0) { - /* Short read - need to set an error code */ - errno = EIO; } } +exitpoint: /* Done */ return dir; }