Sync with 'maint'
[git/gitster.git] / compat / nonblock.c
blob5b51195c32f847ffb058f421fede64439a0d9712
1 #include "git-compat-util.h"
2 #include "nonblock.h"
4 #ifdef O_NONBLOCK
6 int enable_pipe_nonblock(int fd)
8 int flags = fcntl(fd, F_GETFL);
9 if (flags < 0)
10 return -1;
11 flags |= O_NONBLOCK;
12 return fcntl(fd, F_SETFL, flags);
15 #elif defined(GIT_WINDOWS_NATIVE)
17 #include "win32.h"
19 int enable_pipe_nonblock(int fd)
21 HANDLE h = (HANDLE)_get_osfhandle(fd);
22 DWORD mode;
23 DWORD type = GetFileType(h);
24 if (type == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) {
25 errno = EBADF;
26 return -1;
28 if (type != FILE_TYPE_PIPE)
29 BUG("unsupported file type: %lu", type);
30 if (!GetNamedPipeHandleState(h, &mode, NULL, NULL, NULL, NULL, 0)) {
31 errno = err_win_to_posix(GetLastError());
32 return -1;
34 mode |= PIPE_NOWAIT;
35 if (!SetNamedPipeHandleState(h, &mode, NULL, NULL)) {
36 errno = err_win_to_posix(GetLastError());
37 return -1;
39 return 0;
42 #else
44 int enable_pipe_nonblock(int fd UNUSED)
46 errno = ENOSYS;
47 return -1;
50 #endif