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
108 changes: 108 additions & 0 deletions lib/xboxrt/libc_extensions/direct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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
JayFoxRox marked this conversation as resolved.
Show resolved Hide resolved

/*
* These silently succeed, since they wouldn't have any effect on the program at all
*/

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

int _chdrive(int drive) {
return 0;
}

/*
* The Xbox has no concept of current working directory, so these can't work
ExoSkye marked this conversation as resolved.
Show resolved Hide resolved
*/

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
ExoSkye marked this conversation as resolved.
Show resolved Hide resolved
*/

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

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

}
ExoSkye marked this conversation as resolved.
Show resolved Hide resolved
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);
ExoSkye marked this conversation as resolved.
Show resolved Hide resolved