feature: enhance current working directory functions for improved error handling and memory management
This commit is contained in:
parent
9a7fca48bb
commit
815b03eeb7
3 changed files with 25 additions and 15 deletions
|
|
@ -509,12 +509,12 @@ DRANG_PLATFORM_API int drang_fs_closedir(drang_fs_directory_t *directory);
|
|||
* Retrieves the absolute path of the current working directory.
|
||||
* The path is null-terminated and stored in the provided buffer.
|
||||
*
|
||||
* @param[out] buffer Buffer to receive the current working directory path.
|
||||
* @param[in] size Size of the buffer in bytes.
|
||||
* @param[out] buffer Buffer to receive the current working directory path. (may be NULL)
|
||||
* @param[in] size Size of the buffer in bytes. (must be > 0 if buffer is not NULL)
|
||||
* @param[out] out_size Pointer to store the actual path length (may be NULL).
|
||||
* @return 0 on success, negative error code on failure.
|
||||
* @retval DRANG_EOK Success - current directory retrieved successfully.
|
||||
* @retval DRANG_EINVAL Invalid parameter (buffer is NULL or size is 0).
|
||||
* @retval DRANG_EINVAL Invalid parameter (buffer is NULL or size is not 0 or buffer is not NULL and size is 0).
|
||||
* @retval DRANG_ENOMEM Buffer too small to hold the path.
|
||||
* @retval DRANG_EIO I/O error occurred during path retrieval.
|
||||
* @remarks If out_size is provided, it contains the required buffer size on DRANG_ENOMEM.
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int drang_fs_mkdir(const char *path, drang_fs_permissions_t permissions)
|
||||
{
|
||||
|
|
@ -128,17 +129,26 @@ int drang_fs_closedir(drang_fs_directory_t *directory)
|
|||
int drang_fs_get_cwd(char *buffer, size_t size, size_t *out_size)
|
||||
{
|
||||
DRANG_BEGIN_TRY()
|
||||
DRANG_FAIL_IF_NULL(buffer, DRANG_EINVAL);
|
||||
if (getcwd(buffer, size) != NULL) {
|
||||
DRANG_RETURN_IN(out_size, strlen(buffer));
|
||||
}
|
||||
|
||||
if (errno == ERANGE) {
|
||||
if (out_size) {
|
||||
long required_size = pathconf(".", _PC_PATH_MAX);
|
||||
*out_size = required_size;
|
||||
DRANG_CHECK((buffer != NULL && size > 0) || (buffer == NULL && size == 0), DRANG_EINVAL);
|
||||
char* dir = get_current_dir_name();
|
||||
if (dir != NULL) {
|
||||
const size_t dir_length = strlen(dir);
|
||||
if (buffer != NULL && size < dir_length + 1) {
|
||||
free(dir);
|
||||
if (out_size) {
|
||||
*out_size = dir_length;
|
||||
}
|
||||
DRANG_FAIL(DRANG_ENOMEM);
|
||||
}
|
||||
DRANG_FAIL(DRANG_ENOMEM);
|
||||
if (buffer != NULL) {
|
||||
memcpy(buffer, dir, dir_length);
|
||||
buffer[dir_length] = '\0';
|
||||
}
|
||||
free(dir);
|
||||
if (out_size) {
|
||||
*out_size = dir_length + 1;
|
||||
}
|
||||
DRANG_RETURN();
|
||||
}
|
||||
|
||||
DRANG_FAIL(drang_errno_to_error(errno));
|
||||
|
|
@ -149,6 +159,7 @@ int drang_fs_get_cwd(char *buffer, size_t size, size_t *out_size)
|
|||
int drang_fs_set_cwd(const char *path)
|
||||
{
|
||||
DRANG_BEGIN_TRY()
|
||||
DRANG_CHECK(path != NULL, DRANG_EINVAL);
|
||||
|
||||
if (chdir(path) != 0) {
|
||||
DRANG_FAIL(drang_errno_to_error(errno));
|
||||
|
|
|
|||
|
|
@ -125,9 +125,8 @@ int drang_fs_get_cwd(char *buffer, size_t size, size_t *out_size)
|
|||
{
|
||||
DWORD length = 0;
|
||||
DRANG_BEGIN_TRY()
|
||||
DRANG_FAIL_IF_NULL(buffer, DRANG_EINVAL);
|
||||
|
||||
length = GetCurrentDirectoryA((DWORD) size, buffer);
|
||||
length = GetCurrentDirectoryA(buffer ? (DWORD) size : 0, buffer);
|
||||
if (length == 0) {
|
||||
DRANG_FAIL(win32_error_to_drang(GetLastError()));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue