Home | History | Annotate | Download | only in utils
      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 #if defined(__APPLE__)
     23 
     24 /* Mac OS has always had a 64-bit off_t, so it doesn't have off64_t. */
     25 
     26 typedef off_t off64_t;
     27 
     28 static inline off64_t lseek64(int fd, off64_t offset, int whence) {
     29     return lseek(fd, offset, whence);
     30 }
     31 
     32 static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) {
     33     return pread(fd, buf, nbytes, offset);
     34 }
     35 
     36 static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t offset) {
     37     return pwrite(fd, buf, nbytes, offset);
     38 }
     39 
     40 static inline int ftruncate64(int fd, off64_t length) {
     41     return ftruncate(fd, length);
     42 }
     43 
     44 #endif /* __APPLE__ */
     45 
     46 #if defined(_WIN32)
     47 #define O_CLOEXEC O_NOINHERIT
     48 #define O_NOFOLLOW 0
     49 #define DEFFILEMODE 0666
     50 #endif /* _WIN32 */
     51 
     52 #define ZD "%zd"
     53 #define ZD_TYPE ssize_t
     54 
     55 /*
     56  * Needed for cases where something should be constexpr if possible, but not
     57  * being constexpr is fine if in pre-C++11 code (such as a const static float
     58  * member variable).
     59  */
     60 #if __cplusplus >= 201103L
     61 #define CONSTEXPR constexpr
     62 #else
     63 #define CONSTEXPR
     64 #endif
     65 
     66 /*
     67  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
     68  * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
     69  * not already defined, then define it here.
     70  */
     71 #ifndef TEMP_FAILURE_RETRY
     72 /* Used to retry syscalls that can return EINTR. */
     73 #define TEMP_FAILURE_RETRY(exp) ({         \
     74     typeof (exp) _rc;                      \
     75     do {                                   \
     76         _rc = (exp);                       \
     77     } while (_rc == -1 && errno == EINTR); \
     78     _rc; })
     79 #endif
     80 
     81 #if defined(_WIN32)
     82 #define OS_PATH_SEPARATOR '\\'
     83 #else
     84 #define OS_PATH_SEPARATOR '/'
     85 #endif
     86 
     87 #endif /* __LIB_UTILS_COMPAT_H */
     88