| 58 | On failure return -2 if AT_FDCWD is -1, -1 otherwise. */ |
| 59 | |
| 60 | int |
| 61 | target_directory_operand (char const *file, struct stat *st) |
| 62 | { |
| 63 | if (must_be_working_directory (file)) |
| 64 | return AT_FDCWD; |
| 65 | |
| 66 | int fd = open (file, O_PATHSEARCH | O_DIRECTORY); |
| 67 | |
| 68 | /* On platforms lacking O_PATH, using O_SEARCH | O_DIRECTORY to |
| 69 | open an overly-protected non-directory can fail with either |
| 70 | EACCES or ENOTDIR. Prefer ENOTDIR as it makes for better |
| 71 | diagnostics. */ |
| 72 | if (O_PATHSEARCH == O_SEARCH && fd < 0 && errno == EACCES) |
| 73 | errno = stat (file, st) < 0 || S_ISDIR (st->st_mode) ? EACCES : ENOTDIR; |
| 74 | |
| 75 | return fd - (AT_FDCWD == -1 && fd < 0); |
| 76 | } |
no test coverage detected