1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5
6 /*
7 WARNING: This file was generated by the dkct program (see
8 http://dktools.sourceforge.net/ for details).
9 Changes you make here will be lost if dkct is run again!
10 You should modify the original source and run dkct on it.
11 Original source: dk4getcwd8.ctr
12 */
13
14 /** @file dk4getcwd8.c The dk4getcwd8 module.
15 */
16
17
18 #include "dk4conf.h"
19 #include <libdk4c/dk4getcwd8.h>
20
21 #if DK4_ON_WINDOWS
22 #ifndef WINDOWS_H_INCLUDED
23 #include <windows.h>
24 #define WINDOWS_H_INCLUDED 1
25 #endif
26 #endif
27
28 #if DK4_HAVE_UNISTD_H
29 #ifndef UNISTD_H_INCLUDED
30 #include <unistd.h>
31 #define UNISTD_H_INCLUDED 1
32 #endif
33 #endif
34
35 #if DK4_HAVE_STDLIB_H
36 #ifndef STDLIB_H_INCLUDED
37 #include <stdlib.h>
38 #define STDLIB_H_INCLUDED 1
39 #endif
40 #endif
41
42 #if DK4_HAVE_DIRECT_H
43 #ifndef DIRECT_H_INCLUDED
44 #include <direct.h>
45 #define DIRECT_H_INCLUDED 1
46 #endif
47 #endif
48
49 #if DK4_HAVE_ERRNO_H
50 #ifndef ERRNO_H_INCLUDED
51 #include <errno.h>
52 #define ERRNO_H_INCLUDED 1
53 #endif
54 #endif
55
56 #if DK4_HAVE_ASSERT_H
57 #ifndef ASSERT_H_INCLUDED
58 #include <assert.h>
59 #define ASSERT_H_INCLUDED 1
60 #endif
61 #endif
62
63 #ifndef DK4MEM_H_INCLUDED
64 #include <libdk4base/dk4mem.h>
65 #endif
66
67 #ifndef DK4STR8_H_INCLUDED
68 #include <libdk4base/dk4str8.h>
69 #endif
70
71 #ifndef DK4MPL_H_INCLUDED
72 #include <libdk4base/dk4mpl.h>
73 #endif
74
75
76
77
78
79
80
81 int
dk4getcwd_c8(char * dptr,size_t sz,dk4_er_t * erp)82 dk4getcwd_c8(char *dptr, size_t sz, dk4_er_t *erp)
83 {
84 int back = 0;
85 #if DK4_USE_ASSERT
86 assert(NULL != dptr);
87 assert(0 < sz);
88 #endif
89 if ((NULL != dptr) && (0 < sz)) { *dptr = '\0'; }
90 if ((NULL != dptr) && (0 < sz)) {
91 #if DK4_ON_WINDOWS
92 #if DK4_WIN_AVOID_CRT || DK4_WIN_DENY_CRT
93 DWORD dwSz;
94 DWORD dwRes;
95 dwSz = (DWORD)sz;
96 if ((dk4_um_t)sz > (dk4_um_t)0xFFFFFFFFUL) { dwSz = (DWORD)0xFFFFFFFFUL; }
97 dwRes = GetCurrentDirectoryA(dwSz, dptr);
98 if (0 < dwRes) {
99 if (dwRes < dwSz) {
100 back = 1;
101 } else {
102 dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
103 }
104 } else {
105 dk4error_set_ldetails(erp, DK4_E_SYSTEM, GetLastError());
106 }
107 #else
108 #if DK4_HAVE__GETCWD
109 int isz;
110 errno = 0;
111 isz = (int)sz;
112 if((dk4_um_t)sz > (dk4_um_t)(INT_MAX)) { isz = INT_MAX; }
113 if (NULL != _getcwd(dptr, isz)) {
114 back = 1;
115 } else {
116 dk4error_set_idetails(erp, DK4_E_SYSTEM, errno);
117 }
118 #else
119 #if DK4_HAVE_GETCWD
120 errno = 0;
121 if (NULL != getcwd(dptr, sz)) {
122 back = 1;
123 } else {
124 dk4error_set_idetails(erp, DK4_E_SYSTEM, errno);
125 }
126 #else
127 DWORD dwSz;
128 DWORD dwRes;
129 dwSz = (DWORD)sz;
130 if ((dk4_um_t)sz > (dk4_um_t)0xFFFFFFUL) { dwSz = (DWORD)0xFFFFFFFFUL; }
131 dwRes = GetCurrentDirectoryA(dwSz, dptr);
132 if (0 < dwRes) {
133 if (dwRes < dwSz) {
134 back = 1;
135 } else {
136 dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
137 }
138 } else {
139 dk4error_set_ldetails(erp, DK4_E_SYSTEM, GetLastError());
140 }
141 #endif
142 #endif
143 #endif
144 #else
145 errno = 0;
146 if (NULL != getcwd(dptr, sz)) {
147 back = 1;
148 } else {
149 dk4error_set_idetails(erp, DK4_E_SYSTEM, errno);
150 }
151 #endif
152 } else {
153 dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
154 }
155 return back;
156 }
157
158
159
160 char *
dk4getcwd_c8_dup(dk4_er_t * erp)161 dk4getcwd_c8_dup(dk4_er_t *erp)
162 {
163 char buf[DK4_MAX_PATH];
164 char *back = NULL;
165 size_t szbuf = DK4_SIZEOF(buf,char);
166
167 if (0 != dk4getcwd_c8(buf, szbuf, erp)) {
168 back = dk4str8_dup(buf, erp);
169 }
170 return back;
171 }
172
173