%% options copyright owner = Dirk Krause copyright year = 2015-xxxx SPDX-License-Identifier: BSD-3-Clause %% header /** @file dk4getcwd8.h Get current working directory as char string. */ #ifndef DK4CONF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4conf.h" #else #include #endif #endif #ifndef DK4ERROR_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4error.h" #else #include #endif #endif #ifdef __cplusplus extern "C" { #endif /** Find current working directory. @param dptr Destination buffer pointer. @param sz Buffer size. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if dptr is NULL or sz is 0, - DK4_E_SYSTEM
with errno in iDetails if getcwd() or _getcwd() failed or GetLastError() result in iDetails if GetCurrentDirectoryA() failed. */ int dk4getcwd_c8(char *dptr, size_t sz, dk4_er_t *erp); /** Find current working directory, return string in newly allocated memory. @param erp Error report, may be NULL. @return Valid pointer to newly allocated memory containing the directory name on success, NULL on error. On success, use dk4mem_free() to release the memory when done with it. Error codes: - DK4_E_SYSTEM
with errno in iDetails if getcwd() or _getcwd() failed or GetLastError() result in iDetails if GetCurrentDirectoryA() failed. - DK4_E_MATH_OVERFLOW
on mathematical overflow in size calculation, - DK4_E_MEMORY
if no memory is available. */ char * dk4getcwd_c8_dup(dk4_er_t *erp); #ifdef __cplusplus } #endif %% module #include "dk4conf.h" #include "dk4getcwd8.h" #if DK4_ON_WINDOWS #ifndef WINDOWS_H_INCLUDED #include #define WINDOWS_H_INCLUDED 1 #endif #endif #if DK4_HAVE_UNISTD_H #ifndef UNISTD_H_INCLUDED #include #define UNISTD_H_INCLUDED 1 #endif #endif #if DK4_HAVE_STDLIB_H #ifndef STDLIB_H_INCLUDED #include #define STDLIB_H_INCLUDED 1 #endif #endif #if DK4_HAVE_DIRECT_H #ifndef DIRECT_H_INCLUDED #include #define DIRECT_H_INCLUDED 1 #endif #endif #if DK4_HAVE_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_H_INCLUDED 1 #endif #endif #if DK4_HAVE_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif #ifndef DK4MEM_H_INCLUDED #include "dk4mem.h" #endif #ifndef DK4STR8_H_INCLUDED #include "dk4str8.h" #endif #ifndef DK4MPL_H_INCLUDED #include "dk4mpl.h" #endif $!trace-include int dk4getcwd_c8(char *dptr, size_t sz, dk4_er_t *erp) { int back = 0; #if DK4_USE_ASSERT assert(NULL != dptr); assert(0 < sz); #endif if ((NULL != dptr) && (0 < sz)) { *dptr = '\0'; } if ((NULL != dptr) && (0 < sz)) { #if DK4_ON_WINDOWS #if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT DWORD dwSz; DWORD dwRes; dwSz = (DWORD)sz; if ((dk4_um_t)sz > (dk4_um_t)0xFFFFFFFFUL) { dwSz = (DWORD)0xFFFFFFFFUL; } dwRes = GetCurrentDirectoryA(dwSz, dptr); if (0 < dwRes) { if (dwRes < dwSz) { back = 1; } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } else { dk4error_set_ldetails(erp, DK4_E_SYSTEM, GetLastError()); } #else #if DK4_HAVE__GETCWD int isz; errno = 0; isz = (int)sz; if((dk4_um_t)sz > (dk4_um_t)(INT_MAX)) { isz = INT_MAX; } if (NULL != _getcwd(dptr, isz)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_SYSTEM, errno); } #else #if DK4_HAVE_GETCWD errno = 0; if (NULL != getcwd(dptr, sz)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_SYSTEM, errno); } #else DWORD dwSz; DWORD dwRes; dwSz = (DWORD)sz; if ((dk4_um_t)sz > (dk4_um_t)0xFFFFFFUL) { dwSz = (DWORD)0xFFFFFFFFUL; } dwRes = GetCurrentDirectoryA(dwSz, dptr); if (0 < dwRes) { if (dwRes < dwSz) { back = 1; } else { dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL); } } else { dk4error_set_ldetails(erp, DK4_E_SYSTEM, GetLastError()); } #endif #endif #endif #else errno = 0; if (NULL != getcwd(dptr, sz)) { back = 1; } else { dk4error_set_idetails(erp, DK4_E_SYSTEM, errno); } #endif } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } char * dk4getcwd_c8_dup(dk4_er_t *erp) { char buf[DK4_MAX_PATH]; char *back = NULL; size_t szbuf = DK4_SIZEOF(buf,char); if (0 != dk4getcwd_c8(buf, szbuf, erp)) { back = dk4str8_dup(buf, erp); } return back; }