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
11 /* INCLUDES ******************************************************************/
18 #include <kernel32/kernel32.h>
20 /* GLOBALS *******************************************************************/
22 #define MAX_DOS_DRIVES 26
24 static HANDLE hCurrentDirectory
= NULL
;
25 static ULONG CurrentDrive
= 0;
27 static WCHAR DriveDirectoryW
[MAX_DOS_DRIVES
][MAX_PATH
] = {{0}};
29 static WCHAR SystemDirectoryW
[MAX_PATH
];
30 static WCHAR WindowsDirectoryW
[MAX_PATH
];
32 WINBOOL STDCALL
SetCurrentDirectoryW(LPCWSTR lpPathName
);
34 /* FUNCTIONS *****************************************************************/
36 DWORD STDCALL
GetCurrentDriveW(DWORD nBufferLength
, PWSTR lpBuffer
)
38 lpBuffer
[0] = 'A' + CurrentDrive
;
45 DWORD STDCALL
GetCurrentDirectoryA(DWORD nBufferLength
, LPSTR lpBuffer
)
48 WCHAR TempDir
[MAX_PATH
];
50 if ( lpBuffer
== NULL
)
53 GetCurrentDirectoryW(MAX_PATH
, TempDir
);
54 uSize
= lstrlenW(TempDir
);
55 if (nBufferLength
> uSize
)
58 while (TempDir
[i
] != 0)
60 lpBuffer
[i
] = (unsigned char)TempDir
[i
];
68 DWORD STDCALL
GetCurrentDirectoryW(DWORD nBufferLength
, LPWSTR lpBuffer
)
72 DPRINT("GetCurrentDirectoryW()\n");
74 if ( lpBuffer
== NULL
)
76 uSize
= lstrlenW(DriveDirectoryW
[CurrentDrive
]) + 2;
77 if (nBufferLength
> uSize
)
79 lpBuffer
[0] = 'A' + CurrentDrive
;
82 lstrcpyW(&lpBuffer
[2], DriveDirectoryW
[CurrentDrive
]);
84 if (uSize
> 3 && lpBuffer
[uSize
- 1] == L
'\\')
86 lpBuffer
[uSize
- 1] = 0;
89 DPRINT("GetCurrentDirectoryW() = '%w'\n",lpBuffer
);
93 WINBOOL STDCALL
SetCurrentDirectoryA(LPCSTR lpPathName
)
96 WCHAR PathNameW
[MAX_PATH
];
98 if ( lpPathName
== NULL
)
100 if ( lstrlen(lpPathName
) > MAX_PATH
)
103 while ((lpPathName
[i
])!=0 && i
< MAX_PATH
)
105 PathNameW
[i
] = (WCHAR
)lpPathName
[i
];
110 return SetCurrentDirectoryW(PathNameW
);
113 WINBOOL STDCALL
SetCurrentDirectoryW(LPCWSTR lpPathName
)
116 WCHAR PathName
[MAX_PATH
];
120 DPRINT("SetCurrentDirectoryW(lpPathName %w)\n",lpPathName
);
122 if (lpPathName
== NULL
)
125 len
= lstrlenW(lpPathName
);
129 hDir
= CreateFileW(lpPathName
,
134 FILE_ATTRIBUTE_DIRECTORY
,
138 DPRINT("Failed to open directory\n");
141 if (hCurrentDirectory
!= NULL
)
143 CloseHandle(hCurrentDirectory
);
145 hCurrentDirectory
= hDir
;
147 DPRINT("lpPathName %w %x\n",lpPathName
,lpPathName
);
148 if (wcslen(lpPathName
) > 2 &&
149 isalpha(lpPathName
[0]) &&
150 lpPathName
[1] == ':' )
152 DPRINT("lpPathName %w\n",lpPathName
);
154 CurrentDrive
= toupper((UCHAR
)lpPathName
[0]) - 'A';
155 if (!(lpPathName
[2] == '\\' && lpPathName
[3] == 0 &&
156 DriveDirectoryW
[CurrentDrive
][0] != 0))
158 wcscpy(DriveDirectoryW
[CurrentDrive
],&lpPathName
[2]);
159 len
= lstrlenW(DriveDirectoryW
[CurrentDrive
]);
160 if (DriveDirectoryW
[CurrentDrive
][len
-1] != '\\')
162 DriveDirectoryW
[CurrentDrive
][len
] = '\\';
163 DriveDirectoryW
[CurrentDrive
][len
+1] = 0;
168 if (lpPathName
[0] == '\\')
170 wcscpy(DriveDirectoryW
[CurrentDrive
],lpPathName
);
174 len
= GetCurrentDirectoryW(MAX_PATH
, PathName
);
175 if (PathName
[len
-1] != '\\')
177 PathName
[len
] = '\\';
180 lstrcatW(PathName
, lpPathName
);
181 len
= lstrlenW(PathName
);
182 if (PathName
[len
-1] != '\\')
184 PathName
[len
] = '\\';
188 DPRINT("PathName %w\n",PathName
);
191 current
= &PathName
[2];
193 while (current
!= NULL
)
195 if (current
[1] == '.' && current
[2] == '\\')
197 wcscpy(current
, current
+2);
199 else if (current
[1] == '.' && current
[2] == '.' &&
200 current
[3] == '\\' && prev
!= NULL
)
202 wcscpy(prev
, current
+3);
204 while (prev
> PathName
&& (*prev
) != '\\')
208 if (prev
== PathName
)
216 current
= wcschr(current
+1, (WCHAR
)'\\');
220 lstrcpyW(DriveDirectoryW
[CurrentDrive
], &PathName
[2]);
226 DWORD STDCALL
GetTempPathA(DWORD nBufferLength
, LPSTR lpBuffer
)
228 WCHAR BufferW
[MAX_PATH
];
231 retCode
= GetTempPathW(nBufferLength
,BufferW
);
233 while ((BufferW
[i
])!=0 && i
< MAX_PATH
)
235 lpBuffer
[i
] = (unsigned char)BufferW
[i
];
243 DWORD STDCALL
GetTempPathW(DWORD nBufferLength
, LPWSTR lpBuffer
)
245 WCHAR EnvironmentBufferW
[MAX_PATH
];
248 EnvironmentBufferW
[0] = 0;
249 i
= GetEnvironmentVariableW(L
"TMP",EnvironmentBufferW
,MAX_PATH
);
251 i
= GetEnvironmentVariableW(L
"TEMP",EnvironmentBufferW
,MAX_PATH
);
253 i
= GetCurrentDirectoryW(MAX_PATH
,EnvironmentBufferW
);
258 UINT STDCALL
GetSystemDirectoryA(LPSTR lpBuffer
, UINT uSize
)
261 if ( lpBuffer
== NULL
)
263 uPathSize
= lstrlenW(SystemDirectoryW
);
264 if ( uSize
> uPathSize
) {
266 while ((SystemDirectoryW
[i
])!=0 && i
< uSize
)
268 lpBuffer
[i
] = (unsigned char)SystemDirectoryW
[i
];
277 UINT STDCALL
GetWindowsDirectoryA(LPSTR lpBuffer
, UINT uSize
)
280 if ( lpBuffer
== NULL
)
282 uPathSize
= lstrlenW(WindowsDirectoryW
);
283 if ( uSize
> uPathSize
) {
285 while ((WindowsDirectoryW
[i
])!=0 && i
< uSize
)
287 lpBuffer
[i
] = (unsigned char)WindowsDirectoryW
[i
];
303 if ( lpBuffer
== NULL
)
305 uPathSize
= lstrlenW(SystemDirectoryW
);
306 if ( uSize
> uPathSize
)
307 lstrcpynW(lpBuffer
,SystemDirectoryW
,uPathSize
);
315 GetWindowsDirectoryW(
321 if ( lpBuffer
== NULL
)
323 uPathSize
= lstrlenW(WindowsDirectoryW
);
324 if ( uSize
> uPathSize
);
325 lstrcpynW(lpBuffer
,WindowsDirectoryW
,uPathSize
);