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
368731d30085b02012487552d8ad6d95c0ac9a8d4dSami Tolvanenstatic inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t offset) {
378731d30085b02012487552d8ad6d95c0ac9a8d4dSami Tolvanen    return pwrite(fd, buf, nbytes, offset);
388731d30085b02012487552d8ad6d95c0ac9a8d4dSami Tolvanen}
398731d30085b02012487552d8ad6d95c0ac9a8d4dSami Tolvanen
40b02d690336a0fb7b26338c5f663397debcbf99e2Adam Lesinskistatic inline int ftruncate64(int fd, off64_t length) {
41b02d690336a0fb7b26338c5f663397debcbf99e2Adam Lesinski    return ftruncate(fd, length);
42b02d690336a0fb7b26338c5f663397debcbf99e2Adam Lesinski}
43b02d690336a0fb7b26338c5f663397debcbf99e2Adam Lesinski
44cb3d65323d69ee1c101a1fa04e0d0e0929ef0f4fElliott Hughes#endif /* __APPLE__ */
45e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root
46714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#if defined(_WIN32)
47c6b30f376d1aa6113bfd8b8cbafe883c21ae5203Dan Albert#define O_CLOEXEC O_NOINHERIT
48714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#define O_NOFOLLOW 0
49714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#define DEFFILEMODE 0666
50714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes#endif /* _WIN32 */
51714196d05c5952eb09ca718955de6e4c2d4cfd92Elliott Hughes
52146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#define ZD "%zd"
53146c24461395d0752f9e79973827f78d084f7d6fElliott Hughes#define ZD_TYPE ssize_t
54b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root
55b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root/*
56e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert * Needed for cases where something should be constexpr if possible, but not
57e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert * being constexpr is fine if in pre-C++11 code (such as a const static float
58e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert * member variable).
59e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert */
60e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#if __cplusplus >= 201103L
61e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#define CONSTEXPR constexpr
62e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#else
63e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#define CONSTEXPR
64e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert#endif
65e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert
66e4c649c9fca659fda908b0d4608feedd434b5656Dan Albert/*
67b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
68b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
69b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root * not already defined, then define it here.
70b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root */
71b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#ifndef TEMP_FAILURE_RETRY
72b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root/* Used to retry syscalls that can return EINTR. */
73b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#define TEMP_FAILURE_RETRY(exp) ({         \
74b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    typeof (exp) _rc;                      \
75b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    do {                                   \
76b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root        _rc = (exp);                       \
77b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    } while (_rc == -1 && errno == EINTR); \
78b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root    _rc; })
79b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root#endif
80b9fd6f986492d74125274722b8ea1a3819cd216aKenny Root
811f8bc86a7d1c49cd666c5f80d1507e6351a65aa0Elliott Hughes#if defined(_WIN32)
821f8bc86a7d1c49cd666c5f80d1507e6351a65aa0Elliott Hughes#define OS_PATH_SEPARATOR '\\'
831f8bc86a7d1c49cd666c5f80d1507e6351a65aa0Elliott Hughes#else
841f8bc86a7d1c49cd666c5f80d1507e6351a65aa0Elliott Hughes#define OS_PATH_SEPARATOR '/'
851f8bc86a7d1c49cd666c5f80d1507e6351a65aa0Elliott Hughes#endif
861f8bc86a7d1c49cd666c5f80d1507e6351a65aa0Elliott Hughes
87e2fa7dc58eaf34f30b89350d143d97fd4a501199Kenny Root#endif /* __LIB_UTILS_COMPAT_H */
88