filesystem: Added SDL_GetCurrentDirectory().

Fixes #11531.
This commit is contained in:
Ryan C. Gordon
2024-11-27 19:41:37 -05:00
parent 16113374ff
commit f852038384
10 changed files with 101 additions and 2 deletions

View File

@@ -33,6 +33,7 @@
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
bool SDL_SYS_EnumerateDirectory(const char *path, const char *dirname, SDL_EnumerateDirectoryCallback cb, void *userdata)
{
@@ -185,5 +186,36 @@ bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info)
return true;
}
// Note that this isn't actually part of filesystem, not fsops, but everything that uses posix fsops uses this implementation, even with separate filesystem code.
char *SDL_SYS_GetCurrentDirectory(void)
{
size_t buflen = 64;
char *buf = NULL;
while (true) {
void *ptr = SDL_realloc(buf, buflen);
if (!ptr) {
SDL_free(buf);
return NULL;
}
buf = (char *) ptr;
if (getcwd(buf, buflen) != NULL) {
break; // we got it!
}
if (errno == ERANGE) {
buflen *= 2; // try again with a bigger buffer.
continue;
}
SDL_free(buf);
SDL_SetError("getcwd failed: %s", strerror(errno));
return NULL;
}
return buf;
}
#endif // SDL_FSOPS_POSIX