%% options copyright owner = Dirk Krause copyright year = 2015-xxxx SPDX-License-Identifier: BSD-3-Clause %% header /** @file dk4getcwdw.h Get working directroy in wchar_t. */ #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 (Number of wchar_t). @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_BUFFER_TOO_SMALL
if dptr is too small, - DK4_E_NOT_SUPPORTED
if the operation is not supported, - DK4_E_SYSTEM
with errno in iDetails if getcwd() or _getcwd() failed or GetLastError() result in iDetails if GetCurrentDirectoryW() failed. */ int dk4getcwd_wc(wchar_t *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_INVALID_ARGUMENTS
if src is a NULL pointer, - DK4_E_MATH_OVERFLOW
on mathematical overflow in size calculation, - DK4_E_MEMORY
if no memory is available. */ wchar_t * dk4getcwd_wc_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_ERRNO_H #ifndef ERRNO_H_INCLUDED #include #define ERRNO_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_ASSERT_H #ifndef ASSERT_H_INCLUDED #include #define ASSERT_H_INCLUDED 1 #endif #endif #ifndef DK4MEM_H_INCLUDED #include "dk4mem.h" #endif #ifndef DK4STRW_H_INCLUDED #include "dk4strw.h" #endif #ifndef DK4MPL_H_INCLUDED #include "dk4mpl.h" #endif #ifndef DK4GETCWDW_H_INCLUDED #include "dk4getcwdw.h" #endif $!trace-include int dk4getcwd_wc(wchar_t *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)) { #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 = GetCurrentDirectoryW(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__WGETCWD int isz; errno = 0; isz = (int)sz; if ((dk4_um_t)sz > (dk4_um_t)(INT_MAX)) { isz = INT_MAX; } if (NULL != _wgetcwd(dptr, isz)) { 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 = GetCurrentDirectoryW(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 #else dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED); #endif } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } return back; } wchar_t * dk4getcwd_wc_dup(dk4_er_t *erp) { wchar_t buf[DK4_MAX_PATH]; wchar_t *back = NULL; size_t szbuf = DK4_SIZEOF(buf,wchar_t); if (0 != dk4getcwd_wc(buf, szbuf, erp)) { back = dk4strw_dup(buf, erp); } return back; }