1 /* Copyright (c) 2011 Eric Blake <eblake@redhat.com> */
2 /* POSIX variant of strerror_r. */
3 #define _POSIX_C_SOURCE 200809
4 #include <errno.h>
5 #include <string.h>
6 #include "local.h"
7 
8 int
9 __xpg_strerror_r (int errnum,
10 	char *buffer,
11         size_t n);
12 
13 int
__xpg_strerror_r(int errnum,char * buffer,size_t n)14 __xpg_strerror_r (int errnum,
15 	char *buffer,
16 	size_t n)
17 {
18   char *error;
19   int result = 0;
20 
21   if (!n)
22     return ERANGE;
23   error = _strerror_r (errnum, 1, &result);
24   if (strlen (error) >= n)
25     {
26       memcpy (buffer, error, n - 1);
27       buffer[n - 1] = '\0';
28       return ERANGE;
29     }
30   strcpy (buffer, error);
31   return (result || *error) ? result : EINVAL;
32 }
33