Lots of changes to the kernel
[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 <wchar.h>
15 #include <ctype.h>
16 #include <string.h>
17
18 #define NDEBUG
19 #include <kernel32/kernel32.h>
20
21 /* GLOBALS *******************************************************************/
22
23 #define MAX_DOS_DRIVES 26
24
25 static HANDLE hCurrentDirectory = NULL;
26 static ULONG CurrentDrive = 0;
27
28 static WCHAR DriveDirectoryW[MAX_DOS_DRIVES][MAX_PATH] = {{0}};
29
30 static WCHAR SystemDirectoryW[MAX_PATH];
31 static WCHAR WindowsDirectoryW[MAX_PATH];
32
33 WINBOOL STDCALL SetCurrentDirectoryW(LPCWSTR lpPathName);
34
35 /* FUNCTIONS *****************************************************************/
36
37 DWORD STDCALL GetCurrentDriveW(DWORD nBufferLength, PWSTR lpBuffer)
38 {
39 lpBuffer[0] = 'A' + CurrentDrive;
40 lpBuffer[1] = ':';
41 lpBuffer[2] = '\\';
42 lpBuffer[3] = 0;
43 return(4);
44 }
45
46 DWORD STDCALL GetCurrentDirectoryA(DWORD nBufferLength, LPSTR lpBuffer)
47 {
48 UINT uSize,i;
49 WCHAR TempDir[MAX_PATH];
50
51 if ( lpBuffer == NULL )
52 return 0;
53
54 GetCurrentDirectoryW(MAX_PATH, TempDir);
55 uSize = lstrlenW(TempDir);
56 if (nBufferLength > uSize)
57 {
58 i = 0;
59 while (TempDir[i] != 0)
60 {
61 lpBuffer[i] = (unsigned char)TempDir[i];
62 i++;
63 }
64 lpBuffer[i] = 0;
65 }
66 return uSize;
67 }
68
69 DWORD STDCALL GetCurrentDirectoryW(DWORD nBufferLength, LPWSTR lpBuffer)
70 {
71 UINT uSize;
72
73 DPRINT("GetCurrentDirectoryW()\n");
74
75 if ( lpBuffer == NULL )
76 return 0;
77 uSize = lstrlenW(DriveDirectoryW[CurrentDrive]) + 2;
78 if (nBufferLength > uSize)
79 {
80 lpBuffer[0] = 'A' + CurrentDrive;
81 lpBuffer[1] = ':';
82 lpBuffer[2] = 0;
83 lstrcpyW(&lpBuffer[2], DriveDirectoryW[CurrentDrive]);
84 }
85 if (uSize > 3 && lpBuffer[uSize - 1] == L'\\')
86 {
87 lpBuffer[uSize - 1] = 0;
88 uSize--;
89 }
90 DPRINT("GetCurrentDirectoryW() = '%w'\n",lpBuffer);
91 return uSize;
92 }
93
94 WINBOOL STDCALL SetCurrentDirectoryA(LPCSTR lpPathName)
95 {
96 UINT i;
97 WCHAR PathNameW[MAX_PATH];
98
99 if ( lpPathName == NULL )
100 {
101 SetLastError(ERROR_INVALID_PARAMETER);
102 return FALSE;
103 }
104
105 if ( lstrlen(lpPathName) > MAX_PATH )
106 {
107 SetLastError(ERROR_BUFFER_OVERFLOW);
108 return FALSE;
109 }
110
111 i = 0;
112 while ((lpPathName[i])!=0 && i < MAX_PATH)
113 {
114 PathNameW[i] = (WCHAR)lpPathName[i];
115 i++;
116 }
117 PathNameW[i] = 0;
118
119 return SetCurrentDirectoryW(PathNameW);
120 }
121
122 WINBOOL STDCALL SetCurrentDirectoryW(LPCWSTR lpPathName)
123 {
124 ULONG len;
125 WCHAR PathName[MAX_PATH];
126 HANDLE hDir;
127
128 DPRINT("SetCurrentDirectoryW(lpPathName %w)\n",lpPathName);
129
130 if (lpPathName == NULL)
131 {
132 SetLastError(ERROR_INVALID_PARAMETER);
133 return FALSE;
134 }
135
136 len = lstrlenW(lpPathName);
137 if (len > MAX_PATH)
138 {
139 SetLastError(ERROR_BUFFER_OVERFLOW);
140 return FALSE;
141 }
142
143 if (!GetFullPathNameW (lpPathName, MAX_PATH, PathName, NULL))
144 {
145 SetLastError(ERROR_BAD_PATHNAME);
146 return FALSE;
147 }
148
149 DPRINT("PathName %w\n",PathName);
150
151 hDir = CreateFileW(PathName,
152 GENERIC_READ,
153 FILE_SHARE_READ,
154 NULL,
155 OPEN_EXISTING,
156 FILE_ATTRIBUTE_DIRECTORY,
157 NULL);
158 if (hDir == INVALID_HANDLE_VALUE)
159 {
160 DPRINT("Failed to open directory\n");
161 SetLastError(ERROR_BAD_PATHNAME);
162 return FALSE;
163 }
164
165 if (hCurrentDirectory != NULL)
166 {
167 CloseHandle(hCurrentDirectory);
168 }
169
170 hCurrentDirectory = hDir;
171
172 DPRINT("PathName %w\n",PathName);
173
174 CurrentDrive = toupper((UCHAR)PathName[0]) - 'A';
175 wcscpy(DriveDirectoryW[CurrentDrive],&PathName[2]);
176 len = lstrlenW(DriveDirectoryW[CurrentDrive]);
177 if (DriveDirectoryW[CurrentDrive][len-1] != '\\')
178 {
179 DriveDirectoryW[CurrentDrive][len] = '\\';
180 DriveDirectoryW[CurrentDrive][len+1] = 0;
181 }
182
183 return TRUE;
184 }
185
186 DWORD STDCALL GetTempPathA(DWORD nBufferLength, LPSTR lpBuffer)
187 {
188 WCHAR BufferW[MAX_PATH];
189 DWORD retCode;
190 UINT i;
191 retCode = GetTempPathW(nBufferLength,BufferW);
192 i = 0;
193 while ((BufferW[i])!=0 && i < MAX_PATH)
194 {
195 lpBuffer[i] = (unsigned char)BufferW[i];
196 i++;
197 }
198 lpBuffer[i] = 0;
199 return retCode;
200 }
201
202 DWORD STDCALL GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer)
203 {
204 WCHAR EnvironmentBufferW[MAX_PATH];
205 UINT i;
206
207 EnvironmentBufferW[0] = 0;
208 i = GetEnvironmentVariableW(L"TMP",EnvironmentBufferW,MAX_PATH);
209 if ( i==0 )
210 i = GetEnvironmentVariableW(L"TEMP",EnvironmentBufferW,MAX_PATH);
211 if ( i==0 )
212 i = GetCurrentDirectoryW(MAX_PATH,EnvironmentBufferW);
213
214 return i;
215 }
216
217 UINT STDCALL GetSystemDirectoryA(LPSTR lpBuffer, UINT uSize)
218 {
219 UINT uPathSize,i;
220 if ( lpBuffer == NULL )
221 return 0;
222 uPathSize = lstrlenW(SystemDirectoryW);
223 if ( uSize > uPathSize ) {
224 i = 0;
225 while ((SystemDirectoryW[i])!=0 && i < uSize)
226 {
227 lpBuffer[i] = (unsigned char)SystemDirectoryW[i];
228 i++;
229 }
230 lpBuffer[i] = 0;
231 }
232
233 return uPathSize;
234 }
235
236 UINT STDCALL GetWindowsDirectoryA(LPSTR lpBuffer, UINT uSize)
237 {
238 UINT uPathSize,i;
239 if ( lpBuffer == NULL )
240 return 0;
241 uPathSize = lstrlenW(WindowsDirectoryW);
242 if ( uSize > uPathSize ) {
243 i = 0;
244 while ((WindowsDirectoryW[i])!=0 && i < uSize)
245 {
246 lpBuffer[i] = (unsigned char)WindowsDirectoryW[i];
247 i++;
248 }
249 lpBuffer[i] = 0;
250 }
251 return uPathSize;
252 }
253
254 UINT
255 STDCALL
256 GetSystemDirectoryW(
257 LPWSTR lpBuffer,
258 UINT uSize
259 )
260 {
261 UINT uPathSize;
262 if ( lpBuffer == NULL )
263 return 0;
264 uPathSize = lstrlenW(SystemDirectoryW);
265 if ( uSize > uPathSize )
266 lstrcpynW(lpBuffer,SystemDirectoryW,uPathSize);
267
268 return uPathSize;
269 }
270
271 UINT
272 STDCALL
273 GetWindowsDirectoryW(
274 LPWSTR lpBuffer,
275 UINT uSize
276 )
277 {
278 UINT uPathSize;
279 if ( lpBuffer == NULL )
280 return 0;
281 uPathSize = lstrlenW(WindowsDirectoryW);
282 if ( uSize > uPathSize );
283 lstrcpynW(lpBuffer,WindowsDirectoryW,uPathSize);
284
285 return uPathSize;
286 }
287