1 : /*
2 : * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : #include <errno.h>
8 : #include <string.h>
9 :
10 : #include "native_client/src/shared/platform/nacl_error.h"
11 :
12 0 : int NaClGetLastErrorString(char* buffer, size_t length) {
13 : #if defined(__native_client__) || (NACL_LINUX && !NACL_ANDROID)
14 : char* message;
15 : /*
16 : * Note some Linux distributions and newlib provide only the GNU version of
17 : * strerror_r().
18 : */
19 0 : if (buffer == NULL || length == 0) {
20 0 : errno = ERANGE;
21 0 : return -1;
22 : }
23 0 : message = strerror_r(errno, buffer, length);
24 0 : if (message != buffer) {
25 0 : size_t message_bytes = strlen(message) + 1;
26 0 : if (message_bytes < length) {
27 0 : length = message_bytes;
28 : }
29 0 : memmove(buffer, message, length);
30 0 : buffer[length - 1] = '\0';
31 : }
32 0 : return 0;
33 : #else
34 : return strerror_r(errno, buffer, length);
35 : #endif
36 : }
|