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