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