comparison osx/include/kj/miniposix.h @ 134:41e769c91eca

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