Prefer vfork() over system() when opening an URI
[ardour.git] / libs / pbd / pbd / gstdio_compat.h
1 /* glib mingw64/win32 compatibility
2  *
3  * see http://pidgin.im/pipermail/devel/2014-April/023475.html
4  * and https://pidgin.im/pipermail/commits/2014-April/025031.html
5  * http://tracker.ardour.org/view.php?id=6575
6  */
7
8 #ifndef __pbd_gstdio_compat_h__
9 #define __pbd_gstdio_compat_h__
10
11 #include <glib/gstdio.h>
12
13 /* glib's definition of g_stat+GStatBuf is broken for mingw64-w32
14  * (and possibly other 32-bit windows)
15  */
16 #if defined(_WIN32) && !defined(_MSC_VER) && !defined(_WIN64)
17 typedef struct _stat GStatBufW32;
18 static inline int
19 pbd_g_stat(const gchar *filename, GStatBufW32 *buf)
20 {
21         return g_stat(filename, (GStatBuf*)buf);
22 }
23 #  define GStatBuf GStatBufW32
24 #  define g_stat pbd_g_stat
25 #  define g_lstat pbd_g_stat
26 #endif
27
28 /* 64bit mingw -- use _mingw_stat64.h
29  *
30  * glib-2.42.0 wrongly uses _wstat() with 'struct stat' (only MSVC is special cased),
31  * while the windows API is
32  *   int _wstat(const wchar_t*, struct _stat*)
33  * note that  struct _stat != struct stat;
34  */
35 #if defined(_WIN32) && !defined(_MSC_VER) && defined(_WIN64)
36 #include <windows.h>
37 #include <errno.h>
38 #include <wchar.h>
39
40 typedef struct _stat GStatBufW64;
41 static inline int
42 pbd_g_stat(const gchar* filename, GStatBufW64* buf)
43 {
44         gunichar2* wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
45         if (wfilename == NULL) {
46                 errno = EINVAL;
47                 return -1;
48         }
49
50         int len = wcslen ((wchar_t*)wfilename);
51         while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1])) {
52                 --len;
53         }
54         if (len > 0 && (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename)) {
55                 wfilename[len] = '\0';
56         }
57
58         int retval = _wstat ((wchar_t*)wfilename, buf);
59         int save_errno = errno;
60         g_free (wfilename);
61         errno = save_errno;
62         return retval;
63 }
64 #  define GStatBuf GStatBufW64
65 #  define g_stat pbd_g_stat
66 #  define g_lstat pbd_g_stat
67 #endif
68
69 #endif /* __pbd_gstdio_compat_h__ */