reduce dependency on ghcconfig.h
[packages/old-time.git] / cbits / dirUtils.c
1 /*
2 * (c) The University of Glasgow 2002
3 *
4 * Directory Runtime Support
5 */
6
7 /* needed only for solaris2_HOST_OS */
8 #include "ghcconfig.h"
9
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
14 #endif
15
16 #include "HsBase.h"
17
18 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
19 #include <windows.h>
20
21 static
22 int
23 toErrno(DWORD rc)
24 {
25 switch (rc) {
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;
43
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;
48
49 default:
50 return rc;
51 }
52 }
53 #endif
54
55
56 /*
57 * read an entry from the directory stream; opt for the
58 * re-entrant friendly way of doing this, if available.
59 */
60 HsInt
61 __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
62 {
63 struct dirent **pDirE = (struct dirent**)pDirEnt;
64 #if HAVE_READDIR_R
65 struct dirent* p;
66 int res;
67 static unsigned int nm_max = (unsigned int)-1;
68
69 if (pDirE == NULL) {
70 return -1;
71 }
72 if (nm_max == (unsigned int)-1) {
73 #ifdef NAME_MAX
74 nm_max = NAME_MAX + 1;
75 #else
76 nm_max = pathconf(".", _PC_NAME_MAX);
77 if (nm_max == -1) { nm_max = 255; }
78 nm_max++;
79 #endif
80 }
81 p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
82 if (p == NULL) return -1;
83 res = readdir_r((DIR*)dirPtr, p, pDirE);
84 if (res != 0) {
85 *pDirE = NULL;
86 free(p);
87 }
88 else if (*pDirE == NULL) {
89 // end of stream
90 free(p);
91 }
92 return res;
93 #else
94
95 if (pDirE == NULL) {
96 return -1;
97 }
98
99 *pDirE = readdir((DIR*)dirPtr);
100 if (*pDirE == NULL) {
101 return -1;
102 } else {
103 return 0;
104 }
105 #endif
106 }
107
108 /*
109 * Function: __hscore_renameFile()
110 *
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
114 * an error
115 *
116 */
117 HsInt
118 __hscore_renameFile( HsAddr src,
119 HsAddr dest)
120 {
121 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
122 static int forNT = -1;
123
124 /* ToDo: propagate error codes back */
125 if (MoveFileA(src, dest)) {
126 return 0;
127 } else {
128 ;
129 }
130
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());
135 return (-1);
136 }
137
138 if (forNT == -1) {
139 OSVERSIONINFO ovi;
140 ovi.dwOSVersionInfoSize = sizeof(ovi);
141 if ( !GetVersionEx(&ovi) ) {
142 errno = toErrno(GetLastError());
143 return (-1);
144 }
145 forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
146 }
147
148 if (forNT) {
149 /* Easy, go for MoveFileEx() */
150 if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
151 return 0;
152 } else {
153 errno = toErrno(GetLastError());
154 return (-1);
155 }
156 }
157
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)) {
162 return 0;
163 } else {
164 errno = toErrno(GetLastError());
165 return (-1);
166 }
167 } else {
168 errno = toErrno(GetLastError());
169 return (-1);
170 }
171 #else
172 return rename(src,dest);
173 #endif
174 }
175
176 /*
177 * Function: __hscore_getFolderPath()
178 *
179 * Late-bound version of SHGetFolderPath(), coping with OS versions
180 * that have shell32's lacking that particular API.
181 *
182 */
183 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
184 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
185 int
186 __hscore_getFolderPath(HWND hwndOwner,
187 int nFolder,
188 HANDLE hToken,
189 DWORD dwFlags,
190 char* pszPath)
191 {
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" };
197
198 if (loaded_dll < 0) {
199 return (-1);
200 } else if (loaded_dll == 0) {
201 int i;
202 for(i=0;i < sizeof(dlls); i++) {
203 hMod = LoadLibrary(dlls[i]);
204 if ( hMod != NULL &&
205 (funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA")) ) {
206 loaded_dll = 1;
207 break;
208 }
209 }
210 if (loaded_dll == 0) {
211 loaded_dll = (-1);
212 return (-1);
213 }
214 }
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? */
218 }
219 #endif