]> git.ipfire.org Git - thirdparty/git.git/blob - compat/nonblock.c
Merge branch 'rs/opt-parse-long-fixups'
[thirdparty/git.git] / compat / nonblock.c
1 #include "git-compat-util.h"
2 #include "nonblock.h"
3
4 #ifdef O_NONBLOCK
5
6 int enable_pipe_nonblock(int fd)
7 {
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);
13 }
14
15 #elif defined(GIT_WINDOWS_NATIVE)
16
17 #include "win32.h"
18
19 int enable_pipe_nonblock(int fd)
20 {
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;
27 }
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;
33 }
34 mode |= PIPE_NOWAIT;
35 if (!SetNamedPipeHandleState(h, &mode, NULL, NULL)) {
36 errno = err_win_to_posix(GetLastError());
37 return -1;
38 }
39 return 0;
40 }
41
42 #else
43
44 int enable_pipe_nonblock(int fd UNUSED)
45 {
46 errno = ENOSYS;
47 return -1;
48 }
49
50 #endif