Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement direct.h functions #636

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/xboxrt/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
XBOXRT_SRCS := \
$(NXDK_DIR)/lib/xboxrt/libc_extensions/stat.c \
$(NXDK_DIR)/lib/xboxrt/libc_extensions/direct.c \
$(NXDK_DIR)/lib/xboxrt/libc_extensions/strings.c \
$(NXDK_DIR)/lib/xboxrt/libc_extensions/wchar.c \
$(NXDK_DIR)/lib/xboxrt/libc_extensions/wchar_ext_.c \
Expand Down
85 changes: 85 additions & 0 deletions lib/xboxrt/libc_extensions/direct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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


/*
* TODO: Make a working directory system?
*/

int _chdir(char* path) {
Ryzee119 marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

int _chdrive(int drive) {
return 0;
}

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;
}

/*
* 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) {
Copy link
Contributor

@Ryzee119 Ryzee119 Jun 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's alot of cases that are not covered from GetLastError(). ERROR_DISK_FULL or ERROR_FILENAME_EXCED_RANGE is just an obvious example.

Although these will fail, errno will not be set. Not sure if thats a problem. Comment applies to rmdir too.

errno = EACCES;
} else if (err == ERROR_PATH_NOT_FOUND) {
errno = ENOENT;
}

return -1;
}
}

int _rmdir(const char* pathname) {
Ryzee119 marked this conversation as resolved.
Show resolved Hide resolved
BOOL result = RemoveDirectoryA(
pathname
);

if (result == true) {
return 0;
} else {
DWORD err = GetLastError();

if (err == ERROR_ALREADY_EXISTS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is ERROR_ALREADY_EXISTS a valid error code for RemoveDirectoryA?

errno = EACCES;
} else if (err == ERROR_PATH_NOT_FOUND) {
errno = ENOENT;
}

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