2012-06-01 19:23:34 +00:00
|
|
|
/*
|
2014-06-30 09:10:35 +00:00
|
|
|
** Ullrich von Bassewitz, 2012-05-30. Based on code by Groepaz.
|
|
|
|
*/
|
2012-06-01 19:23:34 +00:00
|
|
|
|
2012-05-30 19:37:57 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include "dir.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-03 15:48:32 +00:00
|
|
|
DIR* __fastcall__ opendir (register const char* name)
|
2012-06-03 14:32:15 +00:00
|
|
|
{
|
2012-07-30 19:01:45 +00:00
|
|
|
unsigned char buf[2];
|
2012-06-03 15:48:32 +00:00
|
|
|
DIR* dir = 0;
|
2012-06-07 13:31:32 +00:00
|
|
|
DIR d;
|
2012-05-30 19:37:57 +00:00
|
|
|
|
2012-06-03 15:48:32 +00:00
|
|
|
/* Setup the actual file name that is sent to the disk. We accept "0:",
|
2014-06-30 09:10:35 +00:00
|
|
|
** "1:" and "." as directory names.
|
|
|
|
*/
|
2012-05-30 19:37:57 +00:00
|
|
|
d.name[0] = '$';
|
2012-06-03 15:48:32 +00:00
|
|
|
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);
|
2012-05-30 19:37:57 +00:00
|
|
|
|
|
|
|
/* Open the directory on disk for reading */
|
|
|
|
d.fd = open (d.name, O_RDONLY);
|
|
|
|
if (d.fd >= 0) {
|
|
|
|
|
2012-07-30 19:02:07 +00:00
|
|
|
/* Skip the load address */
|
2012-07-30 19:01:45 +00:00
|
|
|
if (_dirread (&d, buf, sizeof (buf))) {
|
2012-05-30 19:37:57 +00:00
|
|
|
|
|
|
|
/* Allocate memory for the DIR structure returned */
|
|
|
|
dir = malloc (sizeof (*dir));
|
|
|
|
|
|
|
|
/* Copy the contents of d */
|
|
|
|
if (dir) {
|
|
|
|
memcpy (dir, &d, sizeof (d));
|
|
|
|
} else {
|
|
|
|
/* Set an appropriate error code */
|
|
|
|
errno = ENOMEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-03 15:48:32 +00:00
|
|
|
exitpoint:
|
2012-05-30 19:37:57 +00:00
|
|
|
/* Done */
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|