compatibility fixes
[reactos.git] / reactos / lib / kernel32 / file / curdir.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/kernel32/file/curdir.c
5 * PURPOSE: Current directory functions
6 * UPDATE HISTORY:
7 * Created 30/09/98
8 */
9
10
11 /* INCLUDES ******************************************************************/
12
13 #include <windows.h>
14 #include <string.h>
15 #include <ctype.h>
16
17 #define NDEBUG
18 #include <kernel32/kernel32.h>
19
20 /* GLOBALS *******************************************************************/
21
22 #define MAX_DOS_DRIVES 26
23
24 static HANDLE hCurrentDirectory = NULL;
25 static ULONG CurrentDrive = 0;
26
27 static WCHAR DriveDirectoryW[MAX_DOS_DRIVES][MAX_PATH] = {{0}};
28
29 static WCHAR SystemDirectoryW[MAX_PATH];
30 static WCHAR WindowsDirectoryW[MAX_PATH];
31
32 WINBOOL STDCALL SetCurrentDirectoryW(LPCWSTR lpPathName);
33
34 /* FUNCTIONS *****************************************************************/
35
36 DWORD STDCALL GetCurrentDriveW(DWORD nBufferLength, PWSTR lpBuffer)
37 {
38 lpBuffer[0] = 'A' + CurrentDrive;
39 lpBuffer[1] = ':';
40 lpBuffer[2] = '\\';
41 lpBuffer[3] = 0;
42 return(4);
43 }
44
45 DWORD STDCALL GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)
46 {
47 UINT uSize,i;
48 WCHAR TempDir[MAX_PATH];
49
50 if ( lpBuffer == NULL )
51 return 0;
52
53 GetCurrentDirectoryW(MAX_PATH, TempDir);
54 uSize = lstrlenW(TempDir);
55 if (nBufferLength > uSize)
56 {
57 i = 0;
58 while (TempDir[i] != 0)
59 {
60 lpBuffer[i] = (unsigned char)TempDir[i];
61 i++;
62 }
63 lpBuffer[i] = 0;
64 }
65 return uSize;
66 }
67
68 DWORD STDCALL GetCurrentDirectoryW(DWORD nBufferLength, LPWSTR lpBuffer)
69 {
70 UINT uSize;
71
72 DPRINT("GetCurrentDirectoryW()\n");
73
74 if ( lpBuffer == NULL )
75 return 0;
76 uSize = lstrlenW(DriveDirectoryW[CurrentDrive]) + 2;
77 if (nBufferLength > uSize)
78 {
79 lpBuffer[0] = 'A' + CurrentDrive;
80 lpBuffer[1] = ':';
81 lpBuffer[2] = 0;
82 lstrcpyW(&lpBuffer[2], DriveDirectoryW[CurrentDrive]);
83 }
84 if (uSize > 3 && lpBuffer[uSize - 1] == L'\\')
85 {
86 lpBuffer[uSize - 1] = 0;
87 uSize--;
88 }
89 DPRINT("GetCurrentDirectoryW() = '%w'\n",lpBuffer);
90 return uSize;
91 }
92
93 WINBOOL STDCALL SetCurrentDirectoryA(LPCSTR lpPathName)
94 {
95 UINT i;
96 WCHAR PathNameW[MAX_PATH];
97
98 if ( lpPathName == NULL )
99 return FALSE;
100 if ( lstrlen(lpPathName) > MAX_PATH )
101 return FALSE;
102 i = 0;
103 while ((lpPathName[i])!=0 && i < MAX_PATH)
104 {
105 PathNameW[i] = (WCHAR)lpPathName[i];
106 i++;
107 }
108 PathNameW[i] = 0;
109
110 return SetCurrentDirectoryW(PathNameW);
111 }
112
113 WINBOOL STDCALL SetCurrentDirectoryW(LPCWSTR lpPathName)
114 {
115 ULONG len;
116 WCHAR PathName[MAX_PATH];
117 HANDLE hDir;
118 PWSTR prev, current;
119
120 DPRINT("SetCurrentDirectoryW(lpPathName %w)\n",lpPathName);
121
122 if (lpPathName == NULL)
123 return FALSE;
124
125 len = lstrlenW(lpPathName);
126 if (len > MAX_PATH)
127 return FALSE;
128
129 hDir = CreateFileW(lpPathName,
130 GENERIC_READ,
131 FILE_SHARE_READ,
132 NULL,
133 OPEN_EXISTING,
134 FILE_ATTRIBUTE_DIRECTORY,
135 NULL);
136 if (hDir == NULL)
137 {
138 DPRINT("Failed to open directory\n");
139 return(FALSE);
140 }
141 if (hCurrentDirectory != NULL)
142 {
143 CloseHandle(hCurrentDirectory);
144 }
145 hCurrentDirectory = hDir;
146
147 DPRINT("lpPathName %w %x\n",lpPathName,lpPathName);
148 if (wcslen(lpPathName) > 2 &&
149 isalpha(lpPathName[0]) &&
150 lpPathName[1] == ':' )
151 {
152 DPRINT("lpPathName %w\n",lpPathName);
153
154 CurrentDrive = toupper((UCHAR)lpPathName[0]) - 'A';
155 if (!(lpPathName[2] == '\\' && lpPathName[3] == 0 &&
156 DriveDirectoryW[CurrentDrive][0] != 0))
157 {
158 wcscpy(DriveDirectoryW[CurrentDrive],&lpPathName[2]);
159 len = lstrlenW(DriveDirectoryW[CurrentDrive]);
160 if (DriveDirectoryW[CurrentDrive][len-1] != '\\')
161 {
162 DriveDirectoryW[CurrentDrive][len] = '\\';
163 DriveDirectoryW[CurrentDrive][len+1] = 0;
164 }
165 }
166 return(TRUE);
167 }
168 if (lpPathName[0] == '\\')
169 {
170 wcscpy(DriveDirectoryW[CurrentDrive],lpPathName);
171 return(TRUE);
172 }
173
174 len = GetCurrentDirectoryW(MAX_PATH, PathName);
175 if (PathName[len-1] != '\\')
176 {
177 PathName[len] = '\\';
178 PathName[len+1] = 0;
179 }
180 lstrcatW(PathName, lpPathName);
181 len = lstrlenW(PathName);
182 if (PathName[len-1] != '\\')
183 {
184 PathName[len] = '\\';
185 PathName[len+1] = 0;
186 }
187
188 DPRINT("PathName %w\n",PathName);
189
190 prev = NULL;
191 current = &PathName[2];
192
193 while (current != NULL)
194 {
195 if (current[1] == '.' && current[2] == '\\')
196 {
197 wcscpy(current, current+2);
198 }
199 else if (current[1] == '.' && current[2] == '.' &&
200 current[3] == '\\' && prev != NULL)
201 {
202 wcscpy(prev, current+3);
203 current = prev;
204 while (prev > PathName && (*prev) != '\\')
205 {
206 prev--;
207 }
208 if (prev == PathName)
209 {
210 prev = NULL;
211 }
212 }
213 else
214 {
215 prev = current;
216 current = wcschr(current+1, (WCHAR)'\\');
217 }
218 }
219
220 lstrcpyW(DriveDirectoryW[CurrentDrive], &PathName[2]);
221 return TRUE;
222 }
223
224
225
226 DWORD STDCALL GetTempPathA(DWORD nBufferLength, LPSTR lpBuffer)
227 {
228 WCHAR BufferW[MAX_PATH];
229 DWORD retCode;
230 UINT i;
231 retCode = GetTempPathW(nBufferLength,BufferW);
232 i = 0;
233 while ((BufferW[i])!=0 && i < MAX_PATH)
234 {
235 lpBuffer[i] = (unsigned char)BufferW[i];
236 i++;
237 }
238 lpBuffer[i] = 0;
239 return retCode;
240
241 }
242
243 DWORD STDCALL GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer)
244 {
245 WCHAR EnvironmentBufferW[MAX_PATH];
246 UINT i;
247
248 EnvironmentBufferW[0] = 0;
249 i = GetEnvironmentVariableW(L"TMP",EnvironmentBufferW,MAX_PATH);
250 if ( i==0 )
251 i = GetEnvironmentVariableW(L"TEMP",EnvironmentBufferW,MAX_PATH);
252 if ( i==0 )
253 i = GetCurrentDirectoryW(MAX_PATH,EnvironmentBufferW);
254
255 return i;
256 }
257
258 UINT STDCALL GetSystemDirectoryA(LPSTR lpBuffer, UINT uSize)
259 {
260 UINT uPathSize,i;
261 if ( lpBuffer == NULL )
262 return 0;
263 uPathSize = lstrlenW(SystemDirectoryW);
264 if ( uSize > uPathSize ) {
265 i = 0;
266 while ((SystemDirectoryW[i])!=0 && i < uSize)
267 {
268 lpBuffer[i] = (unsigned char)SystemDirectoryW[i];
269 i++;
270 }
271 lpBuffer[i] = 0;
272 }
273
274 return uPathSize;
275 }
276
277 UINT STDCALL GetWindowsDirectoryA(LPSTR lpBuffer, UINT uSize)
278 {
279 UINT uPathSize,i;
280 if ( lpBuffer == NULL )
281 return 0;
282 uPathSize = lstrlenW(WindowsDirectoryW);
283 if ( uSize > uPathSize ) {
284 i = 0;
285 while ((WindowsDirectoryW[i])!=0 && i < uSize)
286 {
287 lpBuffer[i] = (unsigned char)WindowsDirectoryW[i];
288 i++;
289 }
290 lpBuffer[i] = 0;
291 }
292 return uPathSize;
293 }
294
295 UINT
296 STDCALL
297 GetSystemDirectoryW(
298 LPWSTR lpBuffer,
299 UINT uSize
300 )
301 {
302 UINT uPathSize;
303 if ( lpBuffer == NULL )
304 return 0;
305 uPathSize = lstrlenW(SystemDirectoryW);
306 if ( uSize > uPathSize )
307 lstrcpynW(lpBuffer,SystemDirectoryW,uPathSize);
308
309
310 return uPathSize;
311 }
312
313 UINT
314 STDCALL
315 GetWindowsDirectoryW(
316 LPWSTR lpBuffer,
317 UINT uSize
318 )
319 {
320 UINT uPathSize;
321 if ( lpBuffer == NULL )
322 return 0;
323 uPathSize = lstrlenW(WindowsDirectoryW);
324 if ( uSize > uPathSize );
325 lstrcpynW(lpBuffer,WindowsDirectoryW,uPathSize);
326
327 return uPathSize;
328 }
329
330