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