ndk_cruft.cpp revision eae5902e73dc4381811e08fd2334bf4a9300a928
1/*
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
32#include <ctype.h>
33#include <inttypes.h>
34#include <pthread.h>
35#include <stdlib.h>
36#include <sys/resource.h>
37#include <sys/time.h>
38#include <sys/types.h>
39#include <sys/wait.h>
40#include <unistd.h>
41
42// These were accidentally declared in <unistd.h> because we stupidly used to inline
43// getpagesize() and __getpageshift(). Needed for backwards compatibility with old NDK apps.
44extern "C" {
45  unsigned int __page_size = PAGE_SIZE;
46  unsigned int __page_shift = 12;
47}
48
49// TODO: remove this backward compatibility hack (for jb-mr1 strace binaries).
50extern "C" pid_t __wait4(pid_t pid, int* status, int options, struct rusage* rusage) {
51  return wait4(pid, status, options, rusage);
52}
53
54// TODO: does anything still need this?
55extern "C" int __open() {
56  abort();
57}
58
59// TODO: does anything still need this?
60extern "C" void** __get_tls() {
61#include "private/__get_tls.h"
62  return __get_tls();
63}
64
65// This non-standard function was in our <string.h> for some reason.
66extern "C" void memswap(void* m1, void* m2, size_t n) {
67  char* p = reinterpret_cast<char*>(m1);
68  char* p_end = p + n;
69  char* q = reinterpret_cast<char*>(m2);
70  while (p < p_end) {
71    char tmp = *p;
72    *p = *q;
73    *q = tmp;
74    p++;
75    q++;
76  }
77}
78
79extern "C" int pthread_attr_setstackaddr(pthread_attr_t*, void*) {
80  // This was removed from POSIX.1-2008, and is not implemented on bionic.
81  // Needed for ABI compatibility with the NDK.
82  return ENOSYS;
83}
84
85extern "C" int pthread_attr_getstackaddr(const pthread_attr_t* attr, void** stack_addr) {
86  // This was removed from POSIX.1-2008.
87  // Needed for ABI compatibility with the NDK.
88  *stack_addr = (char*)attr->stack_base + attr->stack_size;
89  return 0;
90}
91
92// Non-standard cruft that should only ever have been in system/core/toolbox.
93extern "C" char* strtotimeval(const char* str, struct timeval* ts) {
94  char* s;
95  ts->tv_sec = strtoumax(str, &s, 10);
96
97  long fractional_seconds = 0;
98  if (*s == '.') {
99    s++;
100    int count = 0;
101
102    // Read up to 6 digits (microseconds).
103    while (*s && isdigit(*s)) {
104      if (++count < 7) {
105        fractional_seconds = fractional_seconds*10 + (*s - '0');
106      }
107      s++;
108    }
109
110    for (; count < 6; count++) {
111      fractional_seconds *= 10;
112    }
113  }
114
115  ts->tv_usec = fractional_seconds;
116  return s;
117}
118
119static inline int digitval(int ch) {
120  unsigned d;
121
122  d = (unsigned)(ch - '0');
123  if (d < 10) return (int)d;
124
125  d = (unsigned)(ch - 'a');
126  if (d < 6) return (int)(d+10);
127
128  d = (unsigned)(ch - 'A');
129  if (d < 6) return (int)(d+10);
130
131  return -1;
132}
133
134// This non-standard function was in our <inttypes.h> for some reason.
135extern "C" uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n) {
136  const unsigned char*  p   = (const unsigned char *)nptr;
137  const unsigned char*  end = p + n;
138  int                   minus = 0;
139  uintmax_t             v = 0;
140  int                   d;
141
142  while (p < end && isspace(*p)) {
143    p++;
144  }
145
146  if (p < end) {
147    char c = p[0];
148    if (c == '-' || c == '+') {
149      minus = (c == '-');
150      p++;
151    }
152  }
153
154  if (base == 0) {
155    if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
156      p += 2;
157      base = 16;
158    } else if (p+1 < end && p[0] == '0') {
159      p   += 1;
160      base = 8;
161    } else {
162      base = 10;
163    }
164  } else if (base == 16) {
165    if (p+2 < end && p[0] == '0' && (p[1] == 'x' || p[1] == 'X')) {
166      p += 2;
167    }
168  }
169
170  while (p < end && (d = digitval(*p)) >= 0 && d < base) {
171    v = v*base + d;
172    p += 1;
173  }
174
175  if (endptr) {
176    *endptr = (char*) p;
177  }
178
179  return minus ? -v : v;
180}
181
182// This non-standard function was in our <inttypes.h> for some reason.
183extern "C" intmax_t strntoimax(const char* nptr, char** endptr, int base, size_t n) {
184  return (intmax_t) strntoumax(nptr, endptr, base, n);
185}
186
187#endif
188