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 ******************************************************************/
19 #include <kernel32/kernel32.h>
21 /* GLOBALS *******************************************************************/
23 #define MAX_DOS_DRIVES 26
25 static HANDLE hCurrentDirectory
= NULL
;
26 static ULONG CurrentDrive
= 0;
28 static WCHAR DriveDirectoryW
[MAX_DOS_DRIVES
][MAX_PATH
] = {{0}};
30 static WCHAR SystemDirectoryW
[MAX_PATH
];
31 static WCHAR WindowsDirectoryW
[MAX_PATH
];
33 WINBOOL STDCALL
SetCurrentDirectoryW(LPCWSTR lpPathName
);
35 /* FUNCTIONS *****************************************************************/
37 DWORD STDCALL
GetCurrentDriveW(DWORD nBufferLength
, PWSTR lpBuffer
)
39 lpBuffer
[0] = 'A' + CurrentDrive
;
46 DWORD STDCALL
GetCurrentDirectoryA(DWORD nBufferLength
, LPSTR lpBuffer
)
49 WCHAR TempDir
[MAX_PATH
];
51 if ( lpBuffer
== NULL
)
54 GetCurrentDirectoryW(MAX_PATH
, TempDir
);
55 uSize
= lstrlenW(TempDir
);
56 if (nBufferLength
> uSize
)
59 while (TempDir
[i
] != 0)
61 lpBuffer
[i
] = (unsigned char)TempDir
[i
];
69 DWORD STDCALL
GetCurrentDirectoryW(DWORD nBufferLength
, LPWSTR lpBuffer
)
73 DPRINT("GetCurrentDirectoryW()\n");
75 if ( lpBuffer
== NULL
)
77 uSize
= lstrlenW(DriveDirectoryW
[CurrentDrive
]) + 2;
78 if (nBufferLength
> uSize
)
80 lpBuffer
[0] = 'A' + CurrentDrive
;
83 lstrcpyW(&lpBuffer
[2], DriveDirectoryW
[CurrentDrive
]);
85 if (uSize
> 3 && lpBuffer
[uSize
- 1] == L
'\\')
87 lpBuffer
[uSize
- 1] = 0;
90 DPRINT("GetCurrentDirectoryW() = '%w'\n",lpBuffer
);
94 WINBOOL STDCALL
SetCurrentDirectoryA(LPCSTR lpPathName
)
97 WCHAR PathNameW
[MAX_PATH
];
99 if ( lpPathName
== NULL
)
101 SetLastError(ERROR_INVALID_PARAMETER
);
105 if ( lstrlen(lpPathName
) > MAX_PATH
)
107 SetLastError(ERROR_BUFFER_OVERFLOW
);
112 while ((lpPathName
[i
])!=0 && i
< MAX_PATH
)
114 PathNameW
[i
] = (WCHAR
)lpPathName
[i
];
119 return SetCurrentDirectoryW(PathNameW
);
122 WINBOOL STDCALL
SetCurrentDirectoryW(LPCWSTR lpPathName
)
125 WCHAR PathName
[MAX_PATH
];
128 DPRINT("SetCurrentDirectoryW(lpPathName %w)\n",lpPathName
);
130 if (lpPathName
== NULL
)
132 SetLastError(ERROR_INVALID_PARAMETER
);
136 len
= lstrlenW(lpPathName
);
139 SetLastError(ERROR_BUFFER_OVERFLOW
);
143 if (!GetFullPathNameW (lpPathName
, MAX_PATH
, PathName
, NULL
))
145 SetLastError(ERROR_BAD_PATHNAME
);
149 DPRINT("PathName %w\n",PathName
);
151 hDir
= CreateFileW(PathName
,
156 FILE_ATTRIBUTE_DIRECTORY
,
158 if (hDir
== INVALID_HANDLE_VALUE
)
160 DPRINT("Failed to open directory\n");
161 SetLastError(ERROR_BAD_PATHNAME
);
165 if (hCurrentDirectory
!= NULL
)
167 CloseHandle(hCurrentDirectory
);
170 hCurrentDirectory
= hDir
;
172 DPRINT("PathName %w\n",PathName
);
174 CurrentDrive
= toupper((UCHAR
)PathName
[0]) - 'A';
175 wcscpy(DriveDirectoryW
[CurrentDrive
],&PathName
[2]);
176 len
= lstrlenW(DriveDirectoryW
[CurrentDrive
]);
177 if (DriveDirectoryW
[CurrentDrive
][len
-1] != '\\')
179 DriveDirectoryW
[CurrentDrive
][len
] = '\\';
180 DriveDirectoryW
[CurrentDrive
][len
+1] = 0;
186 DWORD STDCALL
GetTempPathA(DWORD nBufferLength
, LPSTR lpBuffer
)
188 WCHAR BufferW
[MAX_PATH
];
191 retCode
= GetTempPathW(nBufferLength
,BufferW
);
193 while ((BufferW
[i
])!=0 && i
< MAX_PATH
)
195 lpBuffer
[i
] = (unsigned char)BufferW
[i
];
202 DWORD STDCALL
GetTempPathW(DWORD nBufferLength
, LPWSTR lpBuffer
)
204 WCHAR EnvironmentBufferW
[MAX_PATH
];
207 EnvironmentBufferW
[0] = 0;
208 i
= GetEnvironmentVariableW(L
"TMP",EnvironmentBufferW
,MAX_PATH
);
210 i
= GetEnvironmentVariableW(L
"TEMP",EnvironmentBufferW
,MAX_PATH
);
212 i
= GetCurrentDirectoryW(MAX_PATH
,EnvironmentBufferW
);
217 UINT STDCALL
GetSystemDirectoryA(LPSTR lpBuffer
, UINT uSize
)
220 if ( lpBuffer
== NULL
)
222 uPathSize
= lstrlenW(SystemDirectoryW
);
223 if ( uSize
> uPathSize
) {
225 while ((SystemDirectoryW
[i
])!=0 && i
< uSize
)
227 lpBuffer
[i
] = (unsigned char)SystemDirectoryW
[i
];
236 UINT STDCALL
GetWindowsDirectoryA(LPSTR lpBuffer
, UINT uSize
)
239 if ( lpBuffer
== NULL
)
241 uPathSize
= lstrlenW(WindowsDirectoryW
);
242 if ( uSize
> uPathSize
) {
244 while ((WindowsDirectoryW
[i
])!=0 && i
< uSize
)
246 lpBuffer
[i
] = (unsigned char)WindowsDirectoryW
[i
];
262 if ( lpBuffer
== NULL
)
264 uPathSize
= lstrlenW(SystemDirectoryW
);
265 if ( uSize
> uPathSize
)
266 lstrcpynW(lpBuffer
,SystemDirectoryW
,uPathSize
);
273 GetWindowsDirectoryW(
279 if ( lpBuffer
== NULL
)
281 uPathSize
= lstrlenW(WindowsDirectoryW
);
282 if ( uSize
> uPathSize
);
283 lstrcpynW(lpBuffer
,WindowsDirectoryW
,uPathSize
);