| 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 | #include <sys/stat.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <errno.h> |
| 10 | #include "__mingw_fix_stat.h" |
| 11 | |
| 12 | int __mingw_fix_stat_finish(int ret, const void *orig_path, void *used_path, |
| 13 | unsigned short mode) |
| 14 | { |
| 15 | /* |
| 16 | * If the original pathname and used pathname differ, it means that |
| 17 | * __mingw_fix_stat_path or __mingw_fix_wstat_path had to allocate |
| 18 | * a temporary buffer and remove a trailing directory separator. |
| 19 | * In this case the temporary allocation has to be freed, and the |
| 20 | * stat function succeeds only if the pathname was a directory. |
| 21 | */ |
| 22 | if (orig_path != used_path) { |
| 23 | /* Save errno because we call free. */ |
| 24 | int saved_errno = errno; |
| 25 | free(used_path); |
| 26 | |
| 27 | if (ret == 0 && !S_ISDIR(mode)) { |
| 28 | ret = -1; |
| 29 | saved_errno = ENOTDIR; |
| 30 | } |
| 31 | |
| 32 | errno = saved_errno; |
| 33 | } |
| 34 | |
| 35 | return ret; |
| 36 | } |