Merge "Add portable support for errno.h."
diff --git a/ndk/sources/android/libportable/Android.mk b/ndk/sources/android/libportable/Android.mk
index ea32b2d..44b20a2 100644
--- a/ndk/sources/android/libportable/Android.mk
+++ b/ndk/sources/android/libportable/Android.mk
@@ -36,7 +36,8 @@
 			arch-mips/open.c \
 			arch-mips/socket.c \
 			arch-mips/sockopt.c \
-			arch-mips/epoll.c
+			arch-mips/epoll.c \
+			arch-mips/errno.c
 endif
 
 ifeq ($(TARGET_ARCH),arm)
@@ -44,7 +45,8 @@
 			arch-arm/stat.c \
 			arch-arm/socket.c \
 			arch-arm/sockopt.c \
-			arch-arm/epoll.c
+			arch-arm/epoll.c \
+			arch-arm/errno.c
 endif
 
 ifeq ($(TARGET_ARCH),x86)
@@ -55,7 +57,8 @@
 			arch-x86/socket.c \
 			arch-x86/sockopt.c \
 			arch-x86/fcntl.c \
-			arch-x86/epoll.c
+			arch-x86/epoll.c \
+			arch-x86/errno.c
 endif
 
 LOCAL_SRC_FILES := \
diff --git a/ndk/sources/android/libportable/arch-arm/errno.c b/ndk/sources/android/libportable/arch-arm/errno.c
new file mode 100644
index 0000000..ffa5998
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-arm/errno.c
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern volatile int*   __errno(void);
+volatile int* __errno_portable()
+{
+  return __errno();
+}
diff --git a/ndk/sources/android/libportable/arch-mips/errno.c b/ndk/sources/android/libportable/arch-mips/errno.c
new file mode 100644
index 0000000..7372c67
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-mips/errno.c
@@ -0,0 +1,134 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <errno_portable.h>
+
+#if ENAMETOOLONG==ENAMETOOLONG_PORTABLE
+#error Bad build environment
+#endif
+
+static inline int mips_change_errno(int mips_errno)
+{
+    switch (mips_errno) {
+      case ENAMETOOLONG: return ENAMETOOLONG_PORTABLE;
+      case ENOLCK: return ENOLCK_PORTABLE;
+      case ENOSYS: return ENOSYS_PORTABLE;
+      case ENOTEMPTY: return ENOTEMPTY_PORTABLE;
+      case ELOOP: return ELOOP_PORTABLE;
+      case EWOULDBLOCK: return EWOULDBLOCK_PORTABLE;
+      case ENOMSG: return ENOMSG_PORTABLE;
+      case EIDRM: return EIDRM_PORTABLE;
+      case ECHRNG: return ECHRNG_PORTABLE;
+      case EL2NSYNC: return EL2NSYNC_PORTABLE;
+      case EL3HLT: return EL3HLT_PORTABLE;
+      case EL3RST: return EL3RST_PORTABLE;
+      case ELNRNG: return ELNRNG_PORTABLE;
+      case EUNATCH: return EUNATCH_PORTABLE;
+      case ENOCSI: return ENOCSI_PORTABLE;
+      case EL2HLT: return EL2HLT_PORTABLE;
+      case EBADE: return EBADE_PORTABLE;
+      case EBADR: return EBADR_PORTABLE;
+      case EXFULL: return EXFULL_PORTABLE;
+      case ENOANO: return ENOANO_PORTABLE;
+      case EBADRQC: return EBADRQC_PORTABLE;
+      case EBADSLT: return EBADSLT_PORTABLE;
+      case EDEADLOCK: return EDEADLOCK_PORTABLE;
+      case EBFONT: return EBFONT_PORTABLE;
+      case ENOSTR: return ENOSTR_PORTABLE;
+      case ENODATA: return ENODATA_PORTABLE;
+      case ETIME: return ETIME_PORTABLE;
+      case ENOSR: return ENOSR_PORTABLE;
+      case ENONET: return ENONET_PORTABLE;
+      case ENOPKG: return ENOPKG_PORTABLE;
+      case EREMOTE: return EREMOTE_PORTABLE;
+      case ENOLINK: return ENOLINK_PORTABLE;
+      case EADV: return EADV_PORTABLE;
+      case ESRMNT: return ESRMNT_PORTABLE;
+      case ECOMM: return ECOMM_PORTABLE;
+      case EPROTO: return EPROTO_PORTABLE;
+      case EMULTIHOP: return EMULTIHOP_PORTABLE;
+      case EDOTDOT: return EDOTDOT_PORTABLE;
+      case EBADMSG: return EBADMSG_PORTABLE;
+      case EOVERFLOW: return EOVERFLOW_PORTABLE;
+      case ENOTUNIQ: return ENOTUNIQ_PORTABLE;
+      case EBADFD: return EBADFD_PORTABLE;
+      case EREMCHG: return EREMCHG_PORTABLE;
+      case ELIBACC: return ELIBACC_PORTABLE;
+      case ELIBBAD: return ELIBBAD_PORTABLE;
+      case ELIBSCN: return ELIBSCN_PORTABLE;
+      case ELIBMAX: return ELIBMAX_PORTABLE;
+      case ELIBEXEC: return ELIBEXEC_PORTABLE;
+      case EILSEQ: return EILSEQ_PORTABLE;
+      case ERESTART: return ERESTART_PORTABLE;
+      case ESTRPIPE: return ESTRPIPE_PORTABLE;
+      case EUSERS: return EUSERS_PORTABLE;
+      case ENOTSOCK: return ENOTSOCK_PORTABLE;
+      case EDESTADDRREQ: return EDESTADDRREQ_PORTABLE;
+      case EMSGSIZE: return EMSGSIZE_PORTABLE;
+      case EPROTOTYPE: return EPROTOTYPE_PORTABLE;
+      case ENOPROTOOPT: return ENOPROTOOPT_PORTABLE;
+      case EPROTONOSUPPORT: return EPROTONOSUPPORT_PORTABLE;
+      case ESOCKTNOSUPPORT: return ESOCKTNOSUPPORT_PORTABLE;
+      case EOPNOTSUPP: return EOPNOTSUPP_PORTABLE;
+      case EPFNOSUPPORT: return EPFNOSUPPORT_PORTABLE;
+      case EAFNOSUPPORT: return EAFNOSUPPORT_PORTABLE;
+      case EADDRINUSE: return EADDRINUSE_PORTABLE;
+      case EADDRNOTAVAIL: return EADDRNOTAVAIL_PORTABLE;
+      case ENETDOWN: return ENETDOWN_PORTABLE;
+      case ENETUNREACH: return ENETUNREACH_PORTABLE;
+      case ENETRESET: return ENETRESET_PORTABLE;
+      case ECONNABORTED: return ECONNABORTED_PORTABLE;
+      case ECONNRESET: return ECONNRESET_PORTABLE;
+      case ENOBUFS: return ENOBUFS_PORTABLE;
+      case EISCONN: return EISCONN_PORTABLE;
+      case ENOTCONN: return ENOTCONN_PORTABLE;
+      case ESHUTDOWN: return ESHUTDOWN_PORTABLE;
+      case ETOOMANYREFS: return ETOOMANYREFS_PORTABLE;
+      case ETIMEDOUT: return ETIMEDOUT_PORTABLE;
+      case ECONNREFUSED: return ECONNREFUSED_PORTABLE;
+      case EHOSTDOWN: return EHOSTDOWN_PORTABLE;
+      case EHOSTUNREACH: return EHOSTUNREACH_PORTABLE;
+      case EALREADY: return EALREADY_PORTABLE;
+      case EINPROGRESS: return EINPROGRESS_PORTABLE;
+      case ESTALE: return ESTALE_PORTABLE;
+      case EUCLEAN: return EUCLEAN_PORTABLE;
+      case ENOTNAM: return ENOTNAM_PORTABLE;
+      case ENAVAIL: return ENAVAIL_PORTABLE;
+      case EISNAM: return EISNAM_PORTABLE;
+      case EREMOTEIO: return EREMOTEIO_PORTABLE;
+      case EDQUOT: return EDQUOT_PORTABLE;
+      case ENOMEDIUM: return ENOMEDIUM_PORTABLE;
+      case EMEDIUMTYPE: return EMEDIUMTYPE_PORTABLE;
+      case ECANCELED: return ECANCELED_PORTABLE;
+      case ENOKEY: return ENOKEY_PORTABLE;
+      case EKEYEXPIRED: return EKEYEXPIRED_PORTABLE;
+      case EKEYREVOKED: return EKEYREVOKED_PORTABLE;
+      case EKEYREJECTED: return EKEYREJECTED_PORTABLE;
+      case EOWNERDEAD: return EOWNERDEAD_PORTABLE;
+      case ENOTRECOVERABLE: return ENOTRECOVERABLE_PORTABLE;
+    }
+    return mips_errno;
+}
+
+extern volatile int*   __errno(void);
+volatile int* __errno_portable()
+{
+  /* Note that writing to static_errno will not affect the underlying system. */
+  static int static_errno;
+  static_errno = mips_change_errno(*__errno());
+  return &static_errno;
+}
diff --git a/ndk/sources/android/libportable/arch-x86/errno.c b/ndk/sources/android/libportable/arch-x86/errno.c
new file mode 100644
index 0000000..ffa5998
--- /dev/null
+++ b/ndk/sources/android/libportable/arch-x86/errno.c
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+extern volatile int*   __errno(void);
+volatile int* __errno_portable()
+{
+  return __errno();
+}
diff --git a/ndk/sources/android/libportable/common/include/errno_portable.h b/ndk/sources/android/libportable/common/include/errno_portable.h
new file mode 100644
index 0000000..c2f6523
--- /dev/null
+++ b/ndk/sources/android/libportable/common/include/errno_portable.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2012, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+*/
+
+#ifndef _ERRNO_PORTABLE_H_
+#define _ERRNO_PORTABLE_H_
+
+/* Derived from development/ndk/platforms/android-3/include/asm-generic/errno.h */
+#define EDEADLK_PORTABLE 35
+#define ENAMETOOLONG_PORTABLE 36
+#define ENOLCK_PORTABLE 37
+#define ENOSYS_PORTABLE 38
+#define ENOTEMPTY_PORTABLE 39
+#define ELOOP_PORTABLE 40
+#define EWOULDBLOCK_PORTABLE 11 /* EAGAIN */
+#define ENOMSG_PORTABLE 42
+#define EIDRM_PORTABLE 43
+#define ECHRNG_PORTABLE 44
+#define EL2NSYNC_PORTABLE 45
+#define EL3HLT_PORTABLE 46
+#define EL3RST_PORTABLE 47
+#define ELNRNG_PORTABLE 48
+#define EUNATCH_PORTABLE 49
+#define ENOCSI_PORTABLE 50
+#define EL2HLT_PORTABLE 51
+#define EBADE_PORTABLE 52
+#define EBADR_PORTABLE 53
+#define EXFULL_PORTABLE 54
+#define ENOANO_PORTABLE 55
+#define EBADRQC_PORTABLE 56
+#define EBADSLT_PORTABLE 57
+
+#define EDEADLOCK_PORTABLE EDEADLK_PORTABLE
+
+#define EBFONT_PORTABLE 59
+#define ENOSTR_PORTABLE 60
+#define ENODATA_PORTABLE 61
+#define ETIME_PORTABLE 62
+#define ENOSR_PORTABLE 63
+#define ENONET_PORTABLE 64
+#define ENOPKG_PORTABLE 65
+#define EREMOTE_PORTABLE 66
+#define ENOLINK_PORTABLE 67
+#define EADV_PORTABLE 68
+#define ESRMNT_PORTABLE 69
+#define ECOMM_PORTABLE 70
+#define EPROTO_PORTABLE 71
+#define EMULTIHOP_PORTABLE 72
+#define EDOTDOT_PORTABLE 73
+#define EBADMSG_PORTABLE 74
+#define EOVERFLOW_PORTABLE 75
+#define ENOTUNIQ_PORTABLE 76
+#define EBADFD_PORTABLE 77
+#define EREMCHG_PORTABLE 78
+#define ELIBACC_PORTABLE 79
+#define ELIBBAD_PORTABLE 80
+#define ELIBSCN_PORTABLE 81
+#define ELIBMAX_PORTABLE 82
+#define ELIBEXEC_PORTABLE 83
+#define EILSEQ_PORTABLE 84
+#define ERESTART_PORTABLE 85
+#define ESTRPIPE_PORTABLE 86
+#define EUSERS_PORTABLE 87
+#define ENOTSOCK_PORTABLE 88
+#define EDESTADDRREQ_PORTABLE 89
+#define EMSGSIZE_PORTABLE 90
+#define EPROTOTYPE_PORTABLE 91
+#define ENOPROTOOPT_PORTABLE 92
+#define EPROTONOSUPPORT_PORTABLE 93
+#define ESOCKTNOSUPPORT_PORTABLE 94
+#define EOPNOTSUPP_PORTABLE 95
+#define EPFNOSUPPORT_PORTABLE 96
+#define EAFNOSUPPORT_PORTABLE 97
+#define EADDRINUSE_PORTABLE 98
+#define EADDRNOTAVAIL_PORTABLE 99
+#define ENETDOWN_PORTABLE 100
+#define ENETUNREACH_PORTABLE 101
+#define ENETRESET_PORTABLE 102
+#define ECONNABORTED_PORTABLE 103
+#define ECONNRESET_PORTABLE 104
+#define ENOBUFS_PORTABLE 105
+#define EISCONN_PORTABLE 106
+#define ENOTCONN_PORTABLE 107
+#define ESHUTDOWN_PORTABLE 108
+#define ETOOMANYREFS_PORTABLE 109
+#define ETIMEDOUT_PORTABLE 110
+#define ECONNREFUSED_PORTABLE 111
+#define EHOSTDOWN_PORTABLE 112
+#define EHOSTUNREACH_PORTABLE 113
+#define EALREADY_PORTABLE 114
+#define EINPROGRESS_PORTABLE 115
+#define ESTALE_PORTABLE 116
+#define EUCLEAN_PORTABLE 117
+#define ENOTNAM_PORTABLE 118
+#define ENAVAIL_PORTABLE 119
+#define EISNAM_PORTABLE 120
+#define EREMOTEIO_PORTABLE 121
+#define EDQUOT_PORTABLE 122
+
+#define ENOMEDIUM_PORTABLE 123
+#define EMEDIUMTYPE_PORTABLE 124
+#define ECANCELED_PORTABLE 125
+#define ENOKEY_PORTABLE 126
+#define EKEYEXPIRED_PORTABLE 127
+#define EKEYREVOKED_PORTABLE 128
+#define EKEYREJECTED_PORTABLE 129
+
+#define EOWNERDEAD_PORTABLE 130
+#define ENOTRECOVERABLE_PORTABLE 131
+
+#endif /* _ERRNO_PORTABLE_H */