replaced strerror() with thread-safe strerror_malloc()
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user