1e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root/*
2e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * Copyright (C) 2010 The Android Open Source Project
3e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root *
4e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * Licensed under the Apache License, Version 2.0 (the "License");
5e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * you may not use this file except in compliance with the License.
6e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * You may obtain a copy of the License at
7e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root *
8e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root *      http://www.apache.org/licenses/LICENSE-2.0
9e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root *
10e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * Unless required by applicable law or agreed to in writing, software
11e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * distributed under the License is distributed on an "AS IS" BASIS,
12e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * See the License for the specific language governing permissions and
14e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root * limitations under the License.
15e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root */
16e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
17e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root#ifndef __LIB_UTILS_COMPAT_H
18e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root#define __LIB_UTILS_COMPAT_H
19e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
20e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root#include <unistd.h>
21e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
22cb3d65323d69ee1c101a1fa04e0d0e0929ef0f4fElliott Hughes#if defined(__APPLE__)
23cb3d65323d69ee1c101a1fa04e0d0e0929ef0f4fElliott Hughes
24cb3d65323d69ee1c101a1fa04e0d0e0929ef0f4fElliott Hughes/* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
25e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
26e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Roottypedef off_t off64_t;
27e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
28e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Rootstatic inline off64_t lseek64(int fd, off64_t offset, int whence) {
29e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root    return lseek(fd, offset, whence);
30e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root}
31e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
32e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Rootstatic inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
33e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root    return pread(fd, buf, nbytes, offset);
34e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root}
35e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
36cb3d65323d69ee1c101a1fa04e0d0e0929ef0f4fElliott Hughes#endif /* __APPLE__ */
37e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
38714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#if defined(_WIN32)
39c6b30f376d1aa6113bfd8b8cbafe883c21ae5203Dan Albert#define O_CLOEXEC O_NOINHERIT
40714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#define O_NOFOLLOW 0
41714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#define DEFFILEMODE 0666
42714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#endif /* _WIN32 */
43714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes
44146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#if defined(_WIN32)
45146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#define ZD "%ld"
46146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#define ZD_TYPE long
47b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#else
48146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#define ZD "%zd"
49146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#define ZD_TYPE ssize_t
50b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#endif
51b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root
52b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root/*
53e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert * Needed for cases where something should be constexpr if possible, but not
54e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert * being constexpr is fine if in pre-C++11 code (such as a const static float
55e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert * member variable).
56e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert */
57e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#if __cplusplus >= 201103L
58e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#define CONSTEXPR constexpr
59e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#else
60e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#define CONSTEXPR
61e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#endif
62e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert
63e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert/*
64b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
65b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
66b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root * not already defined, then define it here.
67b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root */
68b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#ifndef TEMP_FAILURE_RETRY
69b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root/* Used to retry syscalls that can return EINTR. */
70b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#define TEMP_FAILURE_RETRY(exp) ({         \
71b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    typeof (exp) _rc;                      \
72b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    do {                                   \
73b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root        _rc = (exp);                       \
74b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    } while (_rc == -1 && errno == EINTR); \
75b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    _rc; })
76b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#endif
77b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root
78e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root#endif /* __LIB_UTILS_COMPAT_H */
79