Files
musl/src/string/strerror_r.c
Rich Felker f313a16224 make strerror_r behave nicer on failure
if the buffer is too short, at least return a partial string. this is
helpful if the caller is lazy and does not check for failure. care is
taken to avoid writing anything if the buffer length is zero, and to
always null-terminate when the buffer length is non-zero.
2012-06-20 12:07:18 -04:00

18 lines
295 B
C

#include <string.h>
#include <errno.h>
int strerror_r(int err, char *buf, size_t buflen)
{
char *msg = strerror(err);
size_t l = strlen(msg);
if (l >= buflen) {
if (buflen) {
memcpy(buf, msg, buflen-1);
buf[buflen-1] = 0;
}
return ERANGE;
}
memcpy(buf, msg, l+1);
return 0;
}