2 * (c) The University of Glasgow 2002
4 * Directory Runtime Support
9 // The following is required on Solaris to force the POSIX versions of
10 // the various _r functions instead of the Solaris versions.
11 #ifdef solaris2_HOST_OS
12 #define _POSIX_PTHREAD_SEMANTICS
17 #if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
25 case ERROR_FILE_NOT_FOUND: return ENOENT;
26 case ERROR_PATH_NOT_FOUND: return ENOENT;
27 case ERROR_TOO_MANY_OPEN_FILES: return EMFILE;
28 case ERROR_ACCESS_DENIED: return EACCES;
29 case ERROR_INVALID_HANDLE: return EBADF; /* kinda sorta */
30 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
31 case ERROR_INVALID_ACCESS: return EINVAL;
32 case ERROR_INVALID_DATA: return EINVAL;
33 case ERROR_OUTOFMEMORY: return ENOMEM;
34 case ERROR_SHARING_VIOLATION: return EACCES;
35 case ERROR_LOCK_VIOLATION: return EACCES;
36 case ERROR_ALREADY_EXISTS: return EEXIST;
37 case ERROR_BUSY: return EBUSY;
38 case ERROR_BROKEN_PIPE: return EPIPE;
39 case ERROR_PIPE_CONNECTED: return EBUSY;
40 case ERROR_PIPE_LISTENING: return EBUSY;
41 case ERROR_NOT_CONNECTED: return EINVAL;
43 case ERROR_NOT_OWNER: return EPERM;
44 case ERROR_DIRECTORY: return ENOTDIR;
45 case ERROR_FILE_INVALID: return EACCES;
46 case ERROR_FILE_EXISTS: return EEXIST;
56 * read an entry from the directory stream; opt for the
57 * re-entrant friendly way of doing this, if available.
60 __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
62 struct dirent **pDirE = (struct dirent**)pDirEnt;
66 static unsigned int nm_max = (unsigned int)-1;
71 if (nm_max == (unsigned int)-1) {
73 nm_max = NAME_MAX + 1;
75 nm_max = pathconf(".", _PC_NAME_MAX);
76 if (nm_max == -1) { nm_max = 255; }
80 p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
81 if (p == NULL) return -1;
82 res = readdir_r((DIR*)dirPtr, p, pDirE);
87 else if (*pDirE == NULL) {
98 *pDirE = readdir((DIR*)dirPtr);
108 * Function: __hscore_renameFile()
110 * Provide Haskell98's semantics for renaming files and directories.
111 * It mirrors that of POSIX.1's behaviour for rename() by overwriting
112 * the target if it exists (the MS CRT implementation of rename() returns
117 __hscore_renameFile( HsAddr src,
120 #if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
121 static int forNT = -1;
123 /* ToDo: propagate error codes back */
124 if (MoveFileA(src, dest)) {
130 /* Failed...it could be because the target already existed. */
131 if ( !GetFileAttributes(dest) ) {
132 /* No, it's not there - just fail. */
133 errno = toErrno(GetLastError());
139 ovi.dwOSVersionInfoSize = sizeof(ovi);
140 if ( !GetVersionEx(&ovi) ) {
141 errno = toErrno(GetLastError());
144 forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
148 /* Easy, go for MoveFileEx() */
149 if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
152 errno = toErrno(GetLastError());
157 /* No MoveFileEx() for Win9x, try deleting the target. */
158 /* Similarly, if the MoveFile*() ops didn't work out under NT */
159 if (DeleteFileA(dest)) {
160 if (MoveFileA(src,dest)) {
163 errno = toErrno(GetLastError());
167 errno = toErrno(GetLastError());
171 return rename(src,dest);
176 * Function: __hscore_getFolderPath()
178 * Late-bound version of SHGetFolderPath(), coping with OS versions
179 * that have shell32's lacking that particular API.
182 #if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
183 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
185 __hscore_getFolderPath(HWND hwndOwner,
191 static int loaded_dll = 0;
192 static HMODULE hMod = (HMODULE)NULL;
193 static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
194 /* The DLLs to try loading entry point from */
195 char* dlls[] = { "shell32.dll", "shfolder.dll" };
197 if (loaded_dll < 0) {
199 } else if (loaded_dll == 0) {
201 for(i=0;i < sizeof(dlls); i++) {
202 hMod = LoadLibrary(dlls[i]);
204 (funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA")) ) {
209 if (loaded_dll == 0) {
214 /* OK, if we got this far the function has been bound */
215 return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
216 /* ToDo: unload the DLL on shutdown? */