Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client / src / shared / platform / posix / nacl_error.c
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 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   if (buffer == NULL || length == 0) {
20     errno = ERANGE;
21     return -1;
22   }
23   message = strerror_r(errno, buffer, length);
24   if (message != buffer) {
25     size_t message_bytes = strlen(message) + 1;
26     if (message_bytes < length) {
27       length = message_bytes;
28     }
29     memmove(buffer, message, length);
30     buffer[length - 1] = '\0';
31   }
32   return 0;
33 #else
34   return strerror_r(errno, buffer, length);
35 #endif
36 }