Skip to content

Commit

Permalink
winapi: Implement all direct.h functions apart from path manipulation
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
skye committed Jun 10, 2023
1 parent 500354e commit 680b892
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
111 changes: 111 additions & 0 deletions lib/xboxrt/libc_extensions/direct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// SPDX-License-Identifier: MIT

// SPDX-FileCopyrightText: 2023 ExoSkye

// Part of Microsoft CRT

#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <windows.h>

// 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) {

}
10 changes: 10 additions & 0 deletions lib/xboxrt/libc_extensions/direct.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 680b892

Please sign in to comment.