summaryrefslogtreecommitdiffstats
path: root/sysif.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2020-06-19 20:25:36 -0700
committerKaz Kylheku <kaz@kylheku.com>2020-06-19 20:25:36 -0700
commit29a25713116323adcac696a090420ed3c1e73e5f (patch)
tree53c4e86a83e015268ea435dace0a21d90ca6766e /sysif.c
parent09091bedcc6dd570262de4a72c362b161a492df7 (diff)
downloadtxr-29a25713116323adcac696a090420ed3c1e73e5f.tar.gz
txr-29a25713116323adcac696a090420ed3c1e73e5f.tar.bz2
txr-29a25713116323adcac696a090420ed3c1e73e5f.zip
New function: strerror
* configure (have_strerror_r): New variable. Detect whether we have strerror_r and which flavor: GNU or POSIX. Define HAVE_STRERROR_GNU or HAVE_STRERROR_POSIX in config.h. * sysif.c (strerror_wrap): New function. (sysif_init): Register strerror intrinsic. * txr.1: Documented.
Diffstat (limited to 'sysif.c')
-rw-r--r--sysif.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/sysif.c b/sysif.c
index 3f25bb7e..701a1eed 100644
--- a/sysif.c
+++ b/sysif.c
@@ -141,6 +141,21 @@ static val errno_wrap(val newval)
return oldval;
}
+static val strerror_wrap(val errnum)
+{
+ val self = lit("strerror");
+ int eno = c_int(errnum, self);
+#if HAVE_STRERROR_POSIX
+ char buf[128];
+ return strerror_r(eno, buf, sizeof buf) >= 0 ? string_utf8(buf) : nil;
+#elif HAVE_STRERROR_GNU
+ char buf[128];
+ return string_utf8(strerror_r(eno, buf, sizeof buf));
+#else
+ return string_utf8(strerror(eno));
+#endif
+}
+
#if HAVE_DAEMON
static val daemon_wrap(val nochdir, val noclose)
{
@@ -2327,6 +2342,7 @@ void sysif_init(void)
reg_varl(intern(lit("exdev"), user_package), num_fast(EXDEV));
reg_fun(intern(lit("errno"), user_package), func_n1o(errno_wrap, 0));
+ reg_fun(intern(lit("strerror"), user_package), func_n1o(strerror_wrap, 0));
reg_fun(intern(lit("exit"), user_package), func_n1(exit_wrap));
reg_fun(intern(lit("at-exit-call"), user_package), func_n1(at_exit_call));
reg_fun(intern(lit("at-exit-do-not-call"), user_package), func_n1(at_exit_do_not_call));