annotate osx/include/kj/miniposix.h @ 58:eab3b14ddc95

Further win32 build updates
author Chris Cannam
date Mon, 09 Jan 2017 13:51:38 +0000
parents 3ab5a40c4e3b
children
rev   line source
cannam@49 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@49 2 // Licensed under the MIT License:
cannam@49 3 //
cannam@49 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@49 5 // of this software and associated documentation files (the "Software"), to deal
cannam@49 6 // in the Software without restriction, including without limitation the rights
cannam@49 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@49 8 // copies of the Software, and to permit persons to whom the Software is
cannam@49 9 // furnished to do so, subject to the following conditions:
cannam@49 10 //
cannam@49 11 // The above copyright notice and this permission notice shall be included in
cannam@49 12 // all copies or substantial portions of the Software.
cannam@49 13 //
cannam@49 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@49 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@49 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@49 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@49 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@49 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@49 20 // THE SOFTWARE.
cannam@49 21
cannam@49 22 #ifndef KJ_MINIPOSIX_H_
cannam@49 23 #define KJ_MINIPOSIX_H_
cannam@49 24
cannam@49 25 // This header provides a small subset of the POSIX API which also happens to be available on
cannam@49 26 // Windows under slightly-different names.
cannam@49 27
cannam@49 28 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@49 29 #pragma GCC system_header
cannam@49 30 #endif
cannam@49 31
cannam@49 32 #if _WIN32
cannam@49 33 #include <io.h>
cannam@49 34 #include <direct.h>
cannam@49 35 #include <fcntl.h> // _O_BINARY
cannam@49 36 #else
cannam@49 37 #include <limits.h>
cannam@49 38 #include <errno.h>
cannam@49 39 #endif
cannam@49 40
cannam@49 41 #if !_WIN32 || __MINGW32__
cannam@49 42 #include <unistd.h>
cannam@49 43 #include <sys/stat.h>
cannam@49 44 #include <sys/types.h>
cannam@49 45 #endif
cannam@49 46
cannam@49 47 #if !_WIN32
cannam@49 48 #include <sys/uio.h>
cannam@49 49 #endif
cannam@49 50
cannam@49 51 namespace kj {
cannam@49 52 namespace miniposix {
cannam@49 53
cannam@49 54 #if _WIN32 && !__MINGW32__
cannam@49 55 // We're on Windows and not MinGW. So, we need to define wrappers for the POSIX API.
cannam@49 56
cannam@49 57 typedef int ssize_t;
cannam@49 58
cannam@49 59 inline ssize_t read(int fd, void* buffer, size_t size) {
cannam@49 60 return ::_read(fd, buffer, size);
cannam@49 61 }
cannam@49 62 inline ssize_t write(int fd, const void* buffer, size_t size) {
cannam@49 63 return ::_write(fd, buffer, size);
cannam@49 64 }
cannam@49 65 inline int close(int fd) {
cannam@49 66 return ::_close(fd);
cannam@49 67 }
cannam@49 68
cannam@49 69 #ifndef F_OK
cannam@49 70 #define F_OK 0 // access() existence test
cannam@49 71 #endif
cannam@49 72
cannam@49 73 #ifndef S_ISREG
cannam@49 74 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) // stat() regular file test
cannam@49 75 #endif
cannam@49 76 #ifndef S_ISDIR
cannam@49 77 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) // stat() directory test
cannam@49 78 #endif
cannam@49 79
cannam@49 80 #ifndef STDIN_FILENO
cannam@49 81 #define STDIN_FILENO 0
cannam@49 82 #endif
cannam@49 83 #ifndef STDOUT_FILENO
cannam@49 84 #define STDOUT_FILENO 1
cannam@49 85 #endif
cannam@49 86 #ifndef STDERR_FILENO
cannam@49 87 #define STDERR_FILENO 2
cannam@49 88 #endif
cannam@49 89
cannam@49 90 #else
cannam@49 91 // We're on a POSIX system or MinGW which already defines the wrappers for us.
cannam@49 92
cannam@49 93 using ::ssize_t;
cannam@49 94 using ::read;
cannam@49 95 using ::write;
cannam@49 96 using ::close;
cannam@49 97
cannam@49 98 #endif
cannam@49 99
cannam@49 100 #if _WIN32
cannam@49 101 // We're on Windows, including MinGW. pipe() and mkdir() are non-standard even on MinGW.
cannam@49 102
cannam@49 103 inline int pipe(int fds[2]) {
cannam@49 104 return ::_pipe(fds, 8192, _O_BINARY);
cannam@49 105 }
cannam@49 106 inline int mkdir(const char* path, int mode) {
cannam@49 107 return ::_mkdir(path);
cannam@49 108 }
cannam@49 109
cannam@49 110 #else
cannam@49 111 // We're on real POSIX.
cannam@49 112
cannam@49 113 using ::pipe;
cannam@49 114 using ::mkdir;
cannam@49 115
cannam@49 116 inline size_t iovMax(size_t count) {
cannam@49 117 // Apparently, there is a maximum number of iovecs allowed per call. I don't understand why.
cannam@49 118 // Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd,
cannam@49 119 // define neither.
cannam@49 120 //
cannam@49 121 // On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX),
cannam@49 122 // then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails.
cannam@49 123 //
cannam@49 124 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01
cannam@49 125
cannam@49 126 #if defined(IOV_MAX)
cannam@49 127 // Solaris (and others?)
cannam@49 128 return IOV_MAX;
cannam@49 129 #elif defined(UIO_MAXIOV)
cannam@49 130 // Linux
cannam@49 131 return UIO_MAXIOV;
cannam@49 132 #else
cannam@49 133 // POSIX mystery meat
cannam@49 134
cannam@49 135 long iovmax;
cannam@49 136
cannam@49 137 errno = 0;
cannam@49 138 if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) {
cannam@49 139 // assume iovmax == -1 && errno == 0 means "unbounded"
cannam@49 140 return errno ? _XOPEN_IOV_MAX : count;
cannam@49 141 } else {
cannam@49 142 return (size_t) iovmax;
cannam@49 143 }
cannam@49 144 #endif
cannam@49 145 }
cannam@49 146
cannam@49 147 #endif
cannam@49 148
cannam@49 149 } // namespace miniposix
cannam@49 150 } // namespace kj
cannam@49 151
cannam@49 152 #endif // KJ_MINIPOSIX_H_