pipe.ha (751B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use errors; 5 use io; 6 use rt; 7 8 // Flags to use for the [[io::file]]s returned by [[pipe]]. 9 // Only NOCLOEXEC and NONBLOCK are guaranteed to be available. 10 export type pipe_flag = enum { 11 NOCLOEXEC = rt::O_CLOEXEC, 12 NONBLOCK = rt::O_NONBLOCK, 13 }; 14 15 // Create a pair of two linked [[io::file]]s, such that any data written to the 16 // second [[io::file]] may be read from the first. 17 export fn pipe(flags: pipe_flag = 0) ((io::file, io::file) | errors::error) = { 18 let fds: [2]int = [0...]; 19 flags ^= pipe_flag::NOCLOEXEC; // invert CLOEXEC 20 match (rt::pipe2(&fds, flags)) { 21 case void => void; 22 case let e: rt::errno => 23 return errors::errno(e); 24 }; 25 return (fds[0], fds[1]); 26 };