1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef __LIB_UTILS_COMPAT_H
18 #define __LIB_UTILS_COMPAT_H
19
20 #include <unistd.h>
21
22 /* Compatibility definitions for non-Linux (i.e., BSD-based) hosts. */
23 #ifndef HAVE_OFF64_T
24 #if _FILE_OFFSET_BITS < 64
25 #error "_FILE_OFFSET_BITS < 64; large files are not supported on this platform"
26 #endif /* _FILE_OFFSET_BITS < 64 */
27
28 typedef off_t off64_t;
29
lseek64(int fd,off64_t offset,int whence)30 static inline off64_t lseek64(int fd, off64_t offset, int whence) {
31 return lseek(fd, offset, whence);
32 }
33
34 #ifdef HAVE_PREAD
pread64(int fd,void * buf,size_t nbytes,off64_t offset)35 static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
36 return pread(fd, buf, nbytes, offset);
37 }
38 #endif
39
40 #endif /* !HAVE_OFF64_T */
41
42 #if HAVE_PRINTF_ZD
43 # define ZD "%zd"
44 # define ZD_TYPE ssize_t
45 #else
46 # define ZD "%ld"
47 # define ZD_TYPE long
48 #endif
49
50 /*
51 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
52 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
53 * not already defined, then define it here.
54 */
55 #ifndef TEMP_FAILURE_RETRY
56 /* Used to retry syscalls that can return EINTR. */
57 #define TEMP_FAILURE_RETRY(exp) ({ \
58 typeof (exp) _rc; \
59 do { \
60 _rc = (exp); \
61 } while (_rc == -1 && errno == EINTR); \
62 _rc; })
63 #endif
64
65 #endif /* __LIB_UTILS_COMPAT_H */
66