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( DIR *dirPtr, struct dirent **pDirEnt )
66 static unsigned int nm_max = (unsigned int)-1;
68 if (pDirEnt == NULL) {
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(dirPtr, p, pDirEnt);
87 else if (*pDirEnt == NULL) {
94 if (pDirEnt == NULL) {
98 *pDirEnt = readdir(dirPtr);
99 if (*pDirEnt == NULL) {
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( char *src, char *dest)
119 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
120 static int forNT = -1;
122 /* ToDo: propagate error codes back */
123 if (MoveFileA(src, dest)) {
129 /* Failed...it could be because the target already existed. */
130 if ( !GetFileAttributes(dest) ) {
131 /* No, it's not there - just fail. */
132 errno = toErrno(GetLastError());
138 ovi.dwOSVersionInfoSize = sizeof(ovi);
139 if ( !GetVersionEx(&ovi) ) {
140 errno = toErrno(GetLastError());
143 forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
147 /* Easy, go for MoveFileEx() */
148 if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
151 errno = toErrno(GetLastError());
156 /* No MoveFileEx() for Win9x, try deleting the target. */
157 /* Similarly, if the MoveFile*() ops didn't work out under NT */
158 if (DeleteFileA(dest)) {
159 if (MoveFileA(src,dest)) {
162 errno = toErrno(GetLastError());
166 errno = toErrno(GetLastError());
170 return rename(src,dest);
175 * Function: __hscore_getFolderPath()
177 * Late-bound version of SHGetFolderPath(), coping with OS versions
178 * that have shell32's lacking that particular API.
181 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
182 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
184 __hscore_getFolderPath(HWND hwndOwner,
190 static int loaded_dll = 0;
191 static HMODULE hMod = (HMODULE)NULL;
192 static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
193 /* The DLLs to try loading entry point from */
194 char* dlls[] = { "shell32.dll", "shfolder.dll" };
196 if (loaded_dll < 0) {
198 } else if (loaded_dll == 0) {
200 for(i=0;i < sizeof(dlls); i++) {
201 hMod = LoadLibrary(dlls[i]);
203 (funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA")) ) {
208 if (loaded_dll == 0) {
213 /* OK, if we got this far the function has been bound */
214 return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
215 /* ToDo: unload the DLL on shutdown? */