hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

pipe.ha (775B)


      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 	DIRECT = rt::O_DIRECT,
     13 	NONBLOCK = rt::O_NONBLOCK,
     14 };
     15 
     16 // Create a pair of two linked [[io::file]]s, such that any data written to the
     17 // second [[io::file]] may be read from the first.
     18 export fn pipe(flags: pipe_flag = 0) ((io::file, io::file) | errors::error) = {
     19 	let fds: [2]int = [0...];
     20 	flags ^= pipe_flag::NOCLOEXEC; // invert CLOEXEC
     21 	match (rt::pipe2(&fds, flags)) {
     22 	case void => void;
     23 	case let e: rt::errno =>
     24 		return errors::errno(e);
     25 	};
     26 	return (fds[0], fds[1]);
     27 };