Chris@63: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@63: // Licensed under the MIT License: Chris@63: // Chris@63: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@63: // of this software and associated documentation files (the "Software"), to deal Chris@63: // in the Software without restriction, including without limitation the rights Chris@63: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@63: // copies of the Software, and to permit persons to whom the Software is Chris@63: // furnished to do so, subject to the following conditions: Chris@63: // Chris@63: // The above copyright notice and this permission notice shall be included in Chris@63: // all copies or substantial portions of the Software. Chris@63: // Chris@63: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@63: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@63: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@63: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@63: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@63: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@63: // THE SOFTWARE. Chris@63: Chris@63: #ifndef KJ_MINIPOSIX_H_ Chris@63: #define KJ_MINIPOSIX_H_ Chris@63: Chris@63: // This header provides a small subset of the POSIX API which also happens to be available on Chris@63: // Windows under slightly-different names. Chris@63: Chris@63: #if defined(__GNUC__) && !KJ_HEADER_WARNINGS Chris@63: #pragma GCC system_header Chris@63: #endif Chris@63: Chris@63: #if _WIN32 Chris@63: #include Chris@63: #include Chris@63: #include // _O_BINARY Chris@63: #else Chris@63: #include Chris@63: #include Chris@63: #endif Chris@63: Chris@63: #if !_WIN32 || __MINGW32__ Chris@63: #include Chris@63: #include Chris@63: #include Chris@63: #endif Chris@63: Chris@63: #if !_WIN32 Chris@63: #include Chris@63: #endif Chris@63: Chris@63: namespace kj { Chris@63: namespace miniposix { Chris@63: Chris@63: #if _WIN32 && !__MINGW32__ Chris@63: // We're on Windows and not MinGW. So, we need to define wrappers for the POSIX API. Chris@63: Chris@63: typedef int ssize_t; Chris@63: Chris@63: inline ssize_t read(int fd, void* buffer, size_t size) { Chris@63: return ::_read(fd, buffer, size); Chris@63: } Chris@63: inline ssize_t write(int fd, const void* buffer, size_t size) { Chris@63: return ::_write(fd, buffer, size); Chris@63: } Chris@63: inline int close(int fd) { Chris@63: return ::_close(fd); Chris@63: } Chris@63: Chris@63: #ifndef F_OK Chris@63: #define F_OK 0 // access() existence test Chris@63: #endif Chris@63: Chris@63: #ifndef S_ISREG Chris@63: #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) // stat() regular file test Chris@63: #endif Chris@63: #ifndef S_ISDIR Chris@63: #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) // stat() directory test Chris@63: #endif Chris@63: Chris@63: #ifndef STDIN_FILENO Chris@63: #define STDIN_FILENO 0 Chris@63: #endif Chris@63: #ifndef STDOUT_FILENO Chris@63: #define STDOUT_FILENO 1 Chris@63: #endif Chris@63: #ifndef STDERR_FILENO Chris@63: #define STDERR_FILENO 2 Chris@63: #endif Chris@63: Chris@63: #else Chris@63: // We're on a POSIX system or MinGW which already defines the wrappers for us. Chris@63: Chris@63: using ::ssize_t; Chris@63: using ::read; Chris@63: using ::write; Chris@63: using ::close; Chris@63: Chris@63: #endif Chris@63: Chris@63: #if _WIN32 Chris@63: // We're on Windows, including MinGW. pipe() and mkdir() are non-standard even on MinGW. Chris@63: Chris@63: inline int pipe(int fds[2]) { Chris@63: return ::_pipe(fds, 8192, _O_BINARY); Chris@63: } Chris@63: inline int mkdir(const char* path, int mode) { Chris@63: return ::_mkdir(path); Chris@63: } Chris@63: Chris@63: #else Chris@63: // We're on real POSIX. Chris@63: Chris@63: using ::pipe; Chris@63: using ::mkdir; Chris@63: Chris@63: inline size_t iovMax(size_t count) { Chris@63: // Apparently, there is a maximum number of iovecs allowed per call. I don't understand why. Chris@63: // Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd, Chris@63: // define neither. Chris@63: // Chris@63: // On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX), Chris@63: // then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails. Chris@63: // Chris@63: // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01 Chris@63: Chris@63: #if defined(IOV_MAX) Chris@63: // Solaris (and others?) Chris@63: return IOV_MAX; Chris@63: #elif defined(UIO_MAXIOV) Chris@63: // Linux Chris@63: return UIO_MAXIOV; Chris@63: #else Chris@63: // POSIX mystery meat Chris@63: Chris@63: long iovmax; Chris@63: Chris@63: errno = 0; Chris@63: if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) { Chris@63: // assume iovmax == -1 && errno == 0 means "unbounded" Chris@63: return errno ? _XOPEN_IOV_MAX : count; Chris@63: } else { Chris@63: return (size_t) iovmax; Chris@63: } Chris@63: #endif Chris@63: } Chris@63: Chris@63: #endif Chris@63: Chris@63: } // namespace miniposix Chris@63: } // namespace kj Chris@63: Chris@63: #endif // KJ_MINIPOSIX_H_