duct taped posix.dll (just like microsoft), new cursor, more info in the fault handlers, processes now have a parse-it-yourself cmdline, add SYS_WAIT_THREAD and SYS_GET_PROCESS_PID, writing still doesn't work
This commit is contained in:
1 parent
a37f5b1581
commit
b5c831f593
26 files changed
+1107
-56
No files matched your search
@@ -0,0 +1,78 @@
|
||||
// posix_dir.c - chdir, getcwd, opendir, readdir, closedir
|
||||
#include <string.h>
|
||||
#include "posix.h"
|
||||
#include "posix_path.h"
|
||||
|
||||
extern uint32_t SysGetCurrentPid(void);
|
||||
extern uint32_t SysSetCwd(const char* path);
|
||||
extern uint32_t SysGetCwd(char* buf, size_t size);
|
||||
extern uint32_t SysOpenDir(const char* path, uint32_t* handleOut);
|
||||
extern uint32_t SysReadDir(uint32_t handle, void* entryOut);
|
||||
extern void SysCloseHandle(uint32_t handle);
|
||||
extern void* SysAlloc(size_t);
|
||||
extern void SysFree(void*, size_t);
|
||||
|
||||
// KDIR_ENTRY layout mirrors io_file.h - duplicated here to avoid
|
||||
// pulling kernel headers into posix.dll
|
||||
#define KDIR_NAME_MAX 260
|
||||
typedef struct {
|
||||
char Name[KDIR_NAME_MAX];
|
||||
uint8_t Attr;
|
||||
uint32_t FileSize;
|
||||
} KDIR_ENTRY;
|
||||
|
||||
#define KATTR_DIR 0x10
|
||||
|
||||
int chdir(const char* path) {
|
||||
char native[512];
|
||||
PosixTranslatePath(path, native, sizeof(native));
|
||||
uint32_t s = SysSetCwd(native);
|
||||
if (s != 0) { errno = ENOENT; return -1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
char* getcwd(char* buf, size_t size) {
|
||||
if (!buf || size == 0) { errno = EINVAL; return (char*)0; }
|
||||
char native[512];
|
||||
uint32_t s = SysGetCwd(native, sizeof(native));
|
||||
if (s != 0) { errno = ERANGE; return (char*)0; }
|
||||
NativeToPosix(native, buf, size);
|
||||
return buf;
|
||||
}
|
||||
|
||||
DIR* opendir(const char* path) {
|
||||
char native[512];
|
||||
PosixTranslatePath(path, native, sizeof(native));
|
||||
uint32_t kh;
|
||||
uint32_t s = SysOpenDir(native, &kh);
|
||||
if (s != 0) { errno = ENOENT; return (DIR*)0; }
|
||||
|
||||
// Allocate DIR on the kernel heap (SysAlloc) so it outlives the stack.
|
||||
DIR* d = (DIR*)SysAlloc(sizeof(DIR));
|
||||
if (!d) { SysCloseHandle(kh); errno = ENOMEM; return (DIR*)0; }
|
||||
d->KHandle = kh;
|
||||
d->Valid = false;
|
||||
return d;
|
||||
}
|
||||
|
||||
struct dirent* readdir(DIR* d) {
|
||||
if (!d) { errno = EBADF; return (struct dirent*)0; }
|
||||
KDIR_ENTRY ke;
|
||||
uint32_t s = SysReadDir(d->KHandle, &ke);
|
||||
if (s != 0) return (struct dirent*)0; // end of directory or error
|
||||
|
||||
size_t len = strlen(ke.Name);
|
||||
if (len > NAME_MAX) len = NAME_MAX;
|
||||
memcpy(d->Current.d_name, ke.Name, len);
|
||||
d->Current.d_name[len] = '\0';
|
||||
d->Current.d_type = (ke.Attr & KATTR_DIR) ? DT_DIR : DT_REG;
|
||||
d->Valid = true;
|
||||
return &d->Current;
|
||||
}
|
||||
|
||||
int closedir(DIR* d) {
|
||||
if (!d) { errno = EBADF; return -1; }
|
||||
SysCloseHandle(d->KHandle);
|
||||
SysFree(d, sizeof(DIR));
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user