Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / build_common / android / compatibility / c_compat.c
1 #include <stdlib.h>
2 #include <sys/socket.h>
3
4 /* from stdlib.h */
5 float strtof(const char *nptr, char **endptr)
6 {
7     return (float)strtod(nptr, endptr);
8 }
9
10 double atof(const char *nptr)
11 {
12     return strtod(nptr, NULL);
13 }
14
15 int abs(int __n)
16 {
17     return (__n < 0) ? -__n : __n;
18 }
19
20 long labs(long __n)
21 {
22     return (__n < 0L) ? -__n : __n;
23 }
24
25 long long llabs(long long __n)
26 {
27     return (__n < 0LL) ? -__n : __n;
28 }
29
30 int rand(void)
31 {
32     return (int)lrand48();
33 }
34
35 void srand(unsigned int __s)
36 {
37     srand48(__s);
38 }
39
40 long random(void)
41 {
42     return lrand48();
43 }
44
45 void srandom(unsigned int __s)
46 {
47     srand48(__s);
48 }
49
50 /* from __cmsg_nxthdr.cpp */
51 /*
52  * The function __cmsg_nxthd() is missing in Android 4.4, but the Android NDK
53  * header files in the version we are using are referencing it and we use it in
54  * our code, this functions was added in version 5.0. To make IoTivity
55  * dynamically loadable at load time on Android KitKat 4.4 add this functions
56  * as a weak symbol, so it will be used if the c lib does not provide it, like
57  * on Android < 5.0 This code was taken from these two resources:
58  * https://raw.githubusercontent.com/android/platform_bionic/master/libc/bionic/__cmsg_nxthdr.cpp
59  * https://github.com/android/platform_bionic/commit/ff64831b0965c16c95c9f81a148f30a6ef3a6c64
60  */
61 struct cmsghdr* __attribute__((weak)) __cmsg_nxthdr(struct msghdr* msg, struct cmsghdr* cmsg)
62 {
63     struct cmsghdr* ptr;
64     ptr = (struct cmsghdr*)(((unsigned char*) cmsg) + CMSG_ALIGN(cmsg->cmsg_len));
65     size_t len = (unsigned long)((char*)(ptr+1) - (char*) msg->msg_control);
66     if (len > msg->msg_controllen) {
67         return NULL;
68     }
69     return ptr;
70 }