replaced strerror() with thread-safe strerror_malloc()
This commit is contained in:
@@ -50,7 +50,7 @@ str Error_toStr(Error* e){
|
||||
|
||||
void Error_printAndExit(Error* e){
|
||||
str e_str = Error_toStr(e);
|
||||
printfe("%s\n", e_str.data);
|
||||
printfe("\n"FMT_str"\n", e_str.size, e_str.data);
|
||||
free(e_str.data);
|
||||
Error_free(e);
|
||||
exit(111);
|
||||
|
||||
@@ -37,7 +37,12 @@ Result(bool) dir_create(cstr path){
|
||||
if(mkdir(path, 0777) == -1)
|
||||
#endif
|
||||
{
|
||||
Return RESULT_ERROR_FMT("Can't create dicectory '%s': %s", path, strerror(errno));
|
||||
char* errno_s = strerror_malloc(errno);
|
||||
ResultVar(void) err = RESULT_ERROR_FMT(
|
||||
"Can't create dicectory '%s': %s",
|
||||
path, errno_s);
|
||||
free(errno_s);
|
||||
Return err;
|
||||
}
|
||||
|
||||
Return RESULT_VALUE(i, true);
|
||||
|
||||
@@ -25,21 +25,29 @@ bool file_exists(cstr path){
|
||||
Result(FILE*) file_open(cstr file_name, cstr fopen_mode){
|
||||
FILE* f = fopen(file_name, fopen_mode);
|
||||
if(f == NULL){
|
||||
return RESULT_ERROR_FMT(
|
||||
char* errno_s = strerror_malloc(errno);
|
||||
ResultVar(void) err = RESULT_ERROR_FMT(
|
||||
"Can't open (%s) file '%s': %s",
|
||||
fopen_mode, file_name, strerror(errno));
|
||||
fopen_mode, file_name, errno_s);
|
||||
free(errno_s);
|
||||
return err;
|
||||
}
|
||||
return RESULT_VALUE(p, f);
|
||||
}
|
||||
|
||||
Result(FILE*) file_openOrCreateReadWrite(cstr file_name){
|
||||
FILE* f = fopen(file_name, "rb+");
|
||||
cstr fopen_mode = "rb+";
|
||||
FILE* f = fopen(file_name, fopen_mode);
|
||||
if(f == NULL){
|
||||
f = fopen(file_name, "wb+");
|
||||
fopen_mode = "wb+";
|
||||
f = fopen(file_name, fopen_mode);
|
||||
if(f == NULL){
|
||||
return RESULT_ERROR_FMT(
|
||||
"Can't create (%s) file: %s",
|
||||
file_name, strerror(errno));
|
||||
char* errno_s = strerror_malloc(errno);
|
||||
ResultVar(void) err = RESULT_ERROR_FMT(
|
||||
"Can't open (%s) file '%s': %s",
|
||||
fopen_mode, file_name, errno_s);
|
||||
free(errno_s);
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return RESULT_VALUE(p, f);
|
||||
@@ -57,9 +65,12 @@ Result(i64) file_tellPos(FILE* f){
|
||||
|
||||
Result(void) file_seek(FILE* f, i64 offset, SeekOrigin origin){
|
||||
if(IFWIN(_fseeki64, fseeko64)(f, offset, (int)origin) != 0){
|
||||
return RESULT_ERROR_FMT(
|
||||
char* errno_s = strerror_malloc(errno);
|
||||
ResultVar(void) err = RESULT_ERROR_FMT(
|
||||
"Can't seek (offset: "FMT_i64", origin: %i) in file: %s",
|
||||
offset, origin, strerror(errno));
|
||||
offset, origin, errno_s);
|
||||
free(errno_s);
|
||||
return err;
|
||||
}
|
||||
return RESULT_VOID;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
18
src/time.c
18
src/time.c
@@ -1,10 +1,7 @@
|
||||
// posix version definition to use clock_gettime
|
||||
// Enable POSIX 2004 definitions.
|
||||
// Required to use clock_gettime, localtime_r, gmtime_r in ISO C.
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
#define _XOPEN_SOURCE 600
|
||||
#else
|
||||
#define _XOPEN_SOURCE 500
|
||||
#endif
|
||||
#define _XOPEN_SOURCE 600
|
||||
#endif
|
||||
|
||||
#include "tlibc/time.h"
|
||||
@@ -45,8 +42,13 @@ void sleepMsec(msec_t time){
|
||||
sleepNsec(time * M);
|
||||
}
|
||||
|
||||
#define portable_localtime_s(time_t_ptr, tm_ptr) IFWIN(localtime_s(tm_ptr, time_t_ptr), localtime_r(time_t_ptr, tm_ptr))
|
||||
#define portable_gmtime_s(time_t_ptr, tm_ptr) IFWIN(gmtime_s(tm_ptr, time_t_ptr), gmtime_r(time_t_ptr, tm_ptr))
|
||||
#if defined(_WIN64) || defined(_WIN32)
|
||||
#define portable_localtime_s(time_t_ptr, tm_ptr) localtime_s(tm_ptr, time_t_ptr)
|
||||
#define portable_gmtime_s(time_t_ptr, tm_ptr) gmtime_s(tm_ptr, time_t_ptr)
|
||||
#else
|
||||
#define portable_localtime_s(time_t_ptr, tm_ptr) localtime_r(time_t_ptr, tm_ptr)
|
||||
#define portable_gmtime_s(time_t_ptr, tm_ptr) gmtime_r(time_t_ptr, tm_ptr)
|
||||
#endif
|
||||
|
||||
void DateTime_get(DateTime* dt, bool utc_time){
|
||||
time_t c_time = time(NULL);
|
||||
|
||||
Reference in New Issue
Block a user