| 1 | /** |
| 2 | * This file has no copyright assigned and is placed in the Public Domain. |
| 3 | * This file is part of the mingw-w64 runtime package. |
| 4 | * No warranty is given; refer to the file DISCLAIMER.PD within this package. |
| 5 | */ |
| 6 | |
| 7 | #ifndef __MINGW_FIX_STAT_H |
| 8 | #define __MINGW_FIX_STAT_H |
| 9 | |
| 10 | char* __mingw_fix_stat_path (const char* _path); |
| 11 | wchar_t* __mingw_fix_wstat_path (const wchar_t* _path); |
| 12 | int __mingw_fix_stat_finish(int ret, const void *orig_path, void *used_path, |
| 13 | unsigned short mode); |
| 14 | int __mingw_fix_fstat_finish(int ret, int fd, unsigned short *mode); |
| 15 | int __mingw_fix_wstat_fallback_fd(int ret, const wchar_t *filename, unsigned short mode); |
| 16 | int __mingw_fix_stat_fallback_fd(int ret, const char *filename, unsigned short mode); |
| 17 | |
| 18 | #define __MINGW_FIXED_FSTAT(fstat_func, fd, obj) ({ \ |
| 19 | int _fstat_ret = fstat_func(fd, obj); \ |
| 20 | _fstat_ret = __mingw_fix_fstat_finish(_fstat_ret, fd, &(obj)->st_mode); \ |
| 21 | _fstat_ret; \ |
| 22 | }) |
| 23 | |
| 24 | #define __MINGW_CHOOSE_CHAR_WCHART_EXPR(var, char_expr, wchart_expr, other_expr) \ |
| 25 | __builtin_choose_expr(__builtin_types_compatible_p(typeof(var), char), char_expr, \ |
| 26 | __builtin_choose_expr(__builtin_types_compatible_p(typeof(var), wchar_t), wchart_expr, \ |
| 27 | other_expr)) |
| 28 | |
| 29 | #define __MINGW_PATH_PTR_TYPE(path) \ |
| 30 | typeof(__MINGW_CHOOSE_CHAR_WCHART_EXPR((path)[0], (char*)0, (wchar_t*)0, (void)0)) |
| 31 | |
| 32 | #define __MINGW_FIX_STAT_PATH(path) \ |
| 33 | __MINGW_CHOOSE_CHAR_WCHART_EXPR((path)[0], __mingw_fix_stat_path, __mingw_fix_wstat_path, NULL)(path) |
| 34 | |
| 35 | #define __MINGW_FIX_STAT_FALLBACK_FD(ret, path, mode) \ |
| 36 | __MINGW_CHOOSE_CHAR_WCHART_EXPR((path)[0], __mingw_fix_stat_fallback_fd, __mingw_fix_wstat_fallback_fd, NULL)((ret), (path), (mode)) |
| 37 | |
| 38 | #define __MINGW_CREATE_FILE(path, ...) \ |
| 39 | __MINGW_CHOOSE_CHAR_WCHART_EXPR((path)[0], CreateFileA, CreateFileW, NULL)((path), ##__VA_ARGS__) |
| 40 | |
| 41 | #define __MINGW_STR_PBRK(path, accept) \ |
| 42 | __MINGW_CHOOSE_CHAR_WCHART_EXPR((path)[0], strpbrk, wcspbrk, NULL)((path), __MINGW_CHOOSE_CHAR_WCHART_EXPR((path)[0], accept, L##accept, NULL)) |
| 43 | |
| 44 | #define __MINGW_FIXED_STAT(fstat_func, stat_func, filename, obj) ({ \ |
| 45 | /* First call CRT _stat function with mingw path correction */ \ |
| 46 | int _stat_ret; \ |
| 47 | __MINGW_PATH_PTR_TYPE(filename) path = __MINGW_FIX_STAT_PATH(filename); \ |
| 48 | if (path == NULL && (filename) != NULL) { \ |
| 49 | _stat_ret = -1; \ |
| 50 | } else { \ |
| 51 | _stat_ret = (stat_func)(path, (obj)); \ |
| 52 | _stat_ret = __mingw_fix_stat_finish(_stat_ret, (filename), path, (obj)->st_mode); \ |
| 53 | /* If the CRT _stat function failed then fallback to mingw fstat function */ \ |
| 54 | int _stat_fd = __MINGW_FIX_STAT_FALLBACK_FD(_stat_ret, (filename), (obj)->st_mode); \ |
| 55 | if (_stat_fd >= 0) { \ |
| 56 | _stat_ret = fstat_func(_stat_fd, (void*)(obj)); \ |
| 57 | int _stat_errno = errno; \ |
| 58 | close(_stat_fd); \ |
| 59 | errno = _stat_errno; \ |
| 60 | } \ |
| 61 | } \ |
| 62 | _stat_ret; \ |
| 63 | }) |
| 64 | |
| 65 | #endif |