Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
strerror_r.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <errno.h>
3 #include <string.h>
4 
5 int strerror_r(int err_no, char* buffer, size_t buffer_size)
6 {
7  int r = 0;
8  char* err_msg = strerror(err_no);
9  size_t length = strlen(err_msg);
10 
11  assert(buffer);
12 
13  if(length >= buffer_size)
14  {
15  if(buffer_size)
16  {
17  // -1 so we don't copy an extra byte...
18  memcpy(buffer, err_msg, buffer_size - 1);
19  // since we will null terminate the string
20  buffer[buffer_size - 1] = 0;
21  }
22 
23  r = ERANGE;
24  }
25  else
26  {
27  // +1 for null termination
28  memcpy(buffer, err_msg, length + 1);
29  }
30 
31  return r;
32 }
#define assert(x)
Definition: assert.h:11
#define ERANGE
Definition: errno.h:50
size_t strlen(const char *str)
Returns the length of the given null-terminated byte string.
Definition: strlen.c:77
char * strerror(int)
Definition: strerror.c:17
void * memcpy(void *__restrict dest, const void *__restrict src, size_t n)
Copies n characters from the object pointed to by src to the object pointed to by dest.
int strerror_r(int err_no, char *buffer, size_t buffer_size)
Definition: strerror_r.c:5