f3e694d57d5999bdb7204209890f4ec995ae088b
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 SetLastError(ERROR_INVALID_PARAMETER
);
104 if ( lstrlen(lpPathName
) > MAX_PATH
)
106 SetLastError(ERROR_BUFFER_OVERFLOW
);
111 while ((lpPathName
[i
])!=0 && i
< MAX_PATH
)
113 PathNameW
[i
] = (WCHAR
)lpPathName
[i
];
118 return SetCurrentDirectoryW(PathNameW
);
121 WINBOOL STDCALL
SetCurrentDirectoryW(LPCWSTR lpPathName
)
124 WCHAR PathName
[MAX_PATH
];
127 DPRINT("SetCurrentDirectoryW(lpPathName %w)\n",lpPathName
);
129 if (lpPathName
== NULL
)
131 SetLastError(ERROR_INVALID_PARAMETER
);
135 len
= lstrlenW(lpPathName
);
138 SetLastError(ERROR_BUFFER_OVERFLOW
);
142 if (!GetFullPathNameW (lpPathName
, MAX_PATH
, PathName
, NULL
))
144 SetLastError(ERROR_BAD_PATHNAME
);
148 DPRINT("PathName %w\n",PathName
);
150 hDir
= CreateFileW(PathName
,
155 FILE_ATTRIBUTE_DIRECTORY
,
157 if (hDir
== INVALID_HANDLE_VALUE
)
159 DPRINT("Failed to open directory\n");
160 SetLastError(ERROR_BAD_PATHNAME
);
164 if (hCurrentDirectory
!= NULL
)
166 CloseHandle(hCurrentDirectory
);
169 hCurrentDirectory
= hDir
;
171 DPRINT("PathName %w\n",PathName
);
173 CurrentDrive
= toupper((UCHAR
)PathName
[0]) - 'A';
174 wcscpy(DriveDirectoryW
[CurrentDrive
],&PathName
[2]);
175 len
= lstrlenW(DriveDirectoryW
[CurrentDrive
]);
176 if (DriveDirectoryW
[CurrentDrive
][len
-1] != '\\')
178 DriveDirectoryW
[CurrentDrive
][len
] = '\\';
179 DriveDirectoryW
[CurrentDrive
][len
+1] = 0;
185 DWORD STDCALL
GetTempPathA(DWORD nBufferLength
, LPSTR lpBuffer
)
187 WCHAR BufferW
[MAX_PATH
];
190 retCode
= GetTempPathW(nBufferLength
,BufferW
);
192 while ((BufferW
[i
])!=0 && i
< MAX_PATH
)
194 lpBuffer
[i
] = (unsigned char)BufferW
[i
];
201 DWORD STDCALL
GetTempPathW(DWORD nBufferLength
, LPWSTR lpBuffer
)
203 WCHAR EnvironmentBufferW
[MAX_PATH
];
206 EnvironmentBufferW
[0] = 0;
207 i
= GetEnvironmentVariableW(L
"TMP",EnvironmentBufferW
,MAX_PATH
);
209 i
= GetEnvironmentVariableW(L
"TEMP",EnvironmentBufferW
,MAX_PATH
);
211 i
= GetCurrentDirectoryW(MAX_PATH
,EnvironmentBufferW
);
216 UINT STDCALL
GetSystemDirectoryA(LPSTR lpBuffer
, UINT uSize
)
219 if ( lpBuffer
== NULL
)
221 uPathSize
= lstrlenW(SystemDirectoryW
);
222 if ( uSize
> uPathSize
) {
224 while ((SystemDirectoryW
[i
])!=0 && i
< uSize
)
226 lpBuffer
[i
] = (unsigned char)SystemDirectoryW
[i
];
235 UINT STDCALL
GetWindowsDirectoryA(LPSTR lpBuffer
, UINT uSize
)
238 if ( lpBuffer
== NULL
)
240 uPathSize
= lstrlenW(WindowsDirectoryW
);
241 if ( uSize
> uPathSize
) {
243 while ((WindowsDirectoryW
[i
])!=0 && i
< uSize
)
245 lpBuffer
[i
] = (unsigned char)WindowsDirectoryW
[i
];
261 if ( lpBuffer
== NULL
)
263 uPathSize
= lstrlenW(SystemDirectoryW
);
264 if ( uSize
> uPathSize
)
265 lstrcpynW(lpBuffer
,SystemDirectoryW
,uPathSize
);
272 GetWindowsDirectoryW(
278 if ( lpBuffer
== NULL
)
280 uPathSize
= lstrlenW(WindowsDirectoryW
);
281 if ( uSize
> uPathSize
);
282 lstrcpynW(lpBuffer
,WindowsDirectoryW
,uPathSize
);