35 lines
1.0 KiB
C
35 lines
1.0 KiB
C
#pragma once
|
|
// posix_fd.h - file descriptor table (internal to posix.dll)
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
typedef enum {
|
|
FD_FREE,
|
|
FD_FILE,
|
|
FD_CONSOLE_R, // fd 0 - reads from keyboard via SysKbReadChar
|
|
FD_CONSOLE_W, // fd 1, 2 - writes via SysWriteConsole
|
|
FD_DEVNULL, // /dev/null
|
|
FD_DEVZERO, // /dev/zero
|
|
} FD_TYPE;
|
|
|
|
typedef struct {
|
|
FD_TYPE Type;
|
|
uint32_t KHandle; // kernel handle; 0 for console/devnull/devzero
|
|
bool Used;
|
|
} FD_ENTRY;
|
|
|
|
#define FD_MAX 64
|
|
#define FD_TABLE_PIDS 32 // circular, indexed by pid % FD_TABLE_PIDS
|
|
|
|
// Called from _start before main(); sets up fds 0/1/2 for this process.
|
|
void PosixFdInit(void);
|
|
|
|
// Allocate the lowest free fd >= 3. Returns -1 and sets errno=EMFILE on failure.
|
|
int FdAlloc(uint32_t pid, FD_TYPE type, uint32_t khandle);
|
|
|
|
// Look up an fd. Returns NULL and sets errno=EBADF if invalid.
|
|
FD_ENTRY* FdGet(uint32_t pid, int fd);
|
|
|
|
// Release an fd slot (does NOT close the kernel handle).
|
|
void FdFree(uint32_t pid, int fd);
|