cca7b2c6e0b3f31b034fe92e0a8c8ee2947afbd3
[ghc-hetmet.git] / ghc / lib / compat / cbits / directory.c
1 #include "../../../includes/ghcconfig.h"
2
3 #include "HsFFI.h"
4
5 #if HAVE_LIMITS_H
6 #include <limits.h>
7 #endif
8 #if HAVE_WINDOWS_H
9 #include <windows.h>
10 #endif
11 #include "directory.h"
12
13 #define INLINE /* nothing */
14
15 /*
16  * Following code copied from libraries/base/includes/HsBase.h
17  */
18
19 #ifdef PATH_MAX
20 /* A size that will contain many path names, but not necessarily all
21  * (PATH_MAX is not defined on systems with unlimited path length,
22  * e.g. the Hurd).
23  */
24 INLINE HsInt __compat_long_path_size() { return PATH_MAX; } 
25 #else
26 INLINE HsInt __compat_long_path_size() { return 4096; }
27 #endif
28
29 #if defined(mingw32_HOST_OS)
30
31 /* Make sure we've got the reqd CSIDL_ constants in scope;
32  * w32api header files are lagging a bit in defining the full set.
33  */
34 #if !defined(CSIDL_APPDATA)
35 #define CSIDL_APPDATA 0x001a
36 #endif
37 #if !defined(CSIDL_PERSONAL)
38 #define CSIDL_PERSONAL 0x0005
39 #endif
40 #if !defined(CSIDL_PROFILE)
41 #define CSIDL_PROFILE 0x0028
42 #endif
43 #if !defined(CSIDL_WINDOWS)
44 #define CSIDL_WINDOWS 0x0024
45 #endif
46
47 INLINE int __hscore_CSIDL_PROFILE()  { return CSIDL_PROFILE;  }
48 INLINE int __hscore_CSIDL_APPDATA()  { return CSIDL_APPDATA;  }
49 INLINE int __hscore_CSIDL_WINDOWS()  { return CSIDL_WINDOWS;  }
50 INLINE int __hscore_CSIDL_PERSONAL() { return CSIDL_PERSONAL; }
51
52 /*
53  * Function: __hscore_getFolderPath()
54  *
55  * Late-bound version of SHGetFolderPath(), coping with OS versions
56  * that have shell32's lacking that particular API.
57  *
58  */
59 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
60 int
61 __hscore_getFolderPath(HWND hwndOwner,
62                        int nFolder,
63                        HANDLE hToken,
64                        DWORD dwFlags,
65                        char*  pszPath)
66 {
67     static int loaded_dll = 0;
68     static HMODULE hMod = (HMODULE)NULL;
69     static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
70     
71     if (loaded_dll < 0) {
72         return (-1);
73     } else if (loaded_dll == 0) {
74         hMod = LoadLibrary("shell32.dll");
75         if (hMod == NULL) {
76             loaded_dll = (-1);
77             return (-1);
78         } else {
79             funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA");
80             if (!funcPtr) {
81                 loaded_dll = (-1);
82                 return (-1);
83             } else {
84                 loaded_dll = 1;
85             }
86         }
87     }
88     /* OK, if we got this far the function has been bound */
89     return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
90     /* ToDo: unload the DLL? */
91 }
92 #endif