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

@@ -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);

View File

@@ -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;
}