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