From 39c8698842bc132cbc2f44aadb83f12efc3aa062 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 30 May 2019 06:25:59 -0700 Subject: posix: add close function. * sysif.c (close_wrap): New static function. (sysif_init): close intrinsic registered. * txr.1: Documented. --- sysif.c | 17 +++++++++++++++++ txr.1 | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/sysif.c b/sysif.c index 3556b262..808e0e91 100644 --- a/sysif.c +++ b/sysif.c @@ -609,6 +609,22 @@ static val dup_wrap(val old, val neu) return num(dup2(c_num(old), c_num(neu))); } +static val close_wrap(val fd, val throw_on_error) +{ + int res = close(c_int(fd, lit("close"))); + + if (res < 0) { + if (default_null_arg(throw_on_error)) { + int eno = errno; + uw_throwf(errno_to_file_error(eno), lit("close ~a: ~d/~s"), + fd, num(eno), string_utf8(strerror(eno)), nao); + } + return nil; + } + + return t; +} + val exec_wrap(val file, val args_opt) { val self = lit("execvp"); @@ -1846,6 +1862,7 @@ void sysif_init(void) reg_varl(intern(lit("w-continued"), user_package), num_fast(WCONTINUED)); #endif reg_fun(intern(lit("dupfd"), user_package), func_n2o(dup_wrap, 1)); + reg_fun(intern(lit("close"), user_package), func_n2o(close_wrap, 1)); #endif #if HAVE_PIPE reg_fun(intern(lit("pipe"), user_package), func_n0(pipe_wrap)); diff --git a/txr.1 b/txr.1 index e66684c0..bde02f38 100644 --- a/txr.1 +++ b/txr.1 @@ -54730,6 +54730,28 @@ holds the write end. If the function fails, it throws an exception of type .codn file-error . +.coNP Function @ close +.synb +.mets (close < fileno <> [ throw-on-error-p ]) +.syne +.desc +The +.code close +function passes the integer descriptor +.meta fileno +to the POSIX +.code close +function. If the operation is successful, then +.code t +is returned. Otherwise an exception of type +.code file-error +is thrown, unless the +.meta throw-on-error-p +argument is present, with a true value. In that case, +.code close +indicates failure by returning +.codn nil . + .coNP Function @ poll .synb .mets (poll < poll-list <> [ timeout ]) -- cgit v1.2.3