f3e694d57d5999bdb7204209890f4ec995ae088b
[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 {
100 SetLastError(ERROR_INVALID_PARAMETER);
101 return FALSE;
102 }
103
104 if ( lstrlen(lpPathName) > MAX_PATH )
105 {
106 SetLastError(ERROR_BUFFER_OVERFLOW);
107 return FALSE;
108 }
109
110 i = 0;
111 while ((lpPathName[i])!=0 && i < MAX_PATH)
112 {
113 PathNameW[i] = (WCHAR)lpPathName[i];
114 i++;
115 }
116 PathNameW[i] = 0;
117
118 return SetCurrentDirectoryW(PathNameW);
119 }
120
121 WINBOOL STDCALL SetCurrentDirectoryW(LPCWSTR lpPathName)
122 {
123 ULONG len;
124 WCHAR PathName[MAX_PATH];
125 HANDLE hDir;
126
127 DPRINT("SetCurrentDirectoryW(lpPathName %w)\n",lpPathName);
128
129 if (lpPathName == NULL)
130 {
131 SetLastError(ERROR_INVALID_PARAMETER);
132 return FALSE;
133 }
134
135 len = lstrlenW(lpPathName);
136 if (len > MAX_PATH)
137 {
138 SetLastError(ERROR_BUFFER_OVERFLOW);
139 return FALSE;
140 }
141
142 if (!GetFullPathNameW (lpPathName, MAX_PATH, PathName, NULL))
143 {
144 SetLastError(ERROR_BAD_PATHNAME);
145 return FALSE;
146 }
147
148 DPRINT("PathName %w\n",PathName);
149
150 hDir = CreateFileW(PathName,
151 GENERIC_READ,
152 FILE_SHARE_READ,
153 NULL,
154 OPEN_EXISTING,
155 FILE_ATTRIBUTE_DIRECTORY,
156 NULL);
157 if (hDir == INVALID_HANDLE_VALUE)
158 {
159 DPRINT("Failed to open directory\n");
160 SetLastError(ERROR_BAD_PATHNAME);
161 return FALSE;
162 }
163
164 if (hCurrentDirectory != NULL)
165 {
166 CloseHandle(hCurrentDirectory);
167 }
168
169 hCurrentDirectory = hDir;
170
171 DPRINT("PathName %w\n",PathName);
172
173 CurrentDrive = toupper((UCHAR)PathName[0]) - 'A';
174 wcscpy(DriveDirectoryW[CurrentDrive],&PathName[2]);
175 len = lstrlenW(DriveDirectoryW[CurrentDrive]);
176 if (DriveDirectoryW[CurrentDrive][len-1] != '\\')
177 {
178 DriveDirectoryW[CurrentDrive][len] = '\\';
179 DriveDirectoryW[CurrentDrive][len+1] = 0;
180 }
181
182 return TRUE;
183 }
184
185 DWORD STDCALL GetTempPathA(DWORD nBufferLength, LPSTR lpBuffer)
186 {
187 WCHAR BufferW[MAX_PATH];
188 DWORD retCode;
189 UINT i;
190 retCode = GetTempPathW(nBufferLength,BufferW);
191 i = 0;
192 while ((BufferW[i])!=0 && i < MAX_PATH)
193 {
194 lpBuffer[i] = (unsigned char)BufferW[i];
195 i++;
196 }
197 lpBuffer[i] = 0;
198 return retCode;
199 }
200
201 DWORD STDCALL GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer)
202 {
203 WCHAR EnvironmentBufferW[MAX_PATH];
204 UINT i;
205
206 EnvironmentBufferW[0] = 0;
207 i = GetEnvironmentVariableW(L"TMP",EnvironmentBufferW,MAX_PATH);
208 if ( i==0 )
209 i = GetEnvironmentVariableW(L"TEMP",EnvironmentBufferW,MAX_PATH);
210 if ( i==0 )
211 i = GetCurrentDirectoryW(MAX_PATH,EnvironmentBufferW);
212
213 return i;
214 }
215
216 UINT STDCALL GetSystemDirectoryA(LPSTR lpBuffer, UINT uSize)
217 {
218 UINT uPathSize,i;
219 if ( lpBuffer == NULL )
220 return 0;
221 uPathSize = lstrlenW(SystemDirectoryW);
222 if ( uSize > uPathSize ) {
223 i = 0;
224 while ((SystemDirectoryW[i])!=0 && i < uSize)
225 {
226 lpBuffer[i] = (unsigned char)SystemDirectoryW[i];
227 i++;
228 }
229 lpBuffer[i] = 0;
230 }
231
232 return uPathSize;
233 }
234
235 UINT STDCALL GetWindowsDirectoryA(LPSTR lpBuffer, UINT uSize)
236 {
237 UINT uPathSize,i;
238 if ( lpBuffer == NULL )
239 return 0;
240 uPathSize = lstrlenW(WindowsDirectoryW);
241 if ( uSize > uPathSize ) {
242 i = 0;
243 while ((WindowsDirectoryW[i])!=0 && i < uSize)
244 {
245 lpBuffer[i] = (unsigned char)WindowsDirectoryW[i];
246 i++;
247 }
248 lpBuffer[i] = 0;
249 }
250 return uPathSize;
251 }
252
253 UINT
254 STDCALL
255 GetSystemDirectoryW(
256 LPWSTR lpBuffer,
257 UINT uSize
258 )
259 {
260 UINT uPathSize;
261 if ( lpBuffer == NULL )
262 return 0;
263 uPathSize = lstrlenW(SystemDirectoryW);
264 if ( uSize > uPathSize )
265 lstrcpynW(lpBuffer,SystemDirectoryW,uPathSize);
266
267 return uPathSize;
268 }
269
270 UINT
271 STDCALL
272 GetWindowsDirectoryW(
273 LPWSTR lpBuffer,
274 UINT uSize
275 )
276 {
277 UINT uPathSize;
278 if ( lpBuffer == NULL )
279 return 0;
280 uPathSize = lstrlenW(WindowsDirectoryW);
281 if ( uSize > uPathSize );
282 lstrcpynW(lpBuffer,WindowsDirectoryW,uPathSize);
283
284 return uPathSize;
285 }
286