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