replaced strerror() with thread-safe strerror_malloc()

This commit is contained in:
2025-11-21 13:15:17 +05:00
parent 225a48a8d9
commit 0b4574b53e
8 changed files with 62 additions and 22 deletions

View File

@@ -1,3 +1,9 @@
// Enable POSIX 2004 definitions.
// Required to use strerror_r in ISO C.
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600
#endif
#include "tlibc/string/cstr.h"
char* cstr_copy(cstr self){
@@ -70,3 +76,15 @@ char* vsprintf_malloc(cstr format, va_list args){
}
return buf;
}
#if defined(_WIN64) || defined(_WIN32)
#define portable_strerror_s(BUF, BUFSIZE, ERRCODE) strerror_s(BUF, BUFSIZE, ERRCODE)
#else
#define portable_strerror_s(BUF, BUFSIZE, ERRCODE) strerror_r(ERRCODE, BUF, BUFSIZE)
#endif
char* strerror_malloc(int errcode){
const int buf_size = 256;
char* buf = malloc(buf_size);
portable_strerror_s(buf, buf_size, errcode);
return buf;
}