diff options
-rw-r--r-- | meson.build | 4 | ||||
-rw-r--r-- | src/serd_config.h | 13 | ||||
-rw-r--r-- | src/system.c | 14 | ||||
-rw-r--r-- | src/system.h | 4 | ||||
-rw-r--r-- | src/world.c | 12 | ||||
-rw-r--r-- | src/writer.c | 5 |
6 files changed, 45 insertions, 7 deletions
diff --git a/meson.build b/meson.build index 2dc91aa7..fa183942 100644 --- a/meson.build +++ b/meson.build @@ -87,6 +87,10 @@ else 'stdlib.h', 'void* mem=NULL; posix_memalign(&mem, 8U, 8U);', ], + 'strerror_r': [ + 'string.h', + 'char buf[128]; return strerror_r(0, &buf, sizeof(buf));', + ], } # Define HAVE_SOMETHING symbols for all detected features diff --git a/src/serd_config.h b/src/serd_config.h index 909a357d..90c4d31e 100644 --- a/src/serd_config.h +++ b/src/serd_config.h @@ -74,6 +74,13 @@ # endif # endif +// POSIX.1-2001: strerror_r() +# ifndef HAVE_STRERROR_R +# if defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L +# define HAVE_STRERROR_R +# endif +# endif + #endif // !defined(SERD_NO_DEFAULT_CONFIG) /* @@ -102,4 +109,10 @@ # define USE_POSIX_MEMALIGN 0 #endif +#ifdef HAVE_STRERROR_R +# define USE_STRERROR_R 1 +#else +# define USE_STRERROR_R 0 +#endif + #endif // SERD_SRC_SERD_CONFIG_H diff --git a/src/system.c b/src/system.c index ae9e11bc..567d3e1d 100644 --- a/src/system.c +++ b/src/system.c @@ -14,6 +14,20 @@ #include <stdlib.h> #include <string.h> +int +serd_system_strerror(const int errnum, char* const buf, const size_t buflen) +{ +#if USE_STRERROR_R + return strerror_r(errnum, buf, buflen); + +#else // Not thread-safe, but... oh well? + const char* const message = strerror(errnum); + + strncpy(buf, message, buflen); + return 0; +#endif +} + void* serd_malloc_aligned(const size_t alignment, const size_t size) { diff --git a/src/system.h b/src/system.h index d8737dae..1bc19fb9 100644 --- a/src/system.h +++ b/src/system.h @@ -9,6 +9,10 @@ #include <stdint.h> #include <stdio.h> +/// Write the message for a system error code (like errno) to a buffer +int +serd_system_strerror(int errnum, char* buf, size_t buflen); + /// Allocate a buffer aligned to `alignment` bytes SERD_MALLOC_FUNC void* serd_malloc_aligned(size_t alignment, size_t size); diff --git a/src/world.c b/src/world.c index 916aa869..c3c3660f 100644 --- a/src/world.c +++ b/src/world.c @@ -4,6 +4,7 @@ #include "world.h" #include "serd_config.h" +#include "system.h" #if defined(USE_POSIX_FADVISE) # include <fcntl.h> @@ -13,18 +14,17 @@ #include <stdarg.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode) { FILE* fd = fopen(path, mode); if (!fd) { - serd_world_errorf(world, - SERD_BAD_STREAM, - "failed to open file %s (%s)\n", - path, - strerror(errno)); + char message[1024] = {0}; + serd_system_strerror(errno, message, sizeof(message)); + + serd_world_errorf( + world, SERD_BAD_STREAM, "failed to open file %s (%s)\n", path, message); return NULL; } diff --git a/src/writer.c b/src/writer.c index 9c076c11..ced64adc 100644 --- a/src/writer.c +++ b/src/writer.c @@ -8,6 +8,7 @@ #include "sink.h" #include "stack.h" #include "string_utils.h" +#include "system.h" #include "try.h" #include "uri_utils.h" #include "world.h" @@ -257,7 +258,9 @@ sink(const void* buf, size_t len, SerdWriter* writer) const size_t written = serd_byte_sink_write(buf, len, &writer->byte_sink); if (written != len) { if (errno) { - const char* const message = strerror(errno); + char message[1024] = {0}; + serd_system_strerror(errno, message, sizeof(message)); + w_err(writer, SERD_BAD_WRITE, "write error (%s)\n", message); } else { w_err(writer, SERD_BAD_WRITE, "write error\n"); |