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