From fa62e08dc6e915422c0dd99c18e050182e5e694f Mon Sep 17 00:00:00 2001 From: Kaz Kyheku Date: Wed, 15 Jan 2020 21:46:40 -0800 Subject: fstat: take fd argument. * sysif.c (get_fd): Make available under HAVE_SYS_STAT. (fstat): Allow fd or stream argument using get_fd. * txr.1: Documented. --- sysif.c | 19 +++++++------------ txr.1 | 10 +++++++--- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/sysif.c b/sysif.c index 8e9457d0..90f38ee9 100644 --- a/sysif.c +++ b/sysif.c @@ -449,8 +449,7 @@ static val mknod_wrap(val path, val mode, val dev) #endif -#if HAVE_CHMOD - +#if HAVE_CHMOD || HAVE_SYS_STAT static int get_fd(val stream, val self) { val fd_in = if3(integerp(stream), stream, stream_get_prop(stream, fd_k)); @@ -467,6 +466,9 @@ static int get_fd(val stream, val self) return c_int(fd_in, self); } +#endif + +#if HAVE_CHMOD static val chmod_wrap(val target, val mode) { @@ -790,16 +792,9 @@ static int w_lstat(val wpath, struct stat *buf) static int w_fstat(val stream, struct stat *buf) { - val fd = stream_get_prop(stream, fd_k); - - if (fd) { - int res = fstat(c_num(fd), buf); - return res; - } - - uw_throwf(file_error_s, - lit("cannot fstat stream ~s: it has no :fd property"), - stream, nao); + val self = lit("fstat"); + int fd = get_fd(stream, self); + return fstat(fd, buf); } #endif diff --git a/txr.1 b/txr.1 index 0624f488..82533914 100644 --- a/txr.1 +++ b/txr.1 @@ -57697,7 +57697,7 @@ fields take on values of zero. .synb .mets (stat << path ) .mets (lstat << path ) -.mets (fstat << stream ) +.mets (fstat > { stream | << fd }) .syne .desc The @@ -57731,8 +57731,12 @@ The .code fstat function retrieves information about the file system object associated with the open stream -.metn stream . -The stream must be of a kind from which the +.meta stream +or numeric file descriptor +.metn fd . +If a +.meta stream +is specified, that stream must be of a kind from which the .code fileno function can retrieve a file descriptor, otherwise an exception of type .code file-error -- cgit v1.2.3