Cthulhu  0.2.10
Cthulhu compiler collection
error.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #define _POSIX_C_SOURCE 200112L
4 
5 #include "os/core.h"
6 #include "os_common.h"
7 
8 #include "base/util.h"
9 #include "std/str.h"
10 #include "core/macros.h"
11 
12 #include <string.h>
13 #include <errno.h>
14 
16 CT_LOCAL os_error_t impl_last_error(void)
17 {
18  return (os_error_t)errno;
19 }
20 
21 CT_LOCAL size_t impl_error_length(os_error_t error)
22 {
23  CT_UNUSED(error);
24  return 256; // TODO: find a way to get the size of the buffer
25 }
26 
27 CT_LOCAL size_t impl_error_string(os_error_t error, char *buffer, size_t size)
28 {
29  int err = (int)error;
30  if (strerror_r(err, buffer, size) != 0)
31  {
32  return str_sprintf(buffer, size, "errno: %d", err);
33  }
34 
35  return ctu_strlen(buffer);
36 }
CT_NODISCARD size_t size
Definition: scan.h:128
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
CT_NODISCARD CT_PUREFN CT_BASE_API size_t ctu_strlen(const char *str)
get the length of a string not including the null terminator equivalent to strlen but with safety che...
Definition: util.c:87
#define CT_LOCAL
Definition: compiler.h:166
#define CT_UNUSED(x)
mark a variable as unused
Definition: macros.h:46
CT_STD_API size_t str_sprintf(STA_WRITES(len) char *str, size_t len, STA_FORMAT_STRING const char *fmt,...)
format a string with printf-like syntax
CT_LOCAL size_t impl_error_length(os_error_t error)
Definition: error.c:21
STA_DECL CT_LOCAL os_error_t impl_last_error(void)
Definition: error.c:16
CT_LOCAL size_t impl_error_string(os_error_t error, char *buffer, size_t size)
Definition: error.c:27