[project @ 2003-07-10 19:25:58 by sof]
[packages/old-time.git] / cbits / dirUtils.c
1 /*
2 * (c) The University of Glasgow 2002
3 *
4 * Directory Runtime Support
5 */
6
7 #include "config.h"
8
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
13 #endif
14
15 #include "HsBase.h"
16
17 #if defined(mingw32_TARGET_OS)
18 #include <windows.h>
19 #endif
20
21 /*
22 * read an entry from the directory stream; opt for the
23 * re-entrant friendly way of doing this, if available.
24 */
25 HsInt
26 __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
27 {
28 struct dirent **pDirE = (struct dirent**)pDirEnt;
29 #if HAVE_READDIR_R
30 struct dirent* p;
31 int res;
32 static unsigned int nm_max = -1;
33
34 if (pDirE == NULL) {
35 return -1;
36 }
37 if (nm_max == -1) {
38 #ifdef NAME_MAX
39 nm_max = NAME_MAX + 1;
40 #else
41 nm_max = pathconf(".", _PC_NAME_MAX);
42 if (nm_max == -1) { nm_max = 255; }
43 nm_max++;
44 #endif
45 }
46 p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
47 if (p == NULL) return -1;
48 res = readdir_r((DIR*)dirPtr, p, pDirE);
49 if (res != 0) {
50 *pDirE = NULL;
51 free(p);
52 }
53 return res;
54 #else
55
56 if (pDirE == NULL) {
57 return -1;
58 }
59
60 *pDirE = readdir((DIR*)dirPtr);
61 if (*pDirE == NULL) {
62 return -1;
63 } else {
64 return 0;
65 }
66 #endif
67 }
68
69 /*
70 * Function: __hscore_renameFile()
71 *
72 * Provide Haskell98's semantics for renaming files and directories.
73 * It mirrors that of POSIX.1's behaviour for rename() by overwriting
74 * the target if it exists (the MS CRT implementation of rename() returns
75 * an error
76 *
77 */
78 HsInt
79 __hscore_renameFile( HsAddr src,
80 HsAddr dest)
81 {
82 #if defined(_MSC_VER) || defined(_WIN32)
83 static int forNT = -1;
84 DWORD rc;
85
86 /* ToDo: propagate error codes back */
87 if (MoveFileA(src, dest)) {
88 return 0;
89 } else {
90 rc = GetLastError();
91 }
92
93 /* Failed...it could be because the target already existed. */
94 if ( !GetFileAttributes(dest) ) {
95 /* No, it's not there - just fail. */
96 errno = 0;
97 return (-1);
98 }
99
100 if (forNT == -1) {
101 OSVERSIONINFO ovi;
102 ovi.dwOSVersionInfoSize = sizeof(ovi);
103 if ( !GetVersionEx(&ovi) ) {
104 errno = 0;
105 return (-1);
106 }
107 forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
108 }
109
110 if (forNT) {
111 /* Easy, go for MoveFileEx() */
112 if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
113 return 0;
114 } else {
115 errno = 0;
116 return (-1);
117 }
118 }
119
120 /* No MoveFileEx() for Win9x, try deleting the target. */
121 /* Similarly, if the MoveFile*() ops didn't work out under NT */
122 if (DeleteFileA(dest)) {
123 if (MoveFileA(src,dest)) {
124 return 0;
125 } else {
126 errno = 0;
127 return (-1);
128 }
129 } else {
130 errno = 0;
131 return (-1);
132 }
133 #else
134 return rename(src,dest);
135 #endif
136 }
137