[CMAKE]
[reactos.git] / dll / win32 / kernel32 / file / curdir.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/kernel32/file/curdir.c
6 * PURPOSE: Current directory functions
7 * PROGRAMMER: Eric Kohl
8 * Filip Navara
9 * Steven Edwards
10 * Thomas Weidenmueller
11 * Gunnar Andre' Dalsnes
12 * UPDATE HISTORY:
13 * Created 30/09/98
14 */
15
16
17 /* INCLUDES ******************************************************************/
18
19 #include <k32.h>
20 #define NDEBUG
21 #include <debug.h>
22
23 #if DBG
24 static ULONG gDebugChannel = kernel32file;
25 #endif
26
27 /* GLOBAL VARIABLES **********************************************************/
28
29 UNICODE_STRING SystemDirectory;
30 UNICODE_STRING WindowsDirectory;
31
32
33 /* FUNCTIONS *****************************************************************/
34
35
36
37
38 /*
39 * @implemented
40 */
41 DWORD
42 WINAPI
43 GetCurrentDirectoryA (
44 DWORD nBufferLength,
45 LPSTR lpBuffer
46 )
47 {
48 WCHAR BufferW[MAX_PATH];
49 DWORD ret;
50
51 ret = GetCurrentDirectoryW(MAX_PATH, BufferW);
52
53 if (!ret) return 0;
54 if (ret > MAX_PATH)
55 {
56 SetLastError(ERROR_FILENAME_EXCED_RANGE);
57 return 0;
58 }
59
60 return FilenameW2A_FitOrFail(lpBuffer, nBufferLength, BufferW, ret+1);
61 }
62
63
64 /*
65 * @implemented
66 */
67 DWORD
68 WINAPI
69 GetCurrentDirectoryW (
70 DWORD nBufferLength,
71 LPWSTR lpBuffer
72 )
73 {
74 ULONG Length;
75
76 Length = RtlGetCurrentDirectory_U (nBufferLength * sizeof(WCHAR),
77 lpBuffer);
78
79 return (Length / sizeof (WCHAR));
80 }
81
82
83
84 /*
85 * @implemented
86 */
87 BOOL
88 WINAPI
89 SetCurrentDirectoryA (
90 LPCSTR lpPathName
91 )
92 {
93 PWCHAR PathNameW;
94
95 TRACE("setcurrdir: %s\n",lpPathName);
96
97 if (!(PathNameW = FilenameA2W(lpPathName, FALSE)))
98 return FALSE;
99
100 return SetCurrentDirectoryW(PathNameW);
101 }
102
103
104 /*
105 * @implemented
106 */
107 BOOL
108 WINAPI
109 SetCurrentDirectoryW (
110 LPCWSTR lpPathName
111 )
112 {
113 UNICODE_STRING UnicodeString;
114 NTSTATUS Status;
115
116 RtlInitUnicodeString (&UnicodeString,
117 lpPathName);
118
119 Status = RtlSetCurrentDirectory_U (&UnicodeString);
120 if (!NT_SUCCESS(Status))
121 {
122 SetLastErrorByStatus (Status);
123 return FALSE;
124 }
125
126 return TRUE;
127 }
128
129
130 /*
131 * @implemented
132 *
133 * NOTE: Windows returns a dos/short (8.3) path
134 */
135 DWORD
136 WINAPI
137 GetTempPathA (
138 DWORD nBufferLength,
139 LPSTR lpBuffer
140 )
141 {
142 WCHAR BufferW[MAX_PATH];
143 DWORD ret;
144
145 ret = GetTempPathW(MAX_PATH, BufferW);
146
147 if (!ret)
148 return 0;
149
150 if (ret > MAX_PATH)
151 {
152 SetLastError(ERROR_FILENAME_EXCED_RANGE);
153 return 0;
154 }
155
156 return FilenameW2A_FitOrFail(lpBuffer, nBufferLength, BufferW, ret+1);
157 }
158
159
160 /*
161 * @implemented
162 *
163 * ripped from wine
164 */
165 DWORD
166 WINAPI
167 GetTempPathW (
168 DWORD count,
169 LPWSTR path
170 )
171 {
172 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
173 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
174 static const WCHAR userprofile[] = { 'U','S','E','R','P','R','O','F','I','L','E',0 };
175 WCHAR tmp_path[MAX_PATH];
176 UINT ret;
177
178 TRACE("%u,%p\n", count, path);
179
180 if (!(ret = GetEnvironmentVariableW( tmp, tmp_path, MAX_PATH )) &&
181 !(ret = GetEnvironmentVariableW( temp, tmp_path, MAX_PATH )) &&
182 !(ret = GetEnvironmentVariableW( userprofile, tmp_path, MAX_PATH )) &&
183 !(ret = GetWindowsDirectoryW( tmp_path, MAX_PATH )))
184 return 0;
185
186 if (ret > MAX_PATH)
187 {
188 SetLastError(ERROR_FILENAME_EXCED_RANGE);
189 return 0;
190 }
191
192 ret = GetFullPathNameW(tmp_path, MAX_PATH, tmp_path, NULL);
193 if (!ret) return 0;
194
195 if (ret > MAX_PATH - 2)
196 {
197 SetLastError(ERROR_FILENAME_EXCED_RANGE);
198 return 0;
199 }
200
201 if (tmp_path[ret-1] != '\\')
202 {
203 tmp_path[ret++] = '\\';
204 tmp_path[ret] = '\0';
205 }
206
207 ret++; /* add space for terminating 0 */
208
209 if (count)
210 {
211 lstrcpynW(path, tmp_path, count);
212 if (count >= ret)
213 ret--; /* return length without 0 */
214 else if (count < 4)
215 path[0] = 0; /* avoid returning ambiguous "X:" */
216 }
217
218 TRACE("GetTempPathW returning %u, %S\n", ret, path);
219 return ret;
220
221 }
222
223
224 /*
225 * @implemented
226 */
227 UINT
228 WINAPI
229 GetSystemDirectoryA (
230 LPSTR lpBuffer,
231 UINT uSize
232 )
233 {
234 return FilenameU2A_FitOrFail(lpBuffer, uSize, &SystemDirectory);
235 }
236
237
238 /*
239 * @implemented
240 */
241 UINT
242 WINAPI
243 GetSystemDirectoryW (
244 LPWSTR lpBuffer,
245 UINT uSize
246 )
247 {
248 ULONG Length;
249
250 Length = SystemDirectory.Length / sizeof (WCHAR);
251
252 if (lpBuffer == NULL)
253 return Length + 1;
254
255 if (uSize > Length) {
256 memmove (lpBuffer,
257 SystemDirectory.Buffer,
258 SystemDirectory.Length);
259 lpBuffer[Length] = 0;
260
261 return Length; //good: ret chars excl. nullchar
262 }
263
264 return Length+1; //bad: ret space needed incl. nullchar
265 }
266
267 /*
268 * @implemented
269 */
270 UINT
271 WINAPI
272 GetWindowsDirectoryA (
273 LPSTR lpBuffer,
274 UINT uSize
275 )
276 {
277 return FilenameU2A_FitOrFail(lpBuffer, uSize, &WindowsDirectory);
278 }
279
280
281 /*
282 * @implemented
283 */
284 UINT
285 WINAPI
286 GetWindowsDirectoryW (
287 LPWSTR lpBuffer,
288 UINT uSize
289 )
290 {
291 ULONG Length;
292
293 Length = WindowsDirectory.Length / sizeof (WCHAR);
294
295 if (lpBuffer == NULL)
296 return Length + 1;
297
298 if (uSize > Length)
299 {
300 memmove (lpBuffer,
301 WindowsDirectory.Buffer,
302 WindowsDirectory.Length);
303 lpBuffer[Length] = 0;
304
305 return Length; //good: ret chars excl. nullchar
306 }
307
308 return Length+1; //bad: ret space needed incl. nullchar
309 }
310
311 /*
312 * @implemented
313 */
314 UINT
315 WINAPI
316 GetSystemWindowsDirectoryA(
317 LPSTR lpBuffer,
318 UINT uSize
319 )
320 {
321 return GetWindowsDirectoryA( lpBuffer, uSize );
322 }
323
324 /*
325 * @implemented
326 */
327 UINT
328 WINAPI
329 GetSystemWindowsDirectoryW(
330 LPWSTR lpBuffer,
331 UINT uSize
332 )
333 {
334 return GetWindowsDirectoryW( lpBuffer, uSize );
335 }
336
337 /*
338 * @unimplemented
339 */
340 UINT
341 WINAPI
342 GetSystemWow64DirectoryW(
343 LPWSTR lpBuffer,
344 UINT uSize
345 )
346 {
347 #ifdef _WIN64
348 ERR("GetSystemWow64DirectoryW is UNIMPLEMENTED!\n");
349 return 0;
350 #else
351 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
352 return 0;
353 #endif
354 }
355
356 /*
357 * @unimplemented
358 */
359 UINT
360 WINAPI
361 GetSystemWow64DirectoryA(
362 LPSTR lpBuffer,
363 UINT uSize
364 )
365 {
366 #ifdef _WIN64
367 WCHAR BufferW[MAX_PATH];
368 UINT ret;
369
370 ret = GetSystemWow64DirectoryW(BufferW, MAX_PATH);
371
372 if (!ret) return 0;
373 if (ret > MAX_PATH)
374 {
375 SetLastError(ERROR_FILENAME_EXCED_RANGE);
376 return 0;
377 }
378
379 return FilenameW2A_FitOrFail(lpBuffer, uSize, BufferW, ret+1);
380 #else
381 SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
382 return 0;
383 #endif
384 }
385
386 /* EOF */