diff --git a/f256lib/file.c b/f256lib/file.c index 26568ae..4ed5de3 100644 --- a/f256lib/file.c +++ b/f256lib/file.c @@ -32,7 +32,7 @@ static char _dirStream[MAX_DRIVES]; -static bool findName(const char *name, int16_t *offset); +static bool findName(char *name, int16_t *offset); static char getIn(void); // This isn't the best place for this. static int16_t kernelRead(uint8_t fd, void *buf, uint16_t nbytes); static int16_t kernelWrite(uint8_t fd, void *buf, uint16_t nbytes); @@ -41,10 +41,12 @@ static char *pathWithoutDrive(char *path, char *drive); #pragma push_macro("close") #undef close -void fileClose(uint8_t fd) { - kernelArgs->file.close.stream = fd; +void fileClose(uint8_t *fd) { + kernelArgs->file.close.stream = *fd; kernelCall(File.Close); + free(fd); + for (;;) { kernelNextEvent(); switch (kernelEventData.type) { @@ -83,9 +85,10 @@ int16_t fileCloseDir(fileDirT *dir) { #pragma pop_macro("close") -int16_t fileOpen(char *fname, char *mode) { +int16_t *fileOpen(char *fname, char *mode) { int16_t ret = 0; uint8_t m = 0; // Default to READ. + int16_t *fd; char drive; char *c; @@ -103,16 +106,18 @@ int16_t fileOpen(char *fname, char *mode) { kernelArgs->file.open.drive = drive; kernelArgs->file.open.mode = m; ret = kernelCall(File.Open); - if (kernelError) return -1; + if (kernelError) return NULL; for (;;) { kernelNextEvent(); switch (kernelEventData.type) { case kernelEvent(file.OPENED): - return ret; + fd = (int16_t *)malloc(sizeof(int16_t)); + *fd = ret; + return fd; case kernelEvent(file.NOT_FOUND): case kernelEvent(file.ERROR): - return -1; + return NULL; default: continue; } @@ -150,14 +155,14 @@ fileDirT *fileOpenDir(char *name) { } -int16_t fileRead(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd) { +int16_t fileRead(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd) { char *data = (char *)buf; int16_t gathered = 0; int16_t returned; uint16_t bytes = nbytes * nmemb; while (gathered < bytes) { - returned = kernelRead(fd, data + gathered, bytes - gathered); + returned = kernelRead(*fd, data + gathered, bytes - gathered); if (returned <= 0) break; gathered += returned; } @@ -279,10 +284,12 @@ void fileReset(void) { } -int16_t fileSeek(uint8_t id, uint32_t offset, uint8_t whence) { +int16_t fileSeek(uint8_t *fd, uint32_t offset, uint8_t whence) { if (whence != SEEK_SET) return -1; - kernelArgs->file.seek.stream = id; + //***TODO*** We should do this manually for IEC devices. + + kernelArgs->file.seek.stream = *fd; kernelArgs->file.seek.offset = offset; kernelCall(File.Seek); if (kernelError) return -1; @@ -312,22 +319,12 @@ int16_t fileUnlink(char *name) { } -int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd) { +int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd) { uint8_t *data = (uint8_t *)buf; int16_t total = 0; int16_t bytes = nbytes * nmemb; uint8_t writing; int16_t written; - int16_t i; - char *text; - - if (fd == 1) { - text = (char *)buf; - for (i = 0; i < bytes; i++) { - __putchar(text[i]); - } - return i; - } while (bytes) { @@ -337,7 +334,7 @@ int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd) { writing = bytes; } - written = kernelWrite(fd, data + total, writing); + written = kernelWrite(*fd, data + total, writing); if (written <= 0) { return -1; } @@ -350,7 +347,7 @@ int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd) { } -static bool findName(const char *name, int16_t *offset) { +static bool findName(char *name, int16_t *offset) { int16_t i; int16_t pos; @@ -389,8 +386,8 @@ static char getIn(void) { #undef EOF static int16_t kernelRead(uint8_t fd, void *buf, uint16_t nbytes) { + // STDIN if (fd == 0) { - // stdin *(char *)buf = getIn(); return 1; } @@ -424,6 +421,18 @@ static int16_t kernelRead(uint8_t fd, void *buf, uint16_t nbytes) { static int16_t kernelWrite(uint8_t fd, void *buf, uint16_t nbytes) { + int16_t i; + char *text; + + // STDOUT + if (fd == 1) { + text = (char *)buf; + for (i = 0; i < nbytes; i++) { + __putchar(text[i]); + } + return i; + } + kernelArgs->file.read.stream = fd; kernelArgs->common.buf = buf; kernelArgs->common.buflen = nbytes; diff --git a/f256lib/file.h b/f256lib/file.h index 5e5108e..bc090c5 100644 --- a/f256lib/file.h +++ b/f256lib/file.h @@ -45,17 +45,17 @@ typedef struct fileDirEntS { } fileDirEntT; -void fileClose(uint8_t fd); +void fileClose(uint8_t *fd); int16_t fileCloseDir(fileDirT *dir); -int16_t fileOpen(char *fname, char *mode); +int16_t *fileOpen(char *fname, char *mode); fileDirT *fileOpenDir(char *name); -int16_t fileRead(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd); +int16_t fileRead(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd); fileDirEntT *fileReadDir(fileDirT *dir); int16_t fileRename(char *name, char *to); void fileReset(void); -int16_t fileSeek(uint8_t id, uint32_t offset, uint8_t whence); +int16_t fileSeek(uint8_t *fd, uint32_t offset, uint8_t whence); int16_t fileUnlink(char *name); -int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd); +int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t *fd); #define _DE_ISREG(t) (t == 0) @@ -80,6 +80,8 @@ int16_t fileWrite(void *buf, uint16_t nbytes, uint16_t nmemb, uint8_t fd); #define rename fileRename #define rewind(s) (void)fileSeek(s, 0, SEEK_SET) #define SEEK_SET 0 +#define STDIN 0 +#define STDOUT 1 #define unlink fileUnlink #endif