Commit d3bf3b7d authored by Kirill Smelkov's avatar Kirill Smelkov

internal/syscall: Initial adjust to support MSVC

- there is no strerror_r, but strerror_s instead
- fcntl is not supported
- we need to explicitly tell open to open files in binary mode
- pipes are created via _pipe and explicitly in binary mode
- there is no sigaction.
parent 63b5c1d9
...@@ -57,15 +57,18 @@ string _Errno::Error() { ...@@ -57,15 +57,18 @@ string _Errno::Error() {
_Errno& e = *this; _Errno& e = *this;
char ebuf[128]; char ebuf[128];
bool ok;
#if __APPLE__ #if __APPLE__
int x = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf)); ok = (::strerror_r(-e.syserr, ebuf, sizeof(ebuf)) == 0);
if (x == 0) #elif defined(_WIN32)
return string(ebuf); ok = (::strerror_s(ebuf, sizeof(ebuf), -e.syserr) == 0);
return "unknown error " + std::to_string(-e.syserr);
#else #else
char *estr = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf)); char *estr = ::strerror_r(-e.syserr, ebuf, sizeof(ebuf));
return string(estr); return string(estr);
#endif #endif
if (ok)
return string(ebuf);
return "unknown error " + std::to_string(-e.syserr);
} }
...@@ -99,6 +102,7 @@ __Errno Close(int fd) { ...@@ -99,6 +102,7 @@ __Errno Close(int fd) {
return err; return err;
} }
#ifndef _WIN32
__Errno Fcntl(int fd, int cmd, int arg) { __Errno Fcntl(int fd, int cmd, int arg) {
int save_errno = errno; int save_errno = errno;
int err = ::fcntl(fd, cmd, arg); int err = ::fcntl(fd, cmd, arg);
...@@ -107,6 +111,7 @@ __Errno Fcntl(int fd, int cmd, int arg) { ...@@ -107,6 +111,7 @@ __Errno Fcntl(int fd, int cmd, int arg) {
errno = save_errno; errno = save_errno;
return err; return err;
} }
#endif
__Errno Fstat(int fd, struct ::stat *out_st) { __Errno Fstat(int fd, struct ::stat *out_st) {
int save_errno = errno; int save_errno = errno;
...@@ -119,6 +124,10 @@ __Errno Fstat(int fd, struct ::stat *out_st) { ...@@ -119,6 +124,10 @@ __Errno Fstat(int fd, struct ::stat *out_st) {
int Open(const char *path, int flags, mode_t mode) { int Open(const char *path, int flags, mode_t mode) {
int save_errno = errno; int save_errno = errno;
#ifdef _WIN32 // default to open files in binary mode
if ((flags & (_O_TEXT | _O_BINARY)) == 0)
flags |= _O_BINARY;
#endif
int fd = ::open(path, flags, mode); int fd = ::open(path, flags, mode);
if (fd < 0) if (fd < 0)
fd = -errno; fd = -errno;
...@@ -134,6 +143,8 @@ __Errno Pipe(int vfd[2], int flags) { ...@@ -134,6 +143,8 @@ __Errno Pipe(int vfd[2], int flags) {
int err; int err;
#ifdef __linux__ #ifdef __linux__
err = ::pipe2(vfd, flags); err = ::pipe2(vfd, flags);
#elif defined(_WIN32)
err = ::_pipe(vfd, 4096, flags | _O_BINARY);
#else #else
err = ::pipe(vfd); err = ::pipe(vfd);
if (err) if (err)
...@@ -156,6 +167,7 @@ out: ...@@ -156,6 +167,7 @@ out:
return err; return err;
} }
#ifndef _WIN32
__Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact) { __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact) {
int save_errno = errno; int save_errno = errno;
int err = ::sigaction(signo, act, oldact); int err = ::sigaction(signo, act, oldact);
...@@ -164,6 +176,7 @@ __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction * ...@@ -164,6 +176,7 @@ __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *
errno = save_errno; errno = save_errno;
return err; return err;
} }
#endif
}}} // golang::internal::syscall:: }}} // golang::internal::syscall::
...@@ -63,11 +63,15 @@ LIBGOLANG_API int/*n|err*/ Read(int fd, void *buf, size_t count); ...@@ -63,11 +63,15 @@ LIBGOLANG_API int/*n|err*/ Read(int fd, void *buf, size_t count);
LIBGOLANG_API int/*n|err*/ Write(int fd, const void *buf, size_t count); LIBGOLANG_API int/*n|err*/ Write(int fd, const void *buf, size_t count);
LIBGOLANG_API __Errno Close(int fd); LIBGOLANG_API __Errno Close(int fd);
#ifndef _WIN32
LIBGOLANG_API __Errno Fcntl(int fd, int cmd, int arg); LIBGOLANG_API __Errno Fcntl(int fd, int cmd, int arg);
#endif
LIBGOLANG_API __Errno Fstat(int fd, struct ::stat *out_st); LIBGOLANG_API __Errno Fstat(int fd, struct ::stat *out_st);
LIBGOLANG_API int/*fd|err*/ Open(const char *path, int flags, mode_t mode); LIBGOLANG_API int/*fd|err*/ Open(const char *path, int flags, mode_t mode);
LIBGOLANG_API __Errno Pipe(int vfd[2], int flags); LIBGOLANG_API __Errno Pipe(int vfd[2], int flags);
#ifndef _WIN32
LIBGOLANG_API __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact); LIBGOLANG_API __Errno Sigaction(int signo, const struct ::sigaction *act, struct ::sigaction *oldact);
#endif
}}} // golang::internal::syscall:: }}} // golang::internal::syscall::
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment