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