update for HEAD-2003091401
[reactos.git] / 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 <kernel32/kernel32.h>
18
19
20 /* GLOBAL VARIABLES **********************************************************/
21
22 UNICODE_STRING SystemDirectory;
23 UNICODE_STRING WindowsDirectory;
24
25
26 /* FUNCTIONS *****************************************************************/
27
28 /*
29  * @implemented
30  */
31 DWORD
32 STDCALL
33 GetCurrentDirectoryA (
34         DWORD   nBufferLength,
35         LPSTR   lpBuffer
36         )
37 {
38         ANSI_STRING AnsiString;
39         UNICODE_STRING UnicodeString;
40
41         /* initialize ansi string */
42         AnsiString.Length = 0;
43         AnsiString.MaximumLength = nBufferLength;
44         AnsiString.Buffer = lpBuffer;
45
46         /* allocate buffer for unicode string */
47         UnicodeString.Length = 0;
48         UnicodeString.MaximumLength = nBufferLength * sizeof(WCHAR);
49         UnicodeString.Buffer = RtlAllocateHeap (RtlGetProcessHeap (),
50                                                 0,
51                                                 UnicodeString.MaximumLength);
52
53         /* get current directory */
54         UnicodeString.Length = RtlGetCurrentDirectory_U (UnicodeString.MaximumLength,
55                                                          UnicodeString.Buffer);
56         DPRINT("UnicodeString.Buffer %S\n", UnicodeString.Buffer);
57
58         /* convert unicode string to ansi (or oem) */
59         if (bIsFileApiAnsi)
60                 RtlUnicodeStringToAnsiString (&AnsiString,
61                                               &UnicodeString,
62                                               FALSE);
63         else
64                 RtlUnicodeStringToOemString (&AnsiString,
65                                              &UnicodeString,
66                                              FALSE);
67         DPRINT("AnsiString.Buffer %s\n", AnsiString.Buffer);
68
69         /* free unicode string */
70         RtlFreeHeap (RtlGetProcessHeap (),
71                      0,
72                      UnicodeString.Buffer);
73
74         return AnsiString.Length;
75 }
76
77
78 /*
79  * @implemented
80  */
81 DWORD
82 STDCALL
83 GetCurrentDirectoryW (
84         DWORD   nBufferLength,
85         LPWSTR  lpBuffer
86         )
87 {
88         ULONG Length;
89
90         Length = RtlGetCurrentDirectory_U (nBufferLength * sizeof(WCHAR),
91                                            lpBuffer);
92
93         return (Length / sizeof (WCHAR));
94 }
95
96
97 /*
98  * @implemented
99  */
100 WINBOOL
101 STDCALL
102 SetCurrentDirectoryA (
103         LPCSTR  lpPathName
104         )
105 {
106         ANSI_STRING AnsiString;
107         UNICODE_STRING UnicodeString;
108         NTSTATUS Status;
109
110         RtlInitAnsiString (&AnsiString,
111                            (LPSTR)lpPathName);
112
113         /* convert ansi (or oem) to unicode */
114         if (bIsFileApiAnsi)
115                 RtlAnsiStringToUnicodeString (&UnicodeString,
116                                               &AnsiString,
117                                               TRUE);
118         else
119                 RtlOemStringToUnicodeString (&UnicodeString,
120                                              &AnsiString,
121                                              TRUE);
122
123         Status = RtlSetCurrentDirectory_U (&UnicodeString);
124
125         RtlFreeUnicodeString (&UnicodeString);
126
127         if (!NT_SUCCESS(Status))
128         {
129                 SetLastErrorByStatus (Status);
130                 return FALSE;
131         }
132
133         return TRUE;
134 }
135
136
137 /*
138  * @implemented
139  */
140 WINBOOL
141 STDCALL
142 SetCurrentDirectoryW (
143         LPCWSTR lpPathName
144         )
145 {
146         UNICODE_STRING UnicodeString;
147         NTSTATUS Status;
148
149         RtlInitUnicodeString (&UnicodeString,
150                               lpPathName);
151
152         Status = RtlSetCurrentDirectory_U (&UnicodeString);
153         if (!NT_SUCCESS(Status))
154         {
155                 SetLastErrorByStatus (Status);
156                 return FALSE;
157         }
158
159         return TRUE;
160 }
161
162
163 /*
164  * @implemented
165  */
166 DWORD
167 STDCALL
168 GetTempPathA (
169         DWORD   nBufferLength,
170         LPSTR   lpBuffer
171         )
172 {
173         UNICODE_STRING UnicodeString;
174         ANSI_STRING AnsiString;
175
176         AnsiString.Length = 0;
177         AnsiString.MaximumLength = nBufferLength;
178         AnsiString.Buffer = lpBuffer;
179
180         /* initialize allocate unicode string */
181         UnicodeString.Length = 0;
182         UnicodeString.MaximumLength = nBufferLength * sizeof(WCHAR);
183         UnicodeString.Buffer = RtlAllocateHeap (RtlGetProcessHeap(),
184                                                 0,
185                                                 UnicodeString.MaximumLength);
186
187         UnicodeString.Length = GetTempPathW (nBufferLength,
188                                              UnicodeString.Buffer) * sizeof(WCHAR);
189
190         /* convert unicode string to ansi (or oem) */
191         if (bIsFileApiAnsi)
192                 RtlUnicodeStringToAnsiString (&AnsiString,
193                                               &UnicodeString,
194                                               FALSE);
195         else
196                 RtlUnicodeStringToOemString (&AnsiString,
197                                              &UnicodeString,
198                                              FALSE);
199
200         /* free unicode string buffer */
201         RtlFreeHeap (RtlGetProcessHeap (),
202                      0,
203                      UnicodeString.Buffer);
204
205         return AnsiString.Length;
206 }
207
208
209 /*
210  * @implemented
211  */
212 DWORD
213 STDCALL
214 GetTempPathW (
215         DWORD   nBufferLength,
216         LPWSTR  lpBuffer
217         )
218 {
219         UNICODE_STRING Name;
220         UNICODE_STRING Value;
221         NTSTATUS Status;
222
223         Value.Length = 0;
224         Value.MaximumLength = (nBufferLength - 1) * sizeof(WCHAR);
225         Value.Buffer = lpBuffer;
226
227         RtlInitUnicodeStringFromLiteral (&Name,
228                               L"TMP");
229
230         Status = RtlQueryEnvironmentVariable_U (NULL,
231                                                 &Name,
232                                                 &Value);
233         if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
234         {
235                 RtlInitUnicodeStringFromLiteral (&Name,
236                                       L"TEMP");
237
238                 Status = RtlQueryEnvironmentVariable_U (NULL,
239                                                         &Name,
240                                                         &Value);
241
242                 if (!NT_SUCCESS(Status) && Status != STATUS_BUFFER_TOO_SMALL)
243                 {
244                         Value.Length = RtlGetCurrentDirectory_U (Value.MaximumLength,
245                                                                  Value.Buffer);
246                 }
247         }
248
249         if (NT_SUCCESS(Status))
250         {
251                 lpBuffer[Value.Length / sizeof(WCHAR)] = L'\\';
252                 lpBuffer[Value.Length / sizeof(WCHAR) + 1] = 0;
253         }
254
255         return Value.Length / sizeof(WCHAR) + 1;
256 }
257
258
259 /*
260  * @implemented
261  */
262 UINT
263 STDCALL
264 GetSystemDirectoryA (
265         LPSTR   lpBuffer,
266         UINT    uSize
267         )
268 {
269         ANSI_STRING String;
270         ULONG Length;
271         NTSTATUS Status;
272
273         if (lpBuffer == NULL)
274                 return 0;
275
276         Length = RtlUnicodeStringToAnsiSize (&SystemDirectory);   //len of ansi str incl. nullchar
277
278         if (uSize >= Length){
279                 String.Length = 0;
280                 String.MaximumLength = uSize;
281                 String.Buffer = lpBuffer;
282
283                 /* convert unicode string to ansi (or oem) */
284                 if (bIsFileApiAnsi)
285                         Status = RtlUnicodeStringToAnsiString (&String,
286                                                       &SystemDirectory,
287                                                       FALSE);
288                 else
289                         Status = RtlUnicodeStringToOemString (&String,
290                                                      &SystemDirectory,
291                                                      FALSE);
292                 if (!NT_SUCCESS(Status) )
293                         return 0;
294
295                 return Length-1;  //good: ret chars excl. nullchar
296
297         }
298
299         return Length;   //bad: ret space needed incl. nullchar
300 }
301
302
303 /*
304  * @implemented
305  */
306 UINT
307 STDCALL
308 GetSystemDirectoryW (
309         LPWSTR  lpBuffer,
310         UINT    uSize
311         )
312 {
313         ULONG Length;
314
315         if (lpBuffer == NULL)
316                 return 0;
317
318         Length = SystemDirectory.Length / sizeof (WCHAR);
319         if (uSize > Length)     {
320                 memmove (lpBuffer,
321                          SystemDirectory.Buffer,
322                          SystemDirectory.Length);
323                 lpBuffer[Length] = 0;
324
325                 return Length;    //good: ret chars excl. nullchar
326         }
327
328         return Length+1;         //bad: ret space needed incl. nullchar
329 }
330
331 /*
332  * @implemented
333  */
334 UINT
335 STDCALL
336 GetWindowsDirectoryA (
337         LPSTR   lpBuffer,
338         UINT    uSize
339         )
340 {
341         ANSI_STRING String;
342         ULONG Length;
343         NTSTATUS Status;
344
345         if (lpBuffer == NULL)
346                 return 0;
347
348         Length = RtlUnicodeStringToAnsiSize (&WindowsDirectory); //len of ansi str incl. nullchar
349         
350         if (uSize >= Length){
351
352                 String.Length = 0;
353                 String.MaximumLength = uSize;
354                 String.Buffer = lpBuffer;
355
356                 /* convert unicode string to ansi (or oem) */
357                 if (bIsFileApiAnsi)
358                         Status = RtlUnicodeStringToAnsiString (&String,
359                                                       &WindowsDirectory,
360                                                       FALSE);
361                 else
362                         Status = RtlUnicodeStringToOemString (&String,
363                                                      &WindowsDirectory,
364                                                      FALSE);
365
366                 if (!NT_SUCCESS(Status))
367                         return 0;
368
369                 return Length-1;        //good: ret chars excl. nullchar
370         }
371
372         return Length;  //bad: ret space needed incl. nullchar
373 }
374
375
376 /*
377  * @implemented
378  */
379 UINT
380 STDCALL
381 GetWindowsDirectoryW (
382         LPWSTR  lpBuffer,
383         UINT    uSize
384         )
385 {
386         ULONG Length;
387
388         if (lpBuffer == NULL)
389                 return 0;
390
391         Length = WindowsDirectory.Length / sizeof (WCHAR);
392         if (uSize > Length)
393         {
394                 memmove (lpBuffer,
395                          WindowsDirectory.Buffer,
396                          WindowsDirectory.Length);
397                 lpBuffer[Length] = 0;
398
399                 return Length;    //good: ret chars excl. nullchar
400         }
401
402         return Length+1;        //bad: ret space needed incl. nullchar
403 }
404
405 /*
406  * @unimplemented
407  */
408 UINT
409 STDCALL
410 GetSystemWindowsDirectoryA(
411         LPSTR   lpBuffer,
412         UINT    uSize
413         )
414 {
415     DbgPrint("Fixme: GetSystemWindowsDirectoryA\n");
416     return GetWindowsDirectoryA( lpBuffer, uSize );
417 }
418
419 /*
420  * @unimplemented
421  */
422 UINT
423 STDCALL
424 GetSystemWindowsDirectoryW(
425         LPWSTR  lpBuffer,
426         UINT    uSize
427         )
428 {
429     DbgPrint("Fixme: GetSystemWindowsDirectoryW\n");
430     return GetWindowsDirectoryW( lpBuffer, uSize );
431 }
432
433 /* EOF */