annotate win32-mingw/include/kj/miniposix.h @ 150:0a1a4a299a5d

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