annotate win32-mingw/include/kj/miniposix.h @ 142:75bf92aa2d1f

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