1+ #ifndef ANDROID_COMPAT_H
2+ #define ANDROID_COMPAT_H
3+
4+ #ifdef __ANDROID__
5+
6+ #include <fcntl.h>
7+ #include <sys/stat.h>
8+ #include <unistd.h>
9+ #include <string.h>
10+ #include <errno.h>
11+ #include <stdio.h>
12+
13+ static inline const char * __android_shm_tmpdir (void )
14+ {
15+ const char * dir = getenv ("FAKETIME_SHM_DIR" );
16+ if (dir == NULL )
17+ dir = getenv ("TMPDIR" );
18+ if (dir == NULL )
19+ dir = "/data/local/tmp" ;
20+ return dir ;
21+ }
22+
23+ static inline int __android_shm_open (const char * name , int oflag , mode_t mode )
24+ {
25+ char path [512 ];
26+ const char * dir = __android_shm_tmpdir ();
27+ while (* name == '/' ) name ++ ;
28+ snprintf (path , sizeof (path ), "%s/faketime_shm_%s" , dir , name );
29+ int fd = open (path , oflag , mode );
30+ if (fd == -1 && (oflag & O_CREAT ))
31+ {
32+ mkdir (dir , 0777 );
33+ fd = open (path , oflag , mode );
34+ }
35+ return fd ;
36+ }
37+
38+ static inline int __android_shm_unlink (const char * name )
39+ {
40+ char path [512 ];
41+ const char * dir = __android_shm_tmpdir ();
42+ while (* name == '/' ) name ++ ;
43+ snprintf (path , sizeof (path ), "%s/faketime_shm_%s" , dir , name );
44+ return unlink (path );
45+ }
46+
47+ #define shm_open __android_shm_open
48+ #define shm_unlink __android_shm_unlink
49+
50+ #endif /* __ANDROID__ */
51+ #endif /* ANDROID_COMPAT_H */
0 commit comments