annotate win32-mingw/include/kj/miniposix.h @ 50:37d53a7e8262

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