From dc8f0102a8fa579c920f296e41a891784a49f008 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 16 Feb 2024 08:39:33 +0900 Subject: [PATCH] fixed the strerror_r issue arising for implementation difference --- bin/main.c | 7 +++++-- lib/std.c | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/main.c b/bin/main.c index 352c8f7..6dd1c1a 100644 --- a/bin/main.c +++ b/bin/main.c @@ -637,7 +637,10 @@ int main (int argc, char* argv[]) if (argc < 2) { print_usage: - fprintf (stderr, "Usage: %s filename ...\n", argv[0]); + fprintf (stderr, "Usage: %s [options] script-filename [output-filename]\n", argv[0]); + fprintf (stderr, "Options are:\n"); + fprintf (stderr, " -b enable block construct with {}\n"); + fprintf (stderr, " -n enable line-break as expression terminator\n"); return -1; } @@ -700,7 +703,7 @@ int main (int argc, char* argv[]) } } - if ((opt.ind + 1) != argc && !show_info) goto print_usage; + if ((opt.ind + 1) != argc && (opt.ind + 2) != argc && !show_info) goto print_usage; #endif hcl = hcl_openstd(HCL_SIZEOF(xtn_t), HCL_NULL); diff --git a/lib/std.c b/lib/std.c index 89d3b39..9b5400d 100644 --- a/lib/std.c +++ b/lib/std.c @@ -906,7 +906,18 @@ static hcl_errnum_t _syserrstrb (hcl_t* hcl, int syserr_type, int syserr_code, h #if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__) if (buf) strerror_s (buf, len, syserr_code); #elif defined(HAVE_STRERROR_R) - if (buf) strerror_r (syserr_code, buf, len); + if (buf) + { + /* to work around mess between XSI and GNU strerror_r */ + hcl_oow_t x; + if (len > 0) buf[0] = '\0'; + /* cast to hcl_oow_t to cater for prototype difference. + * one returning int, the other returning a pointer. + * x > 1024 in case the XSI version before glibc 2.13 returns + * a positive error code upon failure */ + x = (hcl_oow_t)strerror_r(syserr_code, buf, len); + if (len > 0 && buf[0] == '\0' && x != 0 && x > 1024 && x != (hcl_oow_t)-1) hcl_copy_bcstr (buf, len, x); + } #else /* this may be thread unsafe */ if (buf) hcl_copy_bcstr (buf, len, strerror(syserr_code)); @@ -3663,7 +3674,7 @@ static HCL_INLINE int open_udo_stream (hcl_t* hcl, hcl_io_udoarg_t* arg) if (!fp) { if (xtn->udo_path) - hcl_seterrbfmtwithsyserr (hcl, 0, errno, "unable to open udp stream '%hs'", xtn->udo_path); + hcl_seterrbfmtwithsyserr (hcl, 0, errno, "unable to open udo stream '%hs'", xtn->udo_path); else hcl_seterrbfmtwithsyserr (hcl, 0, errno, "unable to open udo stream", xtn->udo_path); return -1;