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