2 * (c) The University of Glasgow 2002
4 * Directory Runtime Support
7 /* needed only for solaris2_HOST_OS */
10 // The following is required on Solaris to force the POSIX versions of
11 // the various _r functions instead of the Solaris versions.
12 #ifdef solaris2_HOST_OS
13 #define _POSIX_PTHREAD_SEMANTICS
18 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
26 case ERROR_FILE_NOT_FOUND: return ENOENT;
27 case ERROR_PATH_NOT_FOUND: return ENOENT;
28 case ERROR_TOO_MANY_OPEN_FILES: return EMFILE;
29 case ERROR_ACCESS_DENIED: return EACCES;
30 case ERROR_INVALID_HANDLE: return EBADF; /* kinda sorta */
31 case ERROR_NOT_ENOUGH_MEMORY: return ENOMEM;
32 case ERROR_INVALID_ACCESS: return EINVAL;
33 case ERROR_INVALID_DATA: return EINVAL;
34 case ERROR_OUTOFMEMORY: return ENOMEM;
35 case ERROR_SHARING_VIOLATION: return EACCES;
36 case ERROR_LOCK_VIOLATION: return EACCES;
37 case ERROR_ALREADY_EXISTS: return EEXIST;
38 case ERROR_BUSY: return EBUSY;
39 case ERROR_BROKEN_PIPE: return EPIPE;
40 case ERROR_PIPE_CONNECTED: return EBUSY;
41 case ERROR_PIPE_LISTENING: return EBUSY;
42 case ERROR_NOT_CONNECTED: return EINVAL;
44 case ERROR_NOT_OWNER: return EPERM;
45 case ERROR_DIRECTORY: return ENOTDIR;
46 case ERROR_FILE_INVALID: return EACCES;
47 case ERROR_FILE_EXISTS: return EEXIST;
57 * read an entry from the directory stream; opt for the
58 * re-entrant friendly way of doing this, if available.
61 __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
63 struct dirent **pDirE = (struct dirent**)pDirEnt;
67 static unsigned int nm_max = (unsigned int)-1;
72 if (nm_max == (unsigned int)-1) {
74 nm_max = NAME_MAX + 1;
76 nm_max = pathconf(".", _PC_NAME_MAX);
77 if (nm_max == -1) { nm_max = 255; }
81 p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
82 if (p == NULL) return -1;
83 res = readdir_r((DIR*)dirPtr, p, pDirE);
88 else if (*pDirE == NULL) {
99 *pDirE = readdir((DIR*)dirPtr);
100 if (*pDirE == NULL) {
109 * Function: __hscore_renameFile()
111 * Provide Haskell98's semantics for renaming files and directories.
112 * It mirrors that of POSIX.1's behaviour for rename() by overwriting
113 * the target if it exists (the MS CRT implementation of rename() returns
118 __hscore_renameFile( HsAddr src,
121 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
122 static int forNT = -1;
124 /* ToDo: propagate error codes back */
125 if (MoveFileA(src, dest)) {
131 /* Failed...it could be because the target already existed. */
132 if ( !GetFileAttributes(dest) ) {
133 /* No, it's not there - just fail. */
134 errno = toErrno(GetLastError());
140 ovi.dwOSVersionInfoSize = sizeof(ovi);
141 if ( !GetVersionEx(&ovi) ) {
142 errno = toErrno(GetLastError());
145 forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
149 /* Easy, go for MoveFileEx() */
150 if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
153 errno = toErrno(GetLastError());
158 /* No MoveFileEx() for Win9x, try deleting the target. */
159 /* Similarly, if the MoveFile*() ops didn't work out under NT */
160 if (DeleteFileA(dest)) {
161 if (MoveFileA(src,dest)) {
164 errno = toErrno(GetLastError());
168 errno = toErrno(GetLastError());
172 return rename(src,dest);
177 * Function: __hscore_getFolderPath()
179 * Late-bound version of SHGetFolderPath(), coping with OS versions
180 * that have shell32's lacking that particular API.
183 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
184 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
186 __hscore_getFolderPath(HWND hwndOwner,
192 static int loaded_dll = 0;
193 static HMODULE hMod = (HMODULE)NULL;
194 static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
195 /* The DLLs to try loading entry point from */
196 char* dlls[] = { "shell32.dll", "shfolder.dll" };
198 if (loaded_dll < 0) {
200 } else if (loaded_dll == 0) {
202 for(i=0;i < sizeof(dlls); i++) {
203 hMod = LoadLibrary(dlls[i]);
205 (funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA")) ) {
210 if (loaded_dll == 0) {
215 /* OK, if we got this far the function has been bound */
216 return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
217 /* ToDo: unload the DLL on shutdown? */