From 680b8925a1c8c22dfd4776debbee66e4b7856fb6 Mon Sep 17 00:00:00 2001 From: skye Date: Sat, 10 Jun 2023 14:04:14 +0100 Subject: [PATCH] winapi: Implement all `direct.h` functions apart from path manipulation `fnmerge` and `fnsplit` haven't been implemented since I noticed inconsistencies in the documentation relating to them, for now I simply have empty function definitions in the form described by https://www.digitalmars.com/rtl/direct.html (ie no `_` prefix) The status of other functions is as follows: `_chdir`, `_chdrive`: impossible to implement, succeeds without an error code, or change of state in the Xbox `_getcwd`, `_getwd`: impossible to implement, return NULL pointers and sets `errno` to `-EINVAL` `_getdrive`: as before, impossible to implement - returns `0` and sets `errno` to `-EINVAL` (this is the error state described by a combo of MS public documentation and digital mars docs) `_searchpath`: impossible to implement since there is no PATH variable on the Xbox `_mkdir`, `_rmdir`: implemented, testing required, however --- lib/xboxrt/libc_extensions/direct.c | 111 ++++++++++++++++++++++++++++ lib/xboxrt/libc_extensions/direct.h | 10 +++ 2 files changed, 121 insertions(+) create mode 100644 lib/xboxrt/libc_extensions/direct.c diff --git a/lib/xboxrt/libc_extensions/direct.c b/lib/xboxrt/libc_extensions/direct.c new file mode 100644 index 000000000..96ae4ee19 --- /dev/null +++ b/lib/xboxrt/libc_extensions/direct.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: MIT + +// SPDX-FileCopyrightText: 2023 ExoSkye + +// Part of Microsoft CRT + +#include +#include +#include +#include + +// Made referencing https://www.digitalmars.com/rtl/direct.html, retrieved 2023-06-10, copyrighted 1999-2018 by Digital Mars + +#define MAXPATH 80 +#define MAXDRIVE 3 +#define MAXDIR 66 +#define MAXFILE 9 +#define MAXEXT 5 + +/* + * These silently succeed, since they wouldn't have any effect on the program at all + */ + +int _chdir(char* path) { + return 0; +} + +int _chdrive(int drive) { + return 0; +} + +/* + * The Xbox has no concept of current working directory, so these can't work + */ + +char* _getcwd(char* buffer, size_t length) { + errno = -EINVAL; + return NULL; +} + +char* _getwd(char* path_name) { + errno = -EINVAL; + return NULL; +} + +int _getdrive(void) { + errno = -EINVAL; + return 0; +} + +/* + * There's no path variable on the Xbox, so this can't work + */ + +char* _searchpath(const char* file) { + errno = -EINVAL; + return NULL; +} + +/* + * Below are the only things that can work + */ + +int _mkdir(const char* pathname) { + BOOL result = CreateDirectoryA( + pathname, + NULL + ); + + if (result == true) { + return 0; + } else { + DWORD err = GetLastError(); + + if (err == ERROR_ALREADY_EXISTS) { + errno = EACCES; + } else if (err == ERROR_PATH_NOT_FOUND) { + errno = ENOENT; + } + + return -1; + } +} + +int _rmdir(const char* pathname) { + BOOL result = RemoveDirectoryA( + pathname + ); + + if (result == true) { + return 0; + } else { + DWORD err = GetLastError(); + + if (err == ERROR_ALREADY_EXISTS) { + errno = EACCES; + } else if (err == ERROR_PATH_NOT_FOUND) { + errno = ENOENT; + } + + return -1; + } +} + +void fnmerge(char* path, const char* drive, const char* dir, const char* name, const char* ext) { + +} + +void fnsplit(const char* path, char* drive, char* dir, char* name, char* ext) { + +} \ No newline at end of file diff --git a/lib/xboxrt/libc_extensions/direct.h b/lib/xboxrt/libc_extensions/direct.h index e7244239a..529397574 100644 --- a/lib/xboxrt/libc_extensions/direct.h +++ b/lib/xboxrt/libc_extensions/direct.h @@ -3,3 +3,13 @@ // SPDX-FileCopyrightText: 2020 Jannik Vogel // Part of Microsoft CRT + +int _chdir(char* path); +int _chdrive(int drive); +char* _getcwd(char* buffer, size_t length); +char* _getwd(char* path_name); +int _getdrive(void); +int _mkdir(const char* pathname); +int _rmdir(const char* pathname); +void _fnmerge(char* path, const char* drive, const char* dir, const char* name, const char* ext); +char* _searchpath(const char* file); \ No newline at end of file