00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <pch.h>
00033 #if defined(HOST_IS_WINDOWS)
00034 #include <windows.h>
00035 #elif defined(HOST_IS_CYGWIN) || defined(HOST_IS_LINUX)
00036
00037 #endif
00038 #include "SysDeps/posix.h"
00039
00040 using namespace std;
00041 using namespace Onikiri;
00042 using namespace Onikiri::POSIX;
00043
00044 #if defined(HOST_IS_WINDOWS)
00045
00046
00047
00048 namespace {
00049
00050
00051 int posix_errno = 0;
00052
00053
00054
00055
00056
00057 template <typename T, T ErrorVal>
00058 T check_error(T result)
00059 {
00060 if (result == ErrorVal) {
00061 posix_errno = errno;
00062 }
00063 return result;
00064 }
00065 }
00066
00067 namespace Onikiri {
00068 namespace POSIX {
00069 int posix_geterrno()
00070 {
00071 return posix_errno;
00072 }
00073
00074 int posix_getpid()
00075 {
00076 return _getpid();
00077 }
00078 char *posix_getcwd(char* buf, int maxlen)
00079 {
00080 return check_error<char*, 0>( _getcwd(buf, maxlen) );
00081 }
00082 int posix_chdir(const char* path)
00083 {
00084 return check_error<int, -1>( _chdir(path) );
00085 }
00086
00087 int posix_open(const char* name, int flags)
00088 {
00089 return check_error<int, -1>( _open(name, flags | POSIX_O_BINARY) );
00090 }
00091 int posix_open(const char* name, int flags, int pmode)
00092 {
00093 return check_error<int, -1>( _open(name, flags | POSIX_O_BINARY, pmode) );
00094 }
00095 int posix_read(int fd, void* buf, unsigned int count)
00096 {
00097 return check_error<int, -1>( _read(fd, buf, count) );
00098 }
00099 int posix_write(int fd, void* buf, unsigned int count)
00100 {
00101 return check_error<int, -1>( _write(fd, buf, count) );
00102 }
00103 int posix_close(int fd)
00104 {
00105 return check_error<int, -1>( _close(fd) );
00106 }
00107 s64 posix_lseek(int fd, s64 offset, int whence)
00108 {
00109 return check_error<s64, -1>( _lseeki64(fd, offset, whence) );
00110 }
00111 int posix_dup(int fd)
00112 {
00113 return check_error<int, -1>( _dup(fd) );
00114 }
00115
00116
00117 int posix_fstat(int fd, posix_struct_stat* s)
00118 {
00119 return check_error<int, -1>( fstat(fd, s) );
00120 }
00121 int posix_stat(const char* path, posix_struct_stat* s)
00122 {
00123 return check_error<int, -1>( stat(path, s) );
00124 }
00125 int posix_lstat(const char* path, posix_struct_stat* s)
00126 {
00127
00128 return posix_stat(path, s);
00129 }
00130
00131 int posix_fileno(FILE *file)
00132 {
00133 return check_error<int, -1>( _fileno(file) );
00134 }
00135 int posix_access(const char* path, int mode)
00136 {
00137 return check_error<int, -1>( _access(path, mode) );
00138 }
00139 int posix_unlink(const char* path)
00140 {
00141 return check_error<int, -1>( _unlink(path) );
00142 }
00143 int posix_rename(const char* oldpath, const char* newpath)
00144 {
00145 if (MoveFileEx(oldpath, newpath, MOVEFILE_REPLACE_EXISTING)) {
00146 return 0;
00147 }
00148 else {
00149 switch (GetLastError()) {
00150 case ERROR_FILE_NOT_FOUND:
00151 case ERROR_PATH_NOT_FOUND:
00152 posix_errno = ENOENT;
00153 break;
00154 case ERROR_ACCESS_DENIED:
00155 default:
00156 posix_errno = EACCES;
00157 break;
00158 }
00159 return -1;
00160 }
00161 }
00162
00163 int posix_truncate(const char* path, s64 length)
00164 {
00165 int fd = posix_open(path, POSIX_O_RDWR);
00166 if (fd == -1)
00167 return -1;
00168 int result = posix_ftruncate(fd, length);
00169 posix_close(fd);
00170 return result;
00171 }
00172
00173 int posix_ftruncate(int fd, s64 length)
00174 {
00175 int result = _chsize_s(fd, length);
00176 if (result != 0) {
00177 posix_errno = result;
00178 return -1;
00179 }
00180 else {
00181 return 0;
00182 }
00183 }
00184 }
00185 }
00186
00187 #elif defined(HOST_IS_CYGWIN) || defined(HOST_IS_LINUX)
00188
00189
00190
00191 #endif
00192