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#pragma once
23
24// This header provides a small subset of the POSIX API which also happens to be available on
25// Windows under slightly-different names.
26
27#if defined(__GNUC__) && !KJ_HEADER_WARNINGS
28#pragma GCC system_header
29#endif
30
31#if _WIN32
32#include <io.h>
33#include <direct.h>
34#include <fcntl.h> // _O_BINARY
35#else
36#include <limits.h>
37#include <errno.h>
38#endif
39
40#if !_WIN32 || __MINGW32__
41#include <unistd.h>
42#include <sys/stat.h>
43#include <sys/types.h>
44#endif
45
46#if !_WIN32
47#include <sys/uio.h>
48#endif
49
50namespace kj {
51namespace miniposix {
52
53#if _WIN32 && !__MINGW32__
54// We're on Windows and not MinGW. So, we need to define wrappers for the POSIX API.
55
56typedef int ssize_t;
57
58inline ssize_t read(int fd, void* buffer, size_t size) {
59 return ::_read(fd, buffer, size);
60}
61inline ssize_t write(int fd, const void* buffer, size_t size) {
62 return ::_write(fd, buffer, size);
63}
64inline int close(int fd) {
65 return ::_close(fd);
66}
67
68#ifndef F_OK
69#define F_OK 0 // access() existence test
70#endif
71
72#ifndef S_ISREG
73#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) // stat() regular file test
74#endif
75#ifndef S_ISDIR
76#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) // stat() directory test
77#endif
78
79#ifndef STDIN_FILENO
80#define STDIN_FILENO 0
81#endif
82#ifndef STDOUT_FILENO
83#define STDOUT_FILENO 1
84#endif
85#ifndef STDERR_FILENO
86#define STDERR_FILENO 2
87#endif
88
89#else
90// We're on a POSIX system or MinGW which already defines the wrappers for us.
91
92using ::ssize_t;
93using ::read;
94using ::write;
95using ::close;
96
97#endif
98
99#if _WIN32
100// We're on Windows, including MinGW. pipe() and mkdir() are non-standard even on MinGW.
101
102inline int pipe(int fds[2]) {
103 return ::_pipe(fds, 8192, _O_BINARY | _O_NOINHERIT);
104}
105inline int mkdir(const char* path, int mode) {
106 return ::_mkdir(path);
107}
108
109#else
110// We're on real POSIX.
111
112using ::pipe;
113using ::mkdir;
114
115inline size_t iovMax(size_t count) {
116 // Apparently, there is a maximum number of iovecs allowed per call. I don't understand why.
117 // Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd,
118 // define neither.
119 //
120 // On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX),
121 // then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails.
122 //
123 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01
124
125#if defined(IOV_MAX)
126 // Solaris (and others?)
127 return IOV_MAX;
128#elif defined(UIO_MAXIOV)
129 // Linux
130 return UIO_MAXIOV;
131#else
132 // POSIX mystery meat
133
134 long iovmax;
135
136 errno = 0;
137 if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) {
138 // assume iovmax == -1 && errno == 0 means "unbounded"
139 return errno ? _XOPEN_IOV_MAX : count;
140 } else {
141 return (size_t) iovmax;
142 }
143#endif
144}
145
146#endif
147
148} // namespace miniposix
149} // namespace kj
150