miniposix.h 3.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef KJ_PORTABLE_FD_H_
#define KJ_PORTABLE_FD_H_

// This header provides a small subset of the POSIX API which also happens to be available on
// Windows under slightly-different names.

#if defined(__GNUC__) && !KJ_HEADER_WARNINGS
#pragma GCC system_header
#endif

#if _WIN32
#include <io.h>
#include <direct.h>
Tom Lee's avatar
Tom Lee committed
35 36 37
#else
#include <limits.h>
#include <errno.h>
Kenton Varda's avatar
Kenton Varda committed
38 39 40
#endif

#if !_WIN32 || __MINGW32__
41 42 43 44 45 46 47 48
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#endif

namespace kj {
namespace miniposix {

Kenton Varda's avatar
Kenton Varda committed
49
#if _WIN32 && !__MINGW32__
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
// We're on Windows and not MinGW. So, we need to define wrappers for the POSIX API.

typedef int ssize_t;

inline ssize_t read(int fd, void* buffer, size_t size) {
  return ::_read(fd, buffer, size);
}
inline ssize_t write(int fd, const void* buffer, size_t size) {
  return ::_write(fd, buffer, size);
}
inline int close(int fd) {
  return ::_close(fd);
}

#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif

#else
// We're on a POSIX system or MinGW which already defines the wrappers for us.

using ::ssize_t;
using ::read;
using ::write;
using ::close;
Kenton Varda's avatar
Kenton Varda committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

#endif

#if _WIN32
// We're on Windows, including MinGW. pipe() and mkdir() are non-standard even on MinGW.

inline int pipe(int fds[2]) {
  return ::_pipe(fds, 4096, false);
}
inline int mkdir(const char* path, int mode) {
  return ::_mkdir(path);
}

#else
// We're on real POSIX.

97 98 99
using ::pipe;
using ::mkdir;

Tom Lee's avatar
Tom Lee committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
inline size_t iovMax(size_t count) {
  // Apparently, there is a maximum number of iovecs allowed per call.  I don't understand why.
  // Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd,
  // define neither.
  //
  // On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX),
  // then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails.
  //
  // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01

#if defined(IOV_MAX)
  // Solaris (and others?)
  return IOV_MAX;
#elif defined(UIO_MAXIOV)
  // Linux
  return UIO_MAXIOV;
#else
  // POSIX mystery meat

  long iovmax;

  errno = 0;
  if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) {
    // assume iovmax == -1 && errno == 0 means "unbounded"
    return errno ? _XOPEN_IOV_MAX : count;
  } else {
    return (size_t) iovmax;
  }
#endif
}

131 132 133 134 135 136
#endif

}  // namespace miniposix
}  // namespace kj

#endif  // KJ_WIN32_FD_H_