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_TARGET_OS
12 #define _POSIX_PTHREAD_SEMANTICS
17 #if defined(mingw32_TARGET_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 = -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);
94 *pDirE = readdir((DIR*)dirPtr);
104 * Function: __hscore_renameFile()
106 * Provide Haskell98's semantics for renaming files and directories.
107 * It mirrors that of POSIX.1's behaviour for rename() by overwriting
108 * the target if it exists (the MS CRT implementation of rename() returns
113 __hscore_renameFile( HsAddr src,
116 #if (defined(_MSC_VER) || defined(_WIN32))
117 static int forNT = -1;
119 /* ToDo: propagate error codes back */
120 if (MoveFileA(src, dest)) {
126 /* Failed...it could be because the target already existed. */
127 if ( !GetFileAttributes(dest) ) {
128 /* No, it's not there - just fail. */
129 errno = toErrno(GetLastError());
135 ovi.dwOSVersionInfoSize = sizeof(ovi);
136 if ( !GetVersionEx(&ovi) ) {
137 errno = toErrno(GetLastError());
140 forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
144 /* Easy, go for MoveFileEx() */
145 if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
148 errno = toErrno(GetLastError());
153 /* No MoveFileEx() for Win9x, try deleting the target. */
154 /* Similarly, if the MoveFile*() ops didn't work out under NT */
155 if (DeleteFileA(dest)) {
156 if (MoveFileA(src,dest)) {
159 errno = toErrno(GetLastError());
163 errno = toErrno(GetLastError());
167 return rename(src,dest);