blob: 0c72019860cf81a300bc84da1e266853bbc7f6d5 [file] [log] [blame]
Elliott Hughes567a8de2013-10-24 17:14:55 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29// This file perpetuates the mistakes of the past, but only for 32-bit targets.
30#if !defined(__LP64__)
31
Elliott Hughesefbdb532014-04-07 15:17:19 -070032#include <ctype.h>
Elliott Hughesd1ead2a2014-06-06 15:24:20 -070033#include <dirent.h>
Elliott Hughesefbdb532014-04-07 15:17:19 -070034#include <inttypes.h>
Calin Juravlea4eafa62014-03-10 18:10:04 +000035#include <pthread.h>
Dan Albert205dd7d2014-06-04 10:14:19 -070036#include <signal.h>
Elliott Hughesfcac8ff2014-05-22 01:24:30 -070037#include <stdio.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070038#include <stdlib.h>
David 'Digit' Turner891dedb2014-06-13 12:28:11 +020039#include <string.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070040#include <sys/resource.h>
Elliott Hughesbd3a98c2014-05-24 17:19:36 -070041#include <sys/syscall.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070042#include <sys/time.h>
43#include <sys/types.h>
44#include <sys/wait.h>
45#include <unistd.h>
Dan Albert001f8f02014-06-04 09:53:06 -070046#include <wchar.h>
Elliott Hughes567a8de2013-10-24 17:14:55 -070047
48// These were accidentally declared in <unistd.h> because we stupidly used to inline
49// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps.
50extern "C" {
51 unsigned int __page_size = PAGE_SIZE;
Elliott Hughes0e44bc32014-02-24 15:55:31 -080052 unsigned int __page_shift = 12;
Elliott Hughes567a8de2013-10-24 17:14:55 -070053}
54
55// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
56extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
57 return wait4(pid, status, options, rusage);
58}
59
Elliott Hughes06209252013-11-06 16:20:54 -080060// TODO: does anything still need this?
Elliott Hughes567a8de2013-10-24 17:14:55 -070061extern "C" int __open() {
62 abort();
63}
64
Elliott Hughes06209252013-11-06 16:20:54 -080065// TODO: does anything still need this?
66extern "C" void** __get_tls() {
67#include "private/__get_tls.h"
68 return __get_tls();
69}
70
Elliott Hughes152b9de2014-03-10 15:54:40 -070071// This non-standard function was in our <string.h> for some reason.
72extern "C" void memswap(void* m1, void* m2, size_t n) {
73 char* p = reinterpret_cast<char*>(m1);
74 char* p_end = p + n;
75 char* q = reinterpret_cast<char*>(m2);
76 while (p < p_end) {
77 char tmp = *p;
78 *p = *q;
79 *q = tmp;
80 p++;
81 q++;
82 }
83}
84
Calin Juravlea4eafa62014-03-10 18:10:04 +000085extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
86 // This was removed from POSIX.1-2008, and is not implemented on bionic.
87 // Needed for ABI compatibility with the NDK.
88 return ENOSYS;
89}
90
91extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
92 // This was removed from POSIX.1-2008.
93 // Needed for ABI compatibility with the NDK.
94 *stack_addr = (char*)attr->stack_base + attr->stack_size;
95 return 0;
96}
97
Elliott Hughesefbdb532014-04-07 15:17:19 -070098// Non-standard cruft that should only ever have been in system/core/toolbox.
99extern "C" char* strtotimeval(const char* str, struct timeval* ts) {
100 char* s;
101 ts->tv_sec = strtoumax(str, &s, 10);
102
103 long fractional_seconds = 0;
104 if (*s == '.') {
105 s++;
106 int count = 0;
107
108 // Read up to 6 digits (microseconds).
109 while (*s && isdigit(*s)) {
110 if (++count < 7) {
111 fractional_seconds = fractional_seconds*10 + (*s - '0');
112 }
113 s++;
114 }
115
116 for (; count < 6; count++) {
117 fractional_seconds *= 10;
118 }
119 }
120
121 ts->tv_usec = fractional_seconds;
122 return s;
123}
124
Elliott Hugheseae59022014-04-22 17:56:42 -0700125static inline int digitval(int ch) {
126 unsigned d;
127
128 d = (unsigned)(ch - '0');
129 if (d < 10) return (int)d;
130
131 d = (unsigned)(ch - 'a');
132 if (d < 6) return (int)(d+10);
133
134 d = (unsigned)(ch - 'A');
135 if (d < 6) return (int)(d+10);
136
137 return -1;
138}
139
140// This non-standard function was in our <inttypes.h> for some reason.
141extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
142 const unsigned char* p = (const unsigned char *)nptr;
143 const unsigned char* end = p + n;
144 int minus = 0;
145 uintmax_t v = 0;
146 int d;
147
148 while (p < end && isspace(*p)) {
149 p++;
150 }
151
152 if (p < end) {
153 char c = p[0];
154 if (c == '-' || c == '+') {
155 minus = (c == '-');
156 p++;
157 }
158 }
159
160 if (base == 0) {
161 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
162 p += 2;
163 base = 16;
164 } else if (p+1 < end && p[0] == '0') {
165 p += 1;
166 base = 8;
167 } else {
168 base = 10;
169 }
170 } else if (base == 16) {
171 if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
172 p += 2;
173 }
174 }
175
176 while (p < end && (d = digitval(*p)) >= 0 && d < base) {
177 v = v*base + d;
178 p += 1;
179 }
180
181 if (endptr) {
182 *endptr = (char*) p;
183 }
184
185 return minus ? -v : v;
186}
187
188// This non-standard function was in our <inttypes.h> for some reason.
189extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
190 return (intmax_t) strntoumax(nptr, endptr, base, n);
191}
192
Elliott Hughesfcac8ff2014-05-22 01:24:30 -0700193// POSIX calls this dprintf, but LP32 Android had fdprintf instead.
194extern "C" int fdprintf(int fd, const char* fmt, ...) {
195 va_list ap;
196 va_start(ap, fmt);
197 int rc = vdprintf(fd, fmt, ap);
198 va_end(ap);
199 return rc;
200}
201
202// POSIX calls this vdprintf, but LP32 Android had fdprintf instead.
203extern "C" int vfdprintf(int fd, const char* fmt, va_list ap) {
204 return vdprintf(fd, fmt, ap);
205}
206
Elliott Hughesb30aff42014-05-28 19:35:33 +0000207#define __futex_wake __real_futex_wake
208#define __futex_wait __real_futex_wait
209#include "private/bionic_futex.h"
210#undef __futex_wake
211#undef __futex_wait
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700212
213// This used to be in <sys/atomics.h>.
214extern "C" int __futex_wake(volatile void* ftx, int count) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000215 return __real_futex_wake(ftx, count);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700216}
217
218// This used to be in <sys/atomics.h>.
219extern "C" int __futex_wait(volatile void* ftx, int value, const struct timespec* timeout) {
Elliott Hughesb30aff42014-05-28 19:35:33 +0000220 return __real_futex_wait(ftx, value, timeout);
Elliott Hughesbd3a98c2014-05-24 17:19:36 -0700221}
222
Anthony King00170732014-05-24 16:47:14 +0000223// Unity's libmono uses this.
224extern "C" int tkill(pid_t tid, int sig) {
225 return syscall(__NR_tkill, tid, sig);
226}
227
Elliott Hughes1628eb12014-08-06 10:47:33 -0700228// This was removed from POSIX 2008.
Dan Albert001f8f02014-06-04 09:53:06 -0700229extern "C" wchar_t* wcswcs(wchar_t* haystack, wchar_t* needle) {
230 return wcsstr(haystack, needle);
231}
232
Dan Albert205dd7d2014-06-04 10:14:19 -0700233// This was removed from POSIX 2008.
234extern "C" sighandler_t bsd_signal(int signum, sighandler_t handler) {
235 return signal(signum, handler);
236}
237
238// sysv_signal() was never in POSIX.
239extern sighandler_t _signal(int signum, sighandler_t handler, int flags);
240extern "C" sighandler_t sysv_signal(int signum, sighandler_t handler) {
241 return _signal(signum, handler, SA_RESETHAND);
242}
243
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700244// This is a system call that was never in POSIX. Use readdir(3) instead.
Elliott Hughesd1ead2a2014-06-06 15:24:20 -0700245extern "C" int __getdents64(unsigned int, dirent*, unsigned int);
246extern "C" int getdents(unsigned int fd, dirent* dirp, unsigned int count) {
Elliott Hughes3d5cb302014-06-06 11:44:55 -0700247 return __getdents64(fd, dirp, count);
248}
249
Elliott Hughesbffbfee2014-06-06 20:41:42 -0700250// This is a BSDism that we never implemented correctly. Used by Firefox.
251extern "C" int issetugid() {
252 return 0;
253}
254
Dan Albert8229ae42014-06-13 16:04:41 -0700255// This was removed from POSIX 2004.
256extern "C" pid_t wait3(int* status, int options, struct rusage* rusage) {
257 return wait4(-1, status, options, rusage);
258}
259
Dan Albert462abab2014-06-13 16:51:24 -0700260// This was removed from POSIX 2004.
261extern "C" int getdtablesize() {
262 struct rlimit r;
263
264 if (getrlimit(RLIMIT_NOFILE, &r) < 0) {
265 return sysconf(_SC_OPEN_MAX);
266 }
267
268 return r.rlim_cur;
269}
270
Elliott Hughes1628eb12014-08-06 10:47:33 -0700271// Only used by ftime, which was removed from POSIX 2008.
Dan Albertac646752014-06-05 02:10:49 +0000272struct timeb {
273 time_t time;
274 unsigned short millitm;
275 short timezone;
276 short dstflag;
277};
278
279// This was removed from POSIX 2008.
280extern "C" int ftime(struct timeb* tb) {
281 struct timeval tv;
282 struct timezone tz;
283
284 if (gettimeofday(&tv, &tz) < 0)
285 return -1;
286
287 tb->time = tv.tv_sec;
288 tb->millitm = (tv.tv_usec + 500) / 1000;
289
290 if (tb->millitm == 1000) {
291 ++tb->time;
292 tb->millitm = 0;
293 }
294
295 tb->timezone = tz.tz_minuteswest;
296 tb->dstflag = tz.tz_dsttime;
297
298 return 0;
299}
300
David 'Digit' Turner891dedb2014-06-13 12:28:11 +0200301// This was removed from POSIX 2008.
302extern "C" char* index(const char* str, int ch) {
303 return strchr(str, ch);
304}
305
Elliott Hughes567a8de2013-10-24 17:14:55 -0700306#endif