CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmGetPipes.cxx
bloba5b64692ed1b807b615f1dc8982ea380b086a65f
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmGetPipes.h"
5 #include <cm3p/uv.h>
6 #include <fcntl.h>
8 #if defined(_WIN32) && !defined(__CYGWIN__)
9 # include <io.h>
11 int cmGetPipes(int* fds)
13 SECURITY_ATTRIBUTES attr;
14 HANDLE readh, writeh;
15 attr.nLength = sizeof(attr);
16 attr.lpSecurityDescriptor = nullptr;
17 attr.bInheritHandle = FALSE;
18 if (!CreatePipe(&readh, &writeh, &attr, 0))
19 return uv_translate_sys_error(GetLastError());
20 fds[0] = _open_osfhandle((intptr_t)readh, 0);
21 fds[1] = _open_osfhandle((intptr_t)writeh, 0);
22 if (fds[0] == -1 || fds[1] == -1) {
23 CloseHandle(readh);
24 CloseHandle(writeh);
25 return uv_translate_sys_error(GetLastError());
27 return 0;
29 #else
30 # include <cerrno>
32 # include <unistd.h>
34 int cmGetPipes(int* fds)
36 if (pipe(fds) == -1) {
37 return uv_translate_sys_error(errno);
40 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
41 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
42 close(fds[0]);
43 close(fds[1]);
44 return uv_translate_sys_error(errno);
46 return 0;
48 #endif