22 #undef __STRICT_ANSI__
53 #define isdirsep(x) ((x) == '/' || (x) == '\\')
55 #if defined _MSC_VER && _MSC_VER <= 1200
56 # define CharNextExA(cp, p, flags) CharNextExA((WORD)(cp), (p), (flags))
59 static int w32_stati64(
const char *path,
struct stati64 *st, UINT cp);
63 #define DLN_FIND_EXTRA_ARG_DECL ,UINT cp
64 #define DLN_FIND_EXTRA_ARG ,cp
65 #define rb_w32_stati64(path, st) w32_stati64(path, st, cp)
66 #define getenv(name) w32_getenv(name, cp)
67 #define dln_find_exe_r rb_w32_udln_find_exe_r
68 #define dln_find_file_r rb_w32_udln_find_file_r
74 #undef dln_find_file_r
75 #define dln_find_exe_r(fname, path, buf, size) rb_w32_udln_find_exe_r(fname, path, buf, size, cp)
76 #define dln_find_file_r(fname, path, buf, size) rb_w32_udln_find_file_r(fname, path, buf, size, cp)
84 #if defined __BORLANDC__
85 # define _filbuf _fgetc
86 # define _flsbuf _fputc
87 # define enough_to_get(n) (--(n) >= 0)
88 # define enough_to_put(n) (++(n) < 0)
90 # define enough_to_get(n) (--(n) >= 0)
91 # define enough_to_put(n) (--(n) >= 0)
95 #define Debug(something) something
97 #define Debug(something)
100 #define TO_SOCKET(x) _get_osfhandle(x)
106 static int wstati64(
const WCHAR *path,
struct stati64 *st);
109 #define RUBY_CRITICAL(expr) do { expr; } while (0)
116 { ERROR_INVALID_FUNCTION, EINVAL },
117 { ERROR_FILE_NOT_FOUND, ENOENT },
118 { ERROR_PATH_NOT_FOUND, ENOENT },
119 { ERROR_TOO_MANY_OPEN_FILES, EMFILE },
120 { ERROR_ACCESS_DENIED, EACCES },
121 { ERROR_INVALID_HANDLE, EBADF },
122 { ERROR_ARENA_TRASHED, ENOMEM },
123 { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
124 { ERROR_INVALID_BLOCK, ENOMEM },
125 { ERROR_BAD_ENVIRONMENT, E2BIG },
126 { ERROR_BAD_FORMAT, ENOEXEC },
127 { ERROR_INVALID_ACCESS, EINVAL },
128 { ERROR_INVALID_DATA, EINVAL },
129 { ERROR_INVALID_DRIVE, ENOENT },
130 { ERROR_CURRENT_DIRECTORY, EACCES },
131 { ERROR_NOT_SAME_DEVICE, EXDEV },
132 { ERROR_NO_MORE_FILES, ENOENT },
133 { ERROR_WRITE_PROTECT, EROFS },
134 { ERROR_BAD_UNIT, ENODEV },
135 { ERROR_NOT_READY, ENXIO },
136 { ERROR_BAD_COMMAND, EACCES },
137 { ERROR_CRC, EACCES },
138 { ERROR_BAD_LENGTH, EACCES },
140 { ERROR_NOT_DOS_DISK, EACCES },
141 { ERROR_SECTOR_NOT_FOUND, EACCES },
142 { ERROR_OUT_OF_PAPER, EACCES },
143 { ERROR_WRITE_FAULT, EIO },
144 { ERROR_READ_FAULT, EIO },
145 { ERROR_GEN_FAILURE, EACCES },
146 { ERROR_LOCK_VIOLATION, EACCES },
147 { ERROR_SHARING_VIOLATION, EACCES },
148 { ERROR_WRONG_DISK, EACCES },
149 { ERROR_SHARING_BUFFER_EXCEEDED, EACCES },
150 { ERROR_BAD_NETPATH, ENOENT },
151 { ERROR_NETWORK_ACCESS_DENIED, EACCES },
152 { ERROR_BAD_NET_NAME, ENOENT },
153 { ERROR_FILE_EXISTS, EEXIST },
154 { ERROR_CANNOT_MAKE, EACCES },
155 { ERROR_FAIL_I24, EACCES },
156 { ERROR_INVALID_PARAMETER, EINVAL },
157 { ERROR_NO_PROC_SLOTS, EAGAIN },
158 { ERROR_DRIVE_LOCKED, EACCES },
159 { ERROR_BROKEN_PIPE, EPIPE },
160 { ERROR_DISK_FULL, ENOSPC },
161 { ERROR_INVALID_TARGET_HANDLE, EBADF },
162 { ERROR_INVALID_HANDLE, EINVAL },
163 { ERROR_WAIT_NO_CHILDREN, ECHILD },
164 { ERROR_CHILD_NOT_COMPLETE, ECHILD },
165 { ERROR_DIRECT_ACCESS_HANDLE, EBADF },
166 { ERROR_NEGATIVE_SEEK, EINVAL },
167 { ERROR_SEEK_ON_DEVICE, EACCES },
168 { ERROR_DIR_NOT_EMPTY, ENOTEMPTY },
169 { ERROR_DIRECTORY, ENOTDIR },
170 { ERROR_NOT_LOCKED, EACCES },
171 { ERROR_BAD_PATHNAME, ENOENT },
172 { ERROR_MAX_THRDS_REACHED, EAGAIN },
173 { ERROR_LOCK_FAILED, EACCES },
174 { ERROR_ALREADY_EXISTS, EEXIST },
175 { ERROR_INVALID_STARTING_CODESEG, ENOEXEC },
176 { ERROR_INVALID_STACKSEG, ENOEXEC },
177 { ERROR_INVALID_MODULETYPE, ENOEXEC },
178 { ERROR_INVALID_EXE_SIGNATURE, ENOEXEC },
179 { ERROR_EXE_MARKED_INVALID, ENOEXEC },
180 { ERROR_BAD_EXE_FORMAT, ENOEXEC },
181 { ERROR_ITERATED_DATA_EXCEEDS_64k,ENOEXEC },
182 { ERROR_INVALID_MINALLOCSIZE, ENOEXEC },
183 { ERROR_DYNLINK_FROM_INVALID_RING,ENOEXEC },
184 { ERROR_IOPL_NOT_ENABLED, ENOEXEC },
185 { ERROR_INVALID_SEGDPL, ENOEXEC },
186 { ERROR_AUTODATASEG_EXCEEDS_64k, ENOEXEC },
187 { ERROR_RING2SEG_MUST_BE_MOVABLE, ENOEXEC },
188 { ERROR_RELOC_CHAIN_XEEDS_SEGLIM, ENOEXEC },
189 { ERROR_INFLOOP_IN_RELOC_CHAIN, ENOEXEC },
190 { ERROR_FILENAME_EXCED_RANGE, ENOENT },
191 { ERROR_NESTING_NOT_ALLOWED, EAGAIN },
192 #ifndef ERROR_PIPE_LOCAL
193 #define ERROR_PIPE_LOCAL 229L
196 { ERROR_BAD_PIPE, EPIPE },
197 { ERROR_PIPE_BUSY, EAGAIN },
198 { ERROR_NO_DATA, EPIPE },
199 { ERROR_PIPE_NOT_CONNECTED, EPIPE },
200 { ERROR_OPERATION_ABORTED, EINTR },
201 { ERROR_NOT_ENOUGH_QUOTA, ENOMEM },
202 { ERROR_MOD_NOT_FOUND, ENOENT },
205 { WSAEACCES, EACCES },
206 { WSAEFAULT, EFAULT },
207 { WSAEINVAL, EINVAL },
208 { WSAEMFILE, EMFILE },
237 { WSAENAMETOOLONG, ENAMETOOLONG },
241 { WSAENOTEMPTY, ENOTEMPTY },
258 for (i = 0; i < (int)(
sizeof(
errmap) /
sizeof(*
errmap)); i++) {
259 if (
errmap[i].winerr == winerr) {
264 if (winerr >= WSABASEERR) {
270 #define map_errno rb_w32_map_errno
280 memset(&
osver, 0,
sizeof(OSVERSIONINFO));
281 osver.dwOSVersionInfoSize =
sizeof(OSVERSIONINFO);
282 GetVersionEx(&
osver);
290 return osver.dwPlatformId;
298 return osver.dwMajorVersion;
304 #define LK_ERR(f,i) \
309 DWORD err = GetLastError(); \
310 if (err == ERROR_LOCK_VIOLATION || err == ERROR_IO_PENDING) \
311 errno = EWOULDBLOCK; \
312 else if (err == ERROR_NOT_LOCKED) \
315 errno = map_errno(err); \
318 #define LK_LEN ULONG_MAX
326 const HANDLE fh = (HANDLE)
self;
327 const int oper =
argc;
329 memset(&o, 0,
sizeof(o));
343 LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
371 static inline WCHAR *
386 if ((
unsigned char)*p == from)
388 p = CharNextExA(cp, p, 0);
393 #ifndef CSIDL_LOCAL_APPDATA
394 #define CSIDL_LOCAL_APPDATA 28
396 #ifndef CSIDL_COMMON_APPDATA
397 #define CSIDL_COMMON_APPDATA 35
399 #ifndef CSIDL_WINDOWS
400 #define CSIDL_WINDOWS 36
403 #define CSIDL_SYSTEM 37
405 #ifndef CSIDL_PROFILE
406 #define CSIDL_PROFILE 40
416 if (SHGetSpecialFolderLocation(
NULL, n, &pidl) == 0) {
417 f = SHGetPathFromIDListW(pidl, env);
419 alloc->lpVtbl->Free(alloc, pidl);
420 alloc->lpVtbl->Release(alloc);
430 if (p - path == 2 && path[1] == L
':') {
444 h = LoadLibrary(module);
446 h = GetModuleHandle(module);
450 ptr = GetProcAddress(h, func);
464 typedef UINT WINAPI wgetdir_func(WCHAR*, UINT);
468 return (*(wgetdir_func *)ptr)(path, len);
469 return GetWindowsDirectoryW(path, len);
476 WCHAR path[_MAX_PATH];
487 static const WCHAR temp[] = L
"temp";
494 if (*(p - 1) != L
'/') *p++ = L
'/';
495 if ((UINT)(p - path +
numberof(temp)) >= len)
return 0;
496 memcpy(p, temp,
sizeof(temp));
497 return (UINT)(p - path +
numberof(temp) - 1);
504 static const WCHAR TMPDIR[] = L
"TMPDIR";
505 struct {WCHAR
name[6],
eq,
val[_MAX_PATH];} wk;
509 #define set_env_val(vname) do { \
510 typedef char wk_name_offset[(numberof(wk.name) - (numberof(vname) - 1)) * 2 + 1]; \
511 WCHAR *const buf = wk.name + sizeof(wk_name_offset) / 2; \
512 MEMCPY(buf, vname, WCHAR, numberof(vname) - 1); \
524 if (GetEnvironmentVariableW(L
"HOMEPATH",
env + len,
numberof(
env) - len) || len) {
527 else if (GetEnvironmentVariableW(L
"USERPROFILE",
env,
numberof(
env))) {
577 #if RUBY_MSVCRT_VERSION >= 80
580 invalid_parameter(
const wchar_t *expr,
const wchar_t *
func,
const wchar_t *file,
unsigned int line,
uintptr_t dummy)
585 int ruby_w32_rtc_error;
589 rtc_error_handler(
int e,
const char *src,
int line,
const char *exe,
const char *fmt, ...)
594 if (!ruby_w32_rtc_error)
return 0;
609 #define conlist_disabled ((st_table *)-1)
646 if (NtSocketsInitialized) {
652 DeleteCriticalSection(&select_mutex);
653 NtSocketsInitialized = 0;
661 FreeEnvironmentStrings(envarea);
681 version = MAKEWORD(2, 0);
682 if (WSAStartup(version, &retdata))
683 rb_fatal (
"Unable to locate winsock library!\n");
684 if (LOBYTE(retdata.wVersion) != 2)
685 rb_fatal(
"could not find version 2 of winsock dll\n");
687 InitializeCriticalSection(&select_mutex);
689 NtSocketsInitialized = 1;
692 #define MAKE_SOCKDATA(af, fl) ((int)((((int)af)<<4)|((fl)&0xFFFF)))
693 #define GET_FAMILY(v) ((int)(((v)>>4)&0xFFFF))
694 #define GET_FLAGS(v) ((int)((v)&0xFFFF))
736 *sockp = (SOCKET)key;
751 #if RUBY_MSVCRT_VERSION >= 80
752 static void set_pioinfo_extra(
void);
754 _CrtSetReportMode(_CRT_ASSERT, 0);
755 _set_invalid_parameter_handler(invalid_parameter);
756 _RTC_SetErrorFunc(rtc_error_handler);
759 SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOGPFAULTERRORBOX);
793 #define MAXCHILDNUM 256
802 #define FOREACH_CHILD(v) do { \
803 struct ChildRecord* v; \
804 for (v = ChildRecord; v < ChildRecord + sizeof(ChildRecord) / sizeof(ChildRecord[0]); ++v)
805 #define END_FOREACH_CHILD } while (0)
813 if (child->pid == pid) {
826 if (child->hProcess == h) {
850 child->hProcess =
NULL;
920 return strcmp(key, (*(
const char *
const *)elem) + 1);
927 int i =
strlen(interp) - 11;
929 if ((i == 0 || i > 0 &&
isdirsep(interp[i-1])) &&
942 char cmdname[9], *b = cmdname, c;
945 if (!(c = *cmd++))
return 0;
946 }
while (isspace(c));
951 if (b == cmdname +
sizeof(cmdname))
return 0;
954 if (c ==
'.') c = *cmd;
956 case '<':
case '>':
case '|':
958 case '\0':
case ' ':
case '\t':
case '\n':
973 nm = bsearch(cmdname, szInternalCmds,
974 sizeof(szInternalCmds) /
sizeof(*szInternalCmds),
975 sizeof(*szInternalCmds),
977 if (!nm || !(nm[0][0] & (nt ? 2 : 1)))
986 return _get_osfhandle(fh);
991 join_argv(
char *cmd,
char *
const *
argv, BOOL escape, UINT cp,
int backslash)
995 int len, n, bs, quote;
997 for (t = argv, q = cmd, len = 0; p = *t; t++) {
1000 if (!*p || strpbrk(p,
" \t\"'")) {
1005 for (bs = 0; *p; ++p) {
1019 memset(q,
'\\', bs);
1024 case '<':
case '>':
case '|':
case '^':
1025 if (escape && !quote) {
1026 len += (n = p - s) + 1;
1037 p = CharNextExA(cp, p, 0) - 1;
1041 len += (n = p - s) + 1;
1045 if (backslash > 0) {
1051 if (quote) *q++ =
'"';
1063 #ifdef HAVE_SYS_PARAM_H
1064 # include <sys/param.h>
1066 # define MAXPATHLEN 512
1070 #define STRNDUPV(ptr, v, src, len) \
1071 (((char *)memcpy(((ptr) = ALLOCV((v), (len) + 1)), (src), (len)))[len] = 0)
1097 if (mode == P_OVERLAY) {
1098 WaitForSingleObject(child->
hProcess, INFINITE);
1099 GetExitCodeProcess(child->
hProcess, &exitcode);
1108 CreateChild(
const WCHAR *cmd,
const WCHAR *prog, SECURITY_ATTRIBUTES *psa,
1109 HANDLE hInput, HANDLE hOutput, HANDLE hError,
DWORD dwCreationFlags)
1112 STARTUPINFOW aStartupInfo;
1113 PROCESS_INFORMATION aProcessInformation;
1114 SECURITY_ATTRIBUTES sa;
1117 if (!cmd && !prog) {
1129 sa.nLength =
sizeof (SECURITY_ATTRIBUTES);
1130 sa.lpSecurityDescriptor =
NULL;
1131 sa.bInheritHandle =
TRUE;
1135 memset(&aStartupInfo, 0,
sizeof(aStartupInfo));
1136 memset(&aProcessInformation, 0,
sizeof(aProcessInformation));
1137 aStartupInfo.cb =
sizeof(aStartupInfo);
1138 aStartupInfo.dwFlags = STARTF_USESTDHANDLES;
1140 aStartupInfo.hStdInput = hInput;
1143 aStartupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
1146 aStartupInfo.hStdOutput = hOutput;
1149 aStartupInfo.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
1152 aStartupInfo.hStdError = hError;
1155 aStartupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
1158 dwCreationFlags |= NORMAL_PRIORITY_CLASS;
1160 if (lstrlenW(cmd) > 32767) {
1167 fRet = CreateProcessW(prog, (WCHAR *)cmd, psa, psa,
1168 psa->bInheritHandle, dwCreationFlags,
NULL,
NULL,
1169 &aStartupInfo, &aProcessInformation);
1178 CloseHandle(aProcessInformation.hThread);
1180 child->
hProcess = aProcessInformation.hProcess;
1181 child->
pid = (rb_pid_t)aProcessInformation.dwProcessId;
1191 if (len <= 4)
return 0;
1193 if (*cmd++ !=
'.')
return 0;
1199 static UINT
filecp(
void);
1200 #define mbstr_to_wstr rb_w32_mbstr_to_wstr
1201 #define wstr_to_mbstr rb_w32_wstr_to_mbstr
1202 #define acp_to_wstr(str, plen) mbstr_to_wstr(CP_ACP, str, -1, plen)
1203 #define wstr_to_acp(str, plen) wstr_to_mbstr(CP_ACP, str, -1, plen)
1204 #define filecp_to_wstr(str, plen) mbstr_to_wstr(filecp(), str, -1, plen)
1205 #define wstr_to_filecp(str, plen) wstr_to_mbstr(filecp(), str, -1, plen)
1206 #define utf8_to_wstr(str, plen) mbstr_to_wstr(CP_UTF8, str, -1, plen)
1207 #define wstr_to_utf8(str, plen) wstr_to_mbstr(CP_UTF8, str, -1, plen)
1211 w32_spawn(
int mode,
const char *cmd,
const char *prog, UINT cp)
1215 const char *shell =
NULL;
1216 WCHAR *wcmd =
NULL, *wshell =
NULL;
1222 char *cmd_sep =
NULL;
1240 size_t shell_len =
strlen(shell);
1241 char *tmp =
ALLOCV(v, shell_len +
strlen(cmd) +
sizeof(
" -c ") + 2);
1242 memcpy(tmp, shell, shell_len + 1);
1244 sprintf(tmp + shell_len,
" -c \"%s\"", cmd);
1247 else if ((shell =
getenv(
"COMSPEC")) &&
1252 sprintf(tmp, nt ?
"%s /c \"%s\"" :
"%s /c %s", shell, cmd);
1256 int len = 0, quote = (*cmd ==
'"') ?
'"' : (*cmd ==
'\'') ?
'\'' : 0;
1258 for (prog = cmd + !!quote;; prog = CharNextExA(cp, prog, 0)) {
1259 if (*prog ==
'/') slash = 1;
1269 if ((
unsigned char)*prog == quote) {
1270 len = prog++ - cmd - 1;
1275 if (quote)
continue;
1281 sep = *(cmd_sep = &p[len]);
1291 shell = p ? p : cmd;
1295 if (
strchr(shell,
' ')) quote = -1;
1296 if (shell == fbuf) {
1299 else if (shell != p &&
strchr(shell,
'/')) {
1306 cmd = p =
ALLOCV(v, len + alen + (quote ? 2 : 0) + 1);
1307 if (quote) *p++ =
'"';
1308 memcpy(p, shell, len);
1310 if (quote) *p++ =
'"';
1311 memcpy(p, prog, alen + 1);
1320 if (cmd_sep) *cmd_sep = sep;
1345 return w32_spawn(mode, cmd, prog, CP_UTF8);
1354 BOOL ntcmd =
FALSE, tmpnt;
1364 if (!prog) prog = argv[0];
1365 if ((shell =
getenv(
"COMSPEC")) &&
1372 if (cmd == prog)
strlcpy(cmd = fbuf, prog,
sizeof(fbuf));
1376 else if (
strchr(prog,
'/')) {
1378 if (len <
sizeof(fbuf))
1379 strlcpy(cmd = fbuf, prog,
sizeof(fbuf));
1387 progs[0] = (
char *)prog;
1390 if (c_switch) len += 3;
1395 if (c_switch)
strlcat(cmd,
" /c", len);
1397 prog = c_switch ? shell : 0;
1459 #define NTGLOB 0x1 // element contains a wildcard
1460 #define NTMALLOC 0x2 // string in element was malloc'ed
1461 #define NTSTRING 0x4 // element contains a quoted string
1471 if (!tmpcurr)
return -1;
1475 if (!tmpcurr->
str)
return -1;
1478 *tail = &tmpcurr->
next;
1493 if (!(buf =
malloc(patt->
len + 1)))
return 0;
1496 buf[patt->
len] =
'\0';
1504 if (status || last == tail)
return 0;
1528 for (ptr = cmd; *ptr;) {
1534 else if (quote == *ptr)
1550 if (*++ptr !=
'_' && !
ISALPHA(*ptr))
break;
1551 while (*++ptr ==
'_' ||
ISALNUM(*ptr));
1552 if (*ptr++ ==
'%')
return TRUE;
1558 ptr = CharNextExA(cp, ptr, 0);
1566 static inline char *
1579 int elements, strsz, done;
1580 int slashes, escape;
1581 char *ptr, *base, *buffer, *cmdline;
1598 ptr = cmdline =
strdup(cmd);
1611 quote = slashes = globbing = escape = 0;
1612 for (done = 0; !done && *ptr; ) {
1621 if (quote !=
'\'') slashes++;
1661 if (!(slashes & 1)) {
1664 else if (quote == *ptr) {
1665 if (quote ==
'"' && quote == ptr[1])
1698 slashes = quote = 0;
1699 while (p < base + len) {
1703 if (quote !=
'\'') slashes++;
1708 if (!(slashes & 1) && quote && quote != c) {
1713 memcpy(p - ((slashes + 1) >> 1), p + (~slashes & 1),
1715 len -= ((slashes + 1) >> 1) + (~slashes & 1);
1716 p -= (slashes + 1) >> 1;
1717 if (!(slashes & 1)) {
1719 if (quote ==
'"' && quote == *p)
1740 if (!curr)
goto do_nothing;
1744 if (globbing && (tail =
cmdglob(curr, cmdtail))) {
1749 cmdtail = &curr->
next;
1759 for (elements = 0, strsz = 0, curr = cmdhead; curr; curr = curr->
next) {
1761 strsz += (curr->
len + 1);
1764 len = (elements+1)*
sizeof(
char *) + strsz;
1765 buffer = (
char *)
malloc(len);
1768 while (curr = cmdhead) {
1769 cmdhead = curr->
next;
1774 for (vptr = *vec; *vptr; ++vptr);
1790 vptr = (
char **) buffer;
1792 ptr = buffer + (elements+1) *
sizeof(
char *);
1794 while (curr = cmdhead) {
1797 ptr += curr->
len + 1;
1798 cmdhead = curr->
next;
1804 *vec = (
char **) buffer;
1820 #define GetBit(bits, i) ((bits)[(i) / CHAR_BIT] & (1 << (i) % CHAR_BIT))
1821 #define SetBit(bits, i) ((bits)[(i) / CHAR_BIT] |= (1 << (i) % CHAR_BIT))
1823 #define BitOfIsDir(n) ((n) * 2)
1824 #define BitOfIsRep(n) ((n) * 2 + 1)
1825 #define DIRENT_PER_CHAR (CHAR_BIT / 2)
1832 static const WCHAR wildcard[] = L
"\\*";
1841 len = lstrlenW(filename);
1842 scanname =
ALLOCV_N(WCHAR, v, len +
sizeof(wildcard) /
sizeof(WCHAR));
1843 lstrcpyW(scanname, filename);
1844 p = CharPrevW(scanname, scanname + len);
1845 if (*p == L
'/' || *p == L
'\\' || *p == L
':')
1846 lstrcatW(scanname, wildcard + 1);
1848 lstrcatW(scanname, wildcard);
1853 fh = FindFirstFileW(scanname, fd);
1855 if (fh == INVALID_HANDLE_VALUE) {
1865 struct stati64 sbuf;
1866 WIN32_FIND_DATAW fd;
1880 if (!(sbuf.st_mode & S_IFDIR) &&
1881 (!
ISALPHA(filename[0]) || filename[1] !=
':' || filename[2] !=
'\0' ||
1882 ((1 << ((filename[0] & 0x5f) -
'A')) & GetLogicalDrives()) == 0)) {
1887 if (fh == INVALID_HANDLE_VALUE) {
1907 len = lstrlenW(fd.cFileName) + 1;
1923 memcpy(&p->
start[idx], fd.cFileName, len *
sizeof(WCHAR));
1932 if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1934 if (fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1939 }
while (FindNextFileW(fh, &fd));
1950 UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
1959 int len = WideCharToMultiByte(cp, 0, wstr, clen,
NULL, 0,
NULL,
NULL);
1960 if (!(ptr =
malloc(len)))
return 0;
1961 WideCharToMultiByte(cp, 0, wstr, clen, ptr, len,
NULL,
NULL);
1964 if (clen == -1) --len;
1975 int len = MultiByteToWideChar(cp, 0, str, clen,
NULL, 0);
1976 if (!(ptr =
malloc(
sizeof(WCHAR) * len)))
return 0;
1977 MultiByteToWideChar(cp, 0, str, clen, ptr, len);
1980 if (clen == -1) --len;
2022 dirp->
curr += lstrlenW(dirp->
curr) + 1;
2047 long len = lstrlenW(wstr);
2050 if (encindex == ENCINDEX_UTF_16LE) {
2054 #if SIZEOF_INT < SIZEOF_LONG
2055 # error long should equal to int on Windows
2058 len = WideCharToMultiByte(CP_UTF8, 0, wstr, clen,
NULL, 0,
NULL,
NULL);
2063 case ENCINDEX_ASCII:
2064 case ENCINDEX_US_ASCII:
2066 case ENCINDEX_UTF_8:
2101 static int dummy = 0;
2167 while (dirp->
curr && dirp->
loc < loc) {
2203 #if (defined _MT || defined __MSVCRT__) && !defined __BORLANDC__
2204 #define MSVCRT_THREADS
2206 #ifdef MSVCRT_THREADS
2207 # define MTHREAD_ONLY(x) x
2208 # define STHREAD_ONLY(x)
2209 #elif defined(__BORLANDC__)
2210 # define MTHREAD_ONLY(x)
2211 # define STHREAD_ONLY(x)
2213 # define MTHREAD_ONLY(x)
2214 # define STHREAD_ONLY(x) x
2222 #ifdef MSVCRT_THREADS
2224 CRITICAL_SECTION lock;
2226 #if RUBY_MSVCRT_VERSION >= 80
2232 #if !defined _CRTIMP || defined __MINGW32__
2234 #define _CRTIMP __declspec(dllimport)
2237 #if !defined(__BORLANDC__)
2241 #define IOINFO_L2E 5
2242 #define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
2243 #define _osfhnd(i) (_pioinfo(i)->osfhnd)
2244 #define _osfile(i) (_pioinfo(i)->osfile)
2245 #define _pipech(i) (_pioinfo(i)->pipech)
2247 #if RUBY_MSVCRT_VERSION >= 80
2252 set_pioinfo_extra(
void)
2256 fd = _open(
"NUL", O_RDONLY);
2257 for (pioinfo_extra = 0; pioinfo_extra <= 64; pioinfo_extra +=
sizeof(
void *)) {
2258 if (
_osfhnd(fd) == _get_osfhandle(fd)) {
2264 if (pioinfo_extra > 64) {
2270 #define pioinfo_extra 0
2276 const size_t sizeof_ioinfo =
sizeof(
ioinfo) + pioinfo_extra;
2281 #define _set_osfhnd(fh, osfh) (void)(_osfhnd(fh) = osfh)
2282 #define _set_osflags(fh, flags) (_osfile(fh) = (flags))
2285 #define FEOFLAG 0x02
2287 #define FNOINHERIT 0x10
2288 #define FAPPEND 0x20
2313 if (flags & O_APPEND)
2319 if (flags & O_NOINHERIT)
2323 hF = CreateFile(
"NUL", 0, 0,
NULL, OPEN_ALWAYS, 0,
NULL);
2324 fh = _open_osfhandle((
intptr_t)hF, 0);
2350 #define open_null(fd) \
2352 (nullfd = open("NUL", O_RDWR)) : 0), \
2353 ((nullfd == (fd)) ? (keep = 1) : dup2(nullfd, fd)), \
2362 if (
fileno(stdout) < 0) {
2365 if (
fileno(stderr) < 0) {
2368 if (nullfd >= 0 && !keep) close(nullfd);
2369 setvbuf(stderr,
NULL, _IONBF, 0);
2373 #define _set_osfhnd(fh, osfh) (void)((fh), (osfh))
2374 #define _set_osflags(fh, flags) (void)((fh), (flags))
2388 int fd = _open_osfhandle(osfhandle, flags);
2428 static char buffer[512];
2432 #if defined __BORLANDC__ && defined ENOTEMPTY // _sys_errlist is broken
2435 return "Filename too long";
2437 return "Directory not empty";
2444 #if WSAEWOULDBLOCK != EWOULDBLOCK
2449 for (s = 0; s < (int)(
sizeof(
errmap)/
sizeof(*
errmap)); s++)
2452 for (i = s; i < (int)(
sizeof(
errmap)/
sizeof(*errmap)); i++)
2459 if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
2460 FORMAT_MESSAGE_IGNORE_INSERTS, &source, e,
2461 MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
2462 buffer,
sizeof(buffer),
NULL) == 0 &&
2463 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
2464 FORMAT_MESSAGE_IGNORE_INSERTS, &source, e, 0,
2465 buffer,
sizeof(buffer),
NULL) == 0)
2466 strlcpy(buffer,
"Unknown Error",
sizeof(buffer));
2472 while ((p = strpbrk(p,
"\r\n")) !=
NULL) {
2561 for (i = 0; i < set->fd_count; i++) {
2562 if (set->fd_array[i] == s) {
2563 memmove(&set->fd_array[i], &set->fd_array[i+1],
2564 sizeof(set->fd_array[0]) * (--set->fd_count - i));
2578 if (s == (SOCKET)INVALID_HANDLE_VALUE)
2588 max = min(src->fd_count, (UINT)max);
2589 if ((UINT)dst->capa < (UINT)max) {
2590 dst->capa = (src->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
2591 dst->fdset =
xrealloc(dst->fdset,
sizeof(
unsigned int) +
sizeof(SOCKET) * dst->capa);
2594 memcpy(dst->fdset->fd_array, src->fd_array,
2595 max *
sizeof(src->fd_array[0]));
2596 dst->fdset->fd_count = src->fd_count;
2603 if ((UINT)dst->capa < src->fdset->fd_count) {
2604 dst->capa = (src->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
2605 dst->fdset =
xrealloc(dst->fdset,
sizeof(
unsigned int) +
sizeof(SOCKET) * dst->capa);
2608 memcpy(dst->fdset->fd_array, src->fdset->fd_array,
2609 src->fdset->fd_count *
sizeof(src->fdset->fd_array[0]));
2610 dst->fdset->fd_count = src->fdset->fd_count;
2629 while (s < src->fd_count) {
2630 SOCKET fd = src->fd_array[s];
2636 for (d = 0; d < dst->fdset->fd_count; d++) {
2637 if (dst->fdset->fd_array[d] == fd)
2640 if (d == dst->fdset->fd_count) {
2641 if ((
int)dst->fdset->fd_count >= dst->capa) {
2642 dst->capa = (dst->fdset->fd_count / FD_SETSIZE + 1) * FD_SETSIZE;
2643 dst->fdset =
xrealloc(dst->fdset,
sizeof(
unsigned int) +
sizeof(SOCKET) * dst->capa);
2645 dst->fdset->fd_array[dst->fdset->fd_count++] = fd;
2649 &src->fd_array[s+1],
2650 sizeof(src->fd_array[0]) * (--src->fd_count - s));
2660 return dst ? dst->fdset->fd_count : m;
2668 if (!src || !dst)
return 0;
2670 for (s = 0; s < src->fd_count; ++s) {
2671 SOCKET fd = src->fd_array[s];
2673 for (d = 0; d < dst->fd_count; ++d) {
2674 if (dst->fd_array[d] == fd)
2677 if (d == dst->fd_count && d < FD_SETSIZE) {
2678 dst->fd_array[dst->fd_count++] = fd;
2682 return dst->fd_count;
2699 ret = (GetFileType((HANDLE)sock) == FILE_TYPE_PIPE);
2713 if (PeekNamedPipe((HANDLE)sock,
NULL, 0,
NULL, &n,
NULL)) {
2717 ret = (GetLastError() == ERROR_BROKEN_PIPE);
2733 ret = (PeekConsoleInput((HANDLE)sock, &ir, 1, &n))
2748 if (PeekConsoleInput((HANDLE)sock, &ir, 1, &n) && n > 0) {
2749 if (ir.EventType == KEY_EVENT && ir.Event.KeyEvent.bKeyDown &&
2750 ir.Event.KeyEvent.uChar.AsciiChar) {
2754 ReadConsoleInput((HANDLE)sock, &ir, 1, &n);
2766 return (HANDLE)sock == INVALID_HANDLE_VALUE;
2783 if (!NtSocketsInitialized)
2787 EnterCriticalSection(&select_mutex);
2788 r =
select(nfds, rd, wr, ex, timeout);
2789 LeaveCriticalSection(&select_mutex);
2790 if (r == SOCKET_ERROR) {
2846 struct timeval *timeout,
void *th)
2855 struct timeval limit = {0, 0};
2857 if (nfds < 0 || (timeout && (timeout->
tv_sec < 0 || timeout->
tv_usec < 0))) {
2863 if (timeout->
tv_sec < 0 ||
2865 timeout->
tv_usec >= 1000000) {
2872 if (limit.
tv_usec >= 1000000) {
2909 if (rd && (
int)rd->fd_count > r) r = (int)rd->fd_count;
2910 if (wr && (
int)wr->fd_count > r) r = (
int)wr->fd_count;
2911 if (ex && (
int)ex->fd_count > r) r = (
int)ex->fd_count;
2912 if (nfds > r) nfds = r;
2916 const struct timeval wait = {0, 10 * 1000};
2930 if (else_rd.fdset->fd_count || else_wr.fdset->fd_count) {
2933 r +=
copy_fd(rd, else_rd.fdset);
2934 r +=
copy_fd(wr, else_wr.fdset);
2950 if (rd)
copy_fd(&orig_rd, rd);
2951 if (wr)
copy_fd(&orig_wr, wr);
2952 if (ex)
copy_fd(&orig_ex, ex);
2955 if (rd)
copy_fd(rd, &orig_rd);
2956 if (wr)
copy_fd(wr, &orig_wr);
2957 if (ex)
copy_fd(ex, &orig_ex);
2964 if (
compare(&rest, &wait) < 0) dowait = &rest;
2966 Sleep(dowait->
tv_sec * 1000 + (dowait->
tv_usec + 999) / 1000);
2995 WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER, guid,
sizeof(*guid),
2996 &ptr,
sizeof(ptr), &dmy,
NULL,
NULL);
3011 if (!NtSocketsInitialized) {
3015 HANDLE h = CreateFile(
"NUL", 0, 0,
NULL, OPEN_ALWAYS, 0,
NULL);
3018 r = accept(
TO_SOCKET(s), addr, addrlen);
3019 if (r != INVALID_SOCKET) {
3020 SetHandleInformation((HANDLE)r, HANDLE_FLAG_INHERIT, 0);
3047 if (!NtSocketsInitialized) {
3052 if (r == SOCKET_ERROR)
3065 if (!NtSocketsInitialized) {
3069 r = connect(
TO_SOCKET(s), addr, addrlen);
3070 if (r == SOCKET_ERROR) {
3071 int err = WSAGetLastError();
3072 if (err != WSAEWOULDBLOCK)
3089 if (!NtSocketsInitialized) {
3093 r = getpeername(
TO_SOCKET(s), addr, addrlen);
3094 if (r == SOCKET_ERROR)
3108 if (!NtSocketsInitialized) {
3113 r = getsockname(sock, addr, addrlen);
3114 if (r == SOCKET_ERROR) {
3115 DWORD wsaerror = WSAGetLastError();
3116 if (wsaerror == WSAEINVAL) {
3121 memset(addr, 0, *addrlen);
3122 addr->sa_family = af;
3140 if (!NtSocketsInitialized) {
3144 r = getsockopt(
TO_SOCKET(s), level, optname, optval, optlen);
3145 if (r == SOCKET_ERROR)
3158 if (!NtSocketsInitialized) {
3162 r = ioctlsocket(
TO_SOCKET(s), cmd, argp);
3163 if (r == SOCKET_ERROR)
3176 if (!NtSocketsInitialized) {
3181 if (r == SOCKET_ERROR)
3199 if (result != SOCKET_ERROR)
3201 else if ((err = WSAGetLastError()) == WSA_IO_PENDING) {
3205 result = WSAGetOverlappedResult(s, wol, &size,
TRUE, &flg)
3213 if ((err = WSAGetLastError()) == WSAECONNABORTED && !input)
3218 case WAIT_OBJECT_0 + 1:
3226 if (err == WSAECONNABORTED && !input)
3232 CloseHandle(wol->hEvent);
3240 struct sockaddr *addr,
int *addrlen)
3250 if (!NtSocketsInitialized)
3258 if (addr && addrlen)
3259 r = recvfrom(s, buf, len, flags, addr, addrlen);
3261 r = recv(s, buf, len, flags);
3262 if (r == SOCKET_ERROR)
3266 if (addr && addrlen)
3267 r = sendto(s, buf, len, flags, addr, *addrlen);
3269 r = send(s, buf, len, flags);
3270 if (r == SOCKET_ERROR) {
3272 if (err == WSAECONNABORTED)
3285 memset(&wol, 0,
sizeof(wol));
3290 if (addr && addrlen)
3291 ret = WSARecvFrom(s, &wbuf, 1, &size, &flg, addr, addrlen,
3294 ret = WSARecv(s, &wbuf, 1, &size, &flg, &wol,
NULL);
3297 if (addr && addrlen)
3298 ret = WSASendTo(s, &wbuf, 1, &size, flags, addr, *addrlen,
3301 ret = WSASend(s, &wbuf, 1, &size, flags, &wol,
NULL);
3322 struct sockaddr *from,
int *fromlen)
3337 const struct sockaddr *to,
int tolen)
3340 (
struct sockaddr *)to, &tolen);
3343 #if !defined(MSG_TRUNC) && !defined(__MINGW32__)
3354 #ifndef WSAID_WSARECVMSG
3355 #define WSAID_WSARECVMSG {0xf689d7c8,0x6f1f,0x436b,{0x8a,0x53,0xe5,0x4f,0xe3,0x51,0xc3,0x22}}
3357 #ifndef WSAID_WSASENDMSG
3358 #define WSAID_WSASENDMSG {0xa441e712,0x754f,0x43ca,{0x84,0xa7,0x0d,0xee,0x44,0xcf,0x60,0x6d}}
3362 #define msghdr_to_wsamsg(msg, wsamsg) \
3365 (wsamsg)->name = (msg)->msg_name; \
3366 (wsamsg)->namelen = (msg)->msg_namelen; \
3367 (wsamsg)->lpBuffers = ALLOCA_N(WSABUF, (msg)->msg_iovlen); \
3368 (wsamsg)->dwBufferCount = (msg)->msg_iovlen; \
3369 for (i = 0; i < (msg)->msg_iovlen; ++i) { \
3370 (wsamsg)->lpBuffers[i].buf = (msg)->msg_iov[i].iov_base; \
3371 (wsamsg)->lpBuffers[i].len = (msg)->msg_iov[i].iov_len; \
3373 (wsamsg)->Control.buf = (msg)->msg_control; \
3374 (wsamsg)->Control.len = (msg)->msg_controllen; \
3375 (wsamsg)->dwFlags = (msg)->msg_flags; \
3382 typedef int (WSAAPI *WSARecvMsg_t)(SOCKET,
WSAMSG *,
DWORD *, WSAOVERLAPPED *, LPWSAOVERLAPPED_COMPLETION_ROUTINE);
3383 static WSARecvMsg_t pWSARecvMsg =
NULL;
3390 if (!NtSocketsInitialized)
3408 if ((ret = pWSARecvMsg(s, &wsamsg, &len,
NULL,
NULL)) == SOCKET_ERROR) {
3417 memset(&wol, 0,
sizeof(wol));
3420 ret = pWSARecvMsg(s, &wsamsg, &size, &wol,
NULL);
3425 if (ret == SOCKET_ERROR)
3440 typedef int (WSAAPI *WSASendMsg_t)(SOCKET,
const WSAMSG *,
DWORD, DWORD *, WSAOVERLAPPED *, LPWSAOVERLAPPED_COMPLETION_ROUTINE);
3441 static WSASendMsg_t pWSASendMsg =
NULL;
3448 if (!NtSocketsInitialized)
3465 if ((ret = pWSASendMsg(s, &wsamsg, flags, &len,
NULL,
NULL)) == SOCKET_ERROR) {
3474 memset(&wol, 0,
sizeof(wol));
3477 ret = pWSASendMsg(s, &wsamsg, flags, &size, &wol,
NULL);
3493 if (!NtSocketsInitialized) {
3497 r = setsockopt(
TO_SOCKET(s), level, optname, optval, optlen);
3498 if (r == SOCKET_ERROR)
3511 if (!NtSocketsInitialized) {
3516 if (r == SOCKET_ERROR)
3526 unsigned long proto_buffers_len = 0;
3528 SOCKET out = INVALID_SOCKET;
3530 if (WSAEnumProtocols(
NULL,
NULL, &proto_buffers_len) == SOCKET_ERROR) {
3531 error_code = WSAGetLastError();
3532 if (error_code == WSAENOBUFS) {
3533 WSAPROTOCOL_INFO *proto_buffers;
3534 int protocols_available = 0;
3536 proto_buffers = (WSAPROTOCOL_INFO *)
malloc(proto_buffers_len);
3537 if (!proto_buffers) {
3538 WSASetLastError(WSA_NOT_ENOUGH_MEMORY);
3539 return INVALID_SOCKET;
3542 protocols_available =
3543 WSAEnumProtocols(
NULL, proto_buffers, &proto_buffers_len);
3544 if (protocols_available != SOCKET_ERROR) {
3546 for (i = 0; i < protocols_available; i++) {
3547 if ((af !=
AF_UNSPEC && af != proto_buffers[i].iAddressFamily) ||
3548 (type != proto_buffers[i].iSocketType) ||
3549 (protocol != 0 && protocol != proto_buffers[i].iProtocol))
3552 if ((proto_buffers[i].dwServiceFlags1 & XP1_IFS_HANDLES) == 0)
3555 out = WSASocket(af, type, protocol, &(proto_buffers[i]), 0,
3556 WSA_FLAG_OVERLAPPED);
3559 if (out == INVALID_SOCKET)
3560 out = WSASocket(af, type, protocol,
NULL, 0, 0);
3561 if (out != INVALID_SOCKET)
3562 SetHandleInformation((HANDLE)out, HANDLE_FLAG_INHERIT, 0);
3565 free(proto_buffers);
3581 if (!NtSocketsInitialized) {
3586 if (s == INVALID_SOCKET) {
3601 #undef gethostbyaddr
3604 struct hostent * WSAAPI
3608 if (!NtSocketsInitialized) {
3612 r = gethostbyaddr(addr, len, type);
3619 #undef gethostbyname
3622 struct hostent * WSAAPI
3626 if (!NtSocketsInitialized) {
3630 r = gethostbyname(name);
3644 if (!NtSocketsInitialized) {
3648 r = gethostname(name, len);
3649 if (r == SOCKET_ERROR)
3655 #undef getprotobyname
3658 struct protoent * WSAAPI
3662 if (!NtSocketsInitialized) {
3666 r = getprotobyname(name);
3673 #undef getprotobynumber
3676 struct protoent * WSAAPI
3680 if (!NtSocketsInitialized) {
3684 r = getprotobynumber(num);
3691 #undef getservbyname
3694 struct servent * WSAAPI
3698 if (!NtSocketsInitialized) {
3702 r = getservbyname(name, proto);
3709 #undef getservbyport
3712 struct servent * WSAAPI
3716 if (!NtSocketsInitialized) {
3720 r = getservbyport(port, proto);
3731 SOCKET svr = INVALID_SOCKET, r = INVALID_SOCKET, w = INVALID_SOCKET;
3732 struct sockaddr_in sock_in4;
3734 struct sockaddr_in6 sock_in6;
3736 struct sockaddr *addr;
3740 if (!NtSocketsInitialized) {
3746 #if defined PF_INET && PF_INET != AF_INET
3749 sock_in4.sin_family = AF_INET;
3750 sock_in4.sin_port = 0;
3752 addr = (
struct sockaddr *)&sock_in4;
3753 len =
sizeof(sock_in4);
3757 memset(&sock_in6, 0,
sizeof(sock_in6));
3758 sock_in6.sin6_family = AF_INET6;
3759 sock_in6.sin6_addr = IN6ADDR_LOOPBACK_INIT;
3760 addr = (
struct sockaddr *)&sock_in6;
3761 len =
sizeof(sock_in6);
3768 if (type != SOCK_STREAM) {
3773 sv[0] = (SOCKET)INVALID_HANDLE_VALUE;
3774 sv[1] = (SOCKET)INVALID_HANDLE_VALUE;
3778 if (svr == INVALID_SOCKET)
3780 if (bind(svr, addr, len) < 0)
3782 if (getsockname(svr, addr, &len) < 0)
3784 if (type == SOCK_STREAM)
3788 if (w == INVALID_SOCKET)
3790 if (connect(w, addr, len) < 0)
3793 r = accept(svr, addr, &len);
3794 if (r == INVALID_SOCKET)
3796 SetHandleInformation((HANDLE)r, HANDLE_FLAG_INHERIT, 0);
3803 if (r != INVALID_SOCKET)
3805 if (w != INVALID_SOCKET)
3812 if (svr != INVALID_SOCKET)
3829 closesocket(pair[0]);
3830 closesocket(pair[1]);
3836 closesocket(pair[1]);
3845 #if !defined(_MSC_VER) || _MSC_VER >= 1400
3850 #define hex2byte(str) \
3851 ((isdigit(*(str)) ? *(str) - '0' : toupper(*(str)) - 'A' + 10) << 4 | (isdigit(*((str) + 1)) ? *((str) + 1) - '0' : toupper(*((str) + 1)) - 'A' + 10))
3854 if (*str ==
'{') str++;
3855 guid->Data1 = (long)strtoul(str, &end, 16);
3857 guid->Data2 = (
unsigned short)strtoul(str, &end, 16);
3859 guid->Data3 = (
unsigned short)strtoul(str, &end, 16);
3865 for (i = 0; i < 6; i++) {
3866 guid->Data4[i + 2] =
hex2byte(str);
3872 #ifndef HAVE_TYPE_NET_LUID
3892 IP_ADAPTER_ADDRESSES *root, *addr;
3896 if (ret != ERROR_BUFFER_OVERFLOW) {
3901 ret = GetAdaptersAddresses(
AF_UNSPEC, 0,
NULL, root, &size);
3902 if (ret != ERROR_SUCCESS) {
3908 if (!pConvertInterfaceGuidToLuid)
3909 pConvertInterfaceGuidToLuid =
3911 "ConvertInterfaceGuidToLuid",
NULL);
3912 if (!pConvertInterfaceLuidToNameA)
3913 pConvertInterfaceLuidToNameA =
3915 "ConvertInterfaceLuidToNameA",
NULL);
3917 for (prev =
NULL, addr = root; addr; addr = addr->Next) {
3928 str2guid(addr->AdapterName, &guid);
3929 if (pConvertInterfaceGuidToLuid && pConvertInterfaceLuidToNameA &&
3937 lstrcpy(ifa->
ifa_name, addr->AdapterName);
3940 if (addr->IfType & IF_TYPE_SOFTWARE_LOOPBACK)
3942 if (addr->OperStatus == IfOperStatusUp) {
3945 if (addr->FirstUnicastAddress) {
3946 IP_ADAPTER_UNICAST_ADDRESS *cur;
3948 for (cur = addr->FirstUnicastAddress; cur; cur = cur->Next) {
3949 if (cur->Flags & IP_ADAPTER_ADDRESS_TRANSIENT ||
3950 cur->DadState == IpDadStateDeprecated) {
3963 memcpy(ifa->
ifa_addr, cur->Address.lpSockaddr,
3964 cur->Address.iSockaddrLength);
4035 flag &= ~O_NONBLOCK;
4039 ret = ioctlsocket(sock, FIONBIO, &ioctlArg);
4051 dupfd(HANDLE hDup,
char flags,
int minfd)
4061 goto close_fds_and_return;
4064 goto close_fds_and_return;
4066 fds[filled++] = ret;
4067 }
while (filled < (
int)
numberof(fds));
4069 ret =
dupfd(hDup, flags, minfd);
4071 close_fds_and_return:
4073 while (filled > 0) {
4074 int fd = fds[--filled];
4098 arg = va_arg(va,
int);
4100 return setfl(sock, arg);
4105 if (!(DuplicateHandle(GetCurrentProcess(), (HANDLE)_get_osfhandle(fd),
4106 GetCurrentProcess(), &hDup, 0L,
4108 DUPLICATE_SAME_ACCESS))) {
4114 arg = va_arg(va,
int);
4138 if (!GetExitCodeProcess(child->
hProcess, &exitcode)) {
4141 err = GetLastError();
4143 case ERROR_INVALID_PARAMETER:
4146 case ERROR_INVALID_HANDLE:
4156 if (exitcode != STILL_ACTIVE) {
4165 *stat_loc = exitcode << 8;
4166 if (exitcode & 0xC0000000) {
4167 static const struct {
4171 {STATUS_ACCESS_VIOLATION, SIGSEGV},
4172 {STATUS_ILLEGAL_INSTRUCTION, SIGILL},
4173 {STATUS_PRIVILEGED_INSTRUCTION, SIGILL},
4174 {STATUS_FLOAT_DENORMAL_OPERAND, SIGFPE},
4175 {STATUS_FLOAT_DIVIDE_BY_ZERO, SIGFPE},
4176 {STATUS_FLOAT_INEXACT_RESULT, SIGFPE},
4177 {STATUS_FLOAT_INVALID_OPERATION, SIGFPE},
4178 {STATUS_FLOAT_OVERFLOW, SIGFPE},
4179 {STATUS_FLOAT_STACK_CHECK, SIGFPE},
4180 {STATUS_FLOAT_UNDERFLOW, SIGFPE},
4181 #ifdef STATUS_FLOAT_MULTIPLE_FAULTS
4182 {STATUS_FLOAT_MULTIPLE_FAULTS, SIGFPE},
4184 #ifdef STATUS_FLOAT_MULTIPLE_TRAPS
4185 {STATUS_FLOAT_MULTIPLE_TRAPS, SIGFPE},
4187 {STATUS_CONTROL_C_EXIT,
SIGINT},
4190 for (i = 0; i < (int)
numberof(table); i++) {
4191 if (table[i].status == exitcode) {
4192 *stat_loc |= table[i].sig;
4198 *stat_loc |= SIGSEGV;
4228 if (!child->pid || child->pid < 0)
continue;
4230 events[count++] = child->hProcess;
4238 if (ret == WAIT_TIMEOUT)
return 0;
4239 if ((ret -= WAIT_OBJECT_0) == count) {
4273 if (pid == -1 && retried) pid = 0;
4279 #include <sys/timeb.h>
4286 unsigned LONG_LONG
lt;
4288 tmp.LowPart = ft->dwLowDateTime;
4289 tmp.HighPart = ft->dwHighDateTime;
4297 lt -= (LONG_LONG)((1970-1601)*365.2425) * 24 * 60 * 60 * 1000 * 1000;
4299 tv->
tv_sec = (long)(lt / (1000 * 1000));
4300 tv->
tv_usec = (long)(lt % (1000 * 1000));
4302 return tv->
tv_sec > 0 ? 0 : -1;
4311 GetSystemTimeAsFileTime(&ft);
4333 LARGE_INTEGER
count;
4334 if (!QueryPerformanceFrequency(&freq)) {
4338 if (!QueryPerformanceCounter(&count)) {
4342 sp->
tv_sec = count.QuadPart / freq.QuadPart;
4343 if (freq.QuadPart < 1000000000)
4344 sp->
tv_nsec = (count.QuadPart % freq.QuadPart) * 1000000000 / freq.QuadPart;
4346 sp->
tv_nsec = (
long)((count.QuadPart % freq.QuadPart) * (1000000000.0 / freq.QuadPart));
4369 if (!QueryPerformanceFrequency(&freq)) {
4374 sp->
tv_nsec = (long)(1000000000.0 / freq.QuadPart);
4390 len = GetCurrentDirectory(0,
NULL);
4411 if (!GetCurrentDirectory(size, p)) {
4425 chown(
const char *path,
int owner,
int group)
4444 if (pid < 0 || pid == 0 && sig !=
SIGINT) {
4449 if ((
unsigned int)pid == GetCurrentProcessId() &&
4450 (sig != 0 && sig !=
SIGKILL)) {
4451 if ((ret =
raise(sig)) != 0) {
4462 OpenProcess(PROCESS_QUERY_INFORMATION,
FALSE, (
DWORD)pid);
4463 if (hProc ==
NULL || hProc == INVALID_HANDLE_VALUE) {
4464 if (GetLastError() == ERROR_INVALID_PARAMETER) {
4480 DWORD ctrlEvent = CTRL_C_EVENT;
4484 ctrlEvent = CTRL_BREAK_EVENT;
4486 if (!GenerateConsoleCtrlEvent(ctrlEvent, (
DWORD)pid)) {
4487 if ((err = GetLastError()) == 0)
4504 hProc = OpenProcess(PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION,
FALSE, (
DWORD)pid);
4506 if (hProc ==
NULL || hProc == INVALID_HANDLE_VALUE) {
4507 if (GetLastError() == ERROR_INVALID_PARAMETER) {
4517 if (!GetExitCodeProcess(hProc, &status)) {
4521 else if (status == STILL_ACTIVE) {
4522 if (!TerminateProcess(hProc, 0)) {
4549 wlink(
const WCHAR *from,
const WCHAR *to)
4551 typedef BOOL (WINAPI link_func)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
4552 static link_func *pCreateHardLinkW =
NULL;
4553 static int myerrno = 0;
4555 if (!pCreateHardLinkW && !myerrno) {
4557 if (!pCreateHardLinkW)
4560 if (!pCreateHardLinkW) {
4565 if (!pCreateHardLinkW(to, from,
NULL)) {
4587 ret =
wlink(wfrom, wto);
4595 link(
const char *from,
const char *to)
4607 ret =
wlink(wfrom, wto);
4617 return waitpid(-1, status, 0);
4624 WCHAR *wenvarea, *wenv;
4629 if (len == 0)
return NULL;
4636 FreeEnvironmentStrings(envarea);
4639 wenvarea = GetEnvironmentStringsW();
4644 for (wenv = wenvarea, wlen = 1; *wenv; wenv += lstrlenW(wenv) + 1)
4645 wlen += lstrlenW(wenv) + 1;
4647 FreeEnvironmentStringsW(wenvarea);
4651 for (env = uenvarea; *
env; env +=
strlen(env) + 1)
4652 if (
strncasecmp(env, name, len) == 0 && *(env + len) ==
'=')
4653 return env + len + 1;
4674 wrename(
const WCHAR *oldpath,
const WCHAR *newpath)
4680 oldatts = GetFileAttributesW(oldpath);
4681 newatts = GetFileAttributesW(newpath);
4683 if (oldatts == -1) {
4689 if (newatts != -1 && newatts & FILE_ATTRIBUTE_READONLY)
4690 SetFileAttributesW(newpath, newatts & ~ FILE_ATTRIBUTE_READONLY);
4692 if (!MoveFileExW(oldpath, newpath, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED))
4698 SetFileAttributesW(newpath, oldatts);
4746 if (path[0] == L
'\\' && path[1] == L
'\\') {
4747 const WCHAR *p = path + 2;
4748 if (p[0] == L
'?' && p[1] == L
'\\') {
4756 for (p++; *p; p++) {
4760 if (!p[0] || !p[1] || (p[1] == L
'.' && !p[2]))
4767 #define COPY_STAT(src, dest, size_cast) do { \
4768 (dest).st_dev = (src).st_dev; \
4769 (dest).st_ino = (src).st_ino; \
4770 (dest).st_mode = (src).st_mode; \
4771 (dest).st_nlink = (src).st_nlink; \
4772 (dest).st_uid = (src).st_uid; \
4773 (dest).st_gid = (src).st_gid; \
4774 (dest).st_rdev = (src).st_rdev; \
4775 (dest).st_size = size_cast(src).st_size; \
4776 (dest).st_atime = (src).st_atime; \
4777 (dest).st_mtime = (src).st_mtime; \
4778 (dest).st_ctime = (src).st_ctime; \
4788 BY_HANDLE_FILE_INFORMATION info;
4789 int ret =
fstat(fd, st);
4791 if (ret)
return ret;
4795 if (GetFileInformationByHandle((HANDLE)_get_osfhandle(fd), &info)) {
4797 if (!(info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
4812 BY_HANDLE_FILE_INFORMATION info;
4814 int ret =
fstat(fd, &tmp);
4816 if (ret)
return ret;
4821 if (GetFileInformationByHandle((HANDLE)_get_osfhandle(fd), &info)) {
4823 if (!(info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) {
4827 st->st_size = ((__int64)info.nFileSizeHigh << 32) | info.nFileSizeLow;
4853 if (attr & FILE_ATTRIBUTE_READONLY) {
4857 mode |= S_IREAD | S_IWRITE |
S_IWUSR;
4860 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
4861 mode |= S_IFDIR | S_IEXEC;
4867 if (path && (mode & S_IFREG)) {
4868 const WCHAR *end = path + lstrlenW(path);
4869 while (path < end) {
4870 end = CharPrevW(path, end);
4872 if ((_wcsicmp(end, L
".bat") == 0) ||
4873 (_wcsicmp(end, L
".cmd") == 0) ||
4874 (_wcsicmp(end, L
".com") == 0) ||
4875 (_wcsicmp(end, L
".exe") == 0)) {
4883 mode |= (mode & 0700) >> 3;
4884 mode |= (mode & 0700) >> 6;
4893 WIN32_FIND_DATAW fd;
4895 WCHAR full[MAX_PATH];
4901 if (!(p = wcsstr(path, L
"...")))
4903 q = p + wcsspn(p, L
".");
4904 if ((p == path || wcschr(L
":/\\", *(p - 1))) &&
4905 (!*q || wcschr(L
":/\\", *q))) {
4912 if (!GetFullPathNameW(path,
sizeof(full) /
sizeof(WCHAR), full, &dmy)) {
4916 if (full[1] == L
':' && !full[3] && GetDriveTypeW(full) != DRIVE_NO_ROOT_DIR)
4920 if (fh == INVALID_HANDLE_VALUE)
4931 WIN32_FIND_DATAW wfd;
4932 WIN32_FILE_ATTRIBUTE_DATA wfa;
4933 const WCHAR *p = path;
4935 memset(st, 0,
sizeof(*st));
4938 if (wcsncmp(p, L
"\\\\?\\", 4) == 0) p += 4;
4939 if (wcspbrk(p, L
"?*")) {
4943 if (GetFileAttributesExW(path, GetFileExInfoStandard, (
void*)&wfa)) {
4944 if (wfa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
4949 st->st_size = ((__int64)wfa.nFileSizeHigh << 32) | wfa.nFileSizeLow;
4958 int e = GetLastError();
4960 if ((e == ERROR_FILE_NOT_FOUND) || (e == ERROR_INVALID_NAME)
4961 || (e == ERROR_PATH_NOT_FOUND || (e == ERROR_BAD_NETPATH))) {
4967 h = FindFirstFileW(path, &wfd);
4968 if (h != INVALID_HANDLE_VALUE) {
4974 st->st_size = ((__int64)wfd.nFileSizeHigh << 32) | wfd.nFileSizeLow;
4982 st->st_dev = st->st_rdev = (iswalpha(path[0]) && path[1] == L
':') ?
4983 towupper(path[0]) - L
'A' : _getdrive() - 1;
5004 WCHAR *buf1, *s, *end;
5013 size = lstrlenW(path) + 2;
5015 for (p = path, s = buf1; *p; p++, s++) {
5023 if (!len || L
'\"' == *(--s)) {
5027 end = buf1 + len - 1;
5032 else if (*end != L
'\\')
5033 lstrcatW(buf1, L
"\\");
5035 else if (*end == L
'\\' || (buf1 + 1 == end && *end == L
':'))
5036 lstrcatW(buf1, L
".");
5080 struct stati64 stat;
5084 if ((stat.st_mode & mode) != mode) {
5095 struct stati64 stat;
5099 if ((stat.st_mode & mode) != mode) {
5110 long upos, lpos, usize, lsize;
5114 if ((lpos = SetFilePointer(h, 0, (upos = 0, &upos),
SEEK_CUR)) == -1L &&
5115 (e = GetLastError())) {
5119 usize = (long)(size >> 32);
5121 if (SetFilePointer(h, lsize, &usize,
SEEK_SET) == (
DWORD)-1L &&
5122 (e = GetLastError())) {
5125 else if (!SetEndOfFile(h)) {
5131 SetFilePointer(h, lpos, &upos,
SEEK_SET);
5141 h = CreateFile(path, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
5142 if (h == INVALID_HANDLE_VALUE) {
5157 h = (HANDLE)_get_osfhandle(fd);
5158 if (h == (HANDLE)-1)
return -1;
5165 _filelengthi64(
int fd)
5170 l = GetFileSize((HANDLE)_get_osfhandle(fd), &u);
5171 if (l == (
DWORD)-1L && (e = GetLastError())) {
5175 return ((
off_t)u << 32) | l;
5180 _lseeki64(
int fd,
off_t offset,
int whence)
5184 HANDLE h = (HANDLE)_get_osfhandle(fd);
5190 u = (long)(offset >> 32);
5191 if ((l = SetFilePointer(h, (
long)offset, &u, whence)) == -1L &&
5192 (e = GetLastError())) {
5196 return ((
off_t)u << 32) | l;
5204 __int64 qw = ft->dwHighDateTime;
5206 qw |= ft->dwLowDateTime;
5215 FILETIME create, exit, kernel, user;
5217 if (GetProcessTimes(GetCurrentProcess(),&create, &exit, &kernel, &user)) {
5232 #define yield_once() Sleep(0)
5233 #define yield_until(condition) do yield_once(); while (!(condition))
5243 #if defined __BORLANDC__
5247 read(
int fd,
void *
buf,
size_t size)
5249 int ret = _read(fd, buf, size);
5250 if ((ret < 0) && (
errno == EPIPE)) {
5260 #define FILE_COUNT _cnt
5261 #define FILE_READPTR _ptr
5270 c = (
unsigned char)*stream->FILE_READPTR++;
5273 c = _filbuf(stream);
5274 #if defined __BORLANDC__
5275 if ((c ==
EOF) && (
errno == EPIPE)) {
5290 c = (
unsigned char)(*stream->FILE_READPTR++ = (
char)c);
5293 c = _flsbuf(c, stream);
5330 BOOL interrupted =
FALSE;
5351 if (TerminateThread(thr, intrval)) {
5356 GetExitCodeThread(thr, &val);
5361 MEMORY_BASIC_INFORMATION m;
5363 memset(&m, 0,
sizeof(m));
5364 if (!VirtualQuery(arg.
stackaddr, &m,
sizeof(m))) {
5365 Debug(fprintf(stderr,
"couldn't get stack base:%p:%d\n",
5368 else if (!VirtualFree(m.AllocationBase, 0, MEM_RELEASE)) {
5369 Debug(fprintf(stderr,
"couldn't release stack:%p:%d\n",
5370 m.AllocationBase, GetLastError()));
5381 rb_fatal(
"failed to launch waiter thread:%ld", GetLastError());
5391 WCHAR *envtop, *
env;
5392 char **myenvtop, **myenv;
5405 envtop = GetEnvironmentStringsW();
5406 for (env = envtop, num = 0; *
env; env += lstrlenW(env) + 1)
5407 if (*env !=
'=') num++;
5409 myenvtop = (
char **)
malloc(
sizeof(
char *) * (num + 1));
5410 for (env = envtop, myenv = myenvtop; *
env; env += lstrlenW(env) + 1) {
5419 FreeEnvironmentStringsW(envtop);
5430 while (*t)
free(*t++);
5438 return GetCurrentProcessId();
5446 typedef long (WINAPI query_func)(HANDLE, int,
void *,
ULONG, ULONG *);
5447 static query_func *pNtQueryInformationProcess =
NULL;
5451 if (!pNtQueryInformationProcess)
5452 pNtQueryInformationProcess = (query_func *)
get_proc_address(
"ntdll.dll",
"NtQueryInformationProcess",
NULL);
5453 if (pNtQueryInformationProcess) {
5456 void* PebBaseAddress;
5463 long ret = pNtQueryInformationProcess(GetCurrentProcess(), 0, &pbi,
sizeof(pbi), &len);
5465 ppid = pbi.ParentProcessId;
5473 STATIC_ASSERT(std_handle, (STD_OUTPUT_HANDLE-STD_INPUT_HANDLE)==(STD_ERROR_HANDLE-STD_OUTPUT_HANDLE));
5476 #define set_new_std_handle(newfd, handle) do { \
5477 if ((unsigned)(newfd) > 2) break; \
5478 SetStdHandle(STD_INPUT_HANDLE+(STD_OUTPUT_HANDLE-STD_INPUT_HANDLE)*(newfd), \
5481 #define set_new_std_fd(newfd) set_new_std_handle(newfd, (HANDLE)rb_w32_get_osfhandle(newfd))
5489 if (oldfd == newfd)
return newfd;
5490 ret =
dup2(oldfd, newfd);
5504 va_start(arg, oflag);
5505 pmode = va_arg(arg,
int);
5519 DWORD attr = GetFileAttributesW(wfile);
5520 if (attr == (
DWORD)-1L ||
5521 !(attr & FILE_ATTRIBUTE_DIRECTORY) ||
5552 va_start(arg, oflag);
5553 pmode = va_arg(arg,
int);
5556 if ((oflag & O_TEXT) || !(oflag &
O_BINARY)) {
5557 ret = _open(file, oflag, pmode);
5576 DWORD attr = FILE_ATTRIBUTE_NORMAL;
5577 SECURITY_ATTRIBUTES sec;
5580 if ((oflag & O_TEXT) || !(oflag &
O_BINARY)) {
5583 va_start(arg, oflag);
5584 pmode = va_arg(arg,
int);
5586 fd = _wopen(file, oflag, pmode);
5591 sec.nLength =
sizeof(sec);
5592 sec.lpSecurityDescriptor =
NULL;
5593 if (oflag & O_NOINHERIT) {
5594 sec.bInheritHandle =
FALSE;
5598 sec.bInheritHandle =
TRUE;
5600 oflag &= ~O_NOINHERIT;
5603 oflag &= ~(O_BINARY | O_TEXT);
5605 switch (oflag & (O_RDWR | O_RDONLY | O_WRONLY)) {
5607 access = GENERIC_READ | GENERIC_WRITE;
5610 access = GENERIC_READ;
5613 access = GENERIC_WRITE;
5619 oflag &= ~(O_RDWR | O_RDONLY | O_WRONLY);
5621 switch (oflag & (O_CREAT | O_EXCL | O_TRUNC)) {
5623 create = OPEN_ALWAYS;
5627 create = OPEN_EXISTING;
5629 case O_CREAT | O_EXCL:
5630 case O_CREAT | O_EXCL | O_TRUNC:
5631 create = CREATE_NEW;
5634 case O_TRUNC | O_EXCL:
5635 create = TRUNCATE_EXISTING;
5637 case O_CREAT | O_TRUNC:
5638 create = CREATE_ALWAYS;
5644 if (oflag & O_CREAT) {
5647 va_start(arg, oflag);
5648 pmode = va_arg(arg,
int);
5651 if (!(pmode & S_IWRITE))
5652 attr = FILE_ATTRIBUTE_READONLY;
5654 oflag &= ~(O_CREAT | O_EXCL | O_TRUNC);
5656 if (oflag & O_TEMPORARY) {
5657 attr |= FILE_FLAG_DELETE_ON_CLOSE;
5660 oflag &= ~O_TEMPORARY;
5662 if (oflag & _O_SHORT_LIVED)
5663 attr |= FILE_ATTRIBUTE_TEMPORARY;
5664 oflag &= ~_O_SHORT_LIVED;
5666 switch (oflag & (O_SEQUENTIAL | O_RANDOM)) {
5670 attr |= FILE_FLAG_SEQUENTIAL_SCAN;
5673 attr |= FILE_FLAG_RANDOM_ACCESS;
5679 oflag &= ~(O_SEQUENTIAL | O_RANDOM);
5681 if (oflag & ~O_APPEND) {
5688 h = CreateFile(
"NUL", 0, 0,
NULL, OPEN_ALWAYS, 0,
NULL);
5689 fd = _open_osfhandle((
intptr_t)h, 0);
5701 h = CreateFileW(file, access, FILE_SHARE_READ | FILE_SHARE_WRITE, &sec,
5702 create, attr,
NULL);
5703 if (h == INVALID_HANDLE_VALUE) {
5704 DWORD e = GetLastError();
5712 switch (GetFileType(h)) {
5713 case FILE_TYPE_CHAR:
5716 case FILE_TYPE_PIPE:
5719 case FILE_TYPE_UNKNOWN:
5726 if (!(flags & (
FDEV |
FPIPE)) && (oflag & O_APPEND))
5746 int save_errno =
errno;
5748 if (fflush(fp))
return -1;
5756 if (closesocket(sock) == SOCKET_ERROR) {
5767 static DWORD serial = 0;
5768 static const char prefix[] =
"\\\\.\\pipe\\ruby";
5770 width_of_prefix = (int)
sizeof(prefix) - 1,
5771 width_of_pid = (int)
sizeof(rb_pid_t) * 2,
5772 width_of_serial = (int)
sizeof(serial) * 2,
5773 width_of_ids = width_of_pid + 1 + width_of_serial + 1
5775 char name[
sizeof(prefix) + width_of_ids];
5776 SECURITY_ATTRIBUTES sec;
5777 HANDLE hRead, hWrite, h;
5778 int fdRead, fdWrite;
5783 return _pipe(fds, 65536L, _O_NOINHERIT);
5785 memcpy(
name, prefix, width_of_prefix);
5786 snprintf(
name + width_of_prefix, width_of_ids,
"%.*"PRI_PIDT_PREFIX
"x-%.*lx",
5789 sec.nLength =
sizeof(sec);
5790 sec.lpSecurityDescriptor =
NULL;
5791 sec.bInheritHandle =
FALSE;
5794 hRead = CreateNamedPipe(
name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
5795 0, 2, 65536, 65536, 0, &sec);
5797 if (hRead == INVALID_HANDLE_VALUE) {
5799 if (err == ERROR_PIPE_BUSY)
5807 hWrite = CreateFile(
name, GENERIC_READ | GENERIC_WRITE, 0, &sec,
5808 OPEN_EXISTING, FILE_FLAG_OVERLAPPED,
NULL);
5810 if (hWrite == INVALID_HANDLE_VALUE) {
5818 h = CreateFile(
"NUL", 0, 0,
NULL, OPEN_ALWAYS, 0,
NULL);
5819 fdRead = _open_osfhandle((
intptr_t)h, 0);
5823 CloseHandle(hWrite);
5838 h = CreateFile(
"NUL", 0, 0,
NULL, OPEN_ALWAYS, 0,
NULL);
5839 fdWrite = _open_osfhandle((
intptr_t)h, 0);
5841 if (fdWrite == -1) {
5843 CloseHandle(hWrite);
5870 const void *
const func = WriteConsoleW;
5872 MEMORY_BASIC_INFORMATION m;
5874 memset(&m, 0,
sizeof(m));
5875 if (!VirtualQuery(func, &m,
sizeof(m))) {
5878 k = GetModuleHandle(
"kernel32.dll");
5879 if (!k)
return FALSE;
5880 return (HMODULE)m.AllocationBase != k;
5904 CONSOLE_SCREEN_BUFFER_INFO csbi;
5907 p->
vt100.
attr = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
5909 if (GetConsoleScreenBufferInfo(h, &csbi)) {
5923 if (!conlist)
return;
5933 #define FOREGROUND_MASK (FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED)
5934 #define BACKGROUND_MASK (BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED)
5935 WORD bold = attr & (FOREGROUND_INTENSITY | BACKGROUND_INTENSITY);
5938 if (!count)
return attr;
5939 while (count-- > 0) {
5942 attr = default_attr;
5947 bold |= rev ? BACKGROUND_INTENSITY : FOREGROUND_INTENSITY;
5950 #ifndef COMMON_LVB_UNDERSCORE
5951 #define COMMON_LVB_UNDERSCORE 0x8000
5960 attr &= ~(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
5964 attr = attr & ~(FOREGROUND_BLUE | FOREGROUND_GREEN) | FOREGROUND_RED;
5968 attr = attr & ~(FOREGROUND_BLUE | FOREGROUND_RED) | FOREGROUND_GREEN;
5972 attr = attr & ~FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
5976 attr = attr & ~(FOREGROUND_GREEN | FOREGROUND_RED) | FOREGROUND_BLUE;
5980 attr = attr & ~FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED;
5984 attr = attr & ~FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
5988 attr |= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
5992 attr &= ~(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
5995 attr = attr & ~(BACKGROUND_BLUE | BACKGROUND_GREEN) | BACKGROUND_RED;
5998 attr = attr & ~(BACKGROUND_BLUE | BACKGROUND_RED) | BACKGROUND_GREEN;
6001 attr = attr & ~BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
6004 attr = attr & ~(BACKGROUND_GREEN | BACKGROUND_RED) | BACKGROUND_BLUE;
6007 attr = attr & ~BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED;
6010 attr = attr & ~BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
6013 attr |= BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED;
6029 CONSOLE_SCREEN_BUFFER_INFO csbi;
6036 if (!GetConsoleScreenBufferInfo(handle, &csbi))
return;
6037 if (count > 0 && seq[0] > 0) arg1 = seq[0];
6043 csbi.dwCursorPosition.X = 0;
6045 csbi.dwCursorPosition.Y -= arg1;
6046 if (csbi.dwCursorPosition.Y < 0)
6047 csbi.dwCursorPosition.Y = 0;
6048 SetConsoleCursorPosition(handle, csbi.dwCursorPosition);
6051 csbi.dwCursorPosition.X = 0;
6054 csbi.dwCursorPosition.Y += arg1;
6055 if (csbi.dwCursorPosition.Y >= csbi.dwSize.Y)
6056 csbi.dwCursorPosition.Y = csbi.dwSize.Y;
6057 SetConsoleCursorPosition(handle, csbi.dwCursorPosition);
6060 csbi.dwCursorPosition.X += arg1;
6061 if (csbi.dwCursorPosition.X >= csbi.dwSize.X)
6062 csbi.dwCursorPosition.X = csbi.dwSize.X;
6063 SetConsoleCursorPosition(handle, csbi.dwCursorPosition);
6066 csbi.dwCursorPosition.X -= arg1;
6067 if (csbi.dwCursorPosition.X < 0)
6068 csbi.dwCursorPosition.X = 0;
6069 SetConsoleCursorPosition(handle, csbi.dwCursorPosition);
6073 csbi.dwCursorPosition.X = (arg1 > csbi.dwSize.X ? csbi.dwSize.X : arg1) - 1;
6074 SetConsoleCursorPosition(handle, csbi.dwCursorPosition);
6077 csbi.dwCursorPosition.Y = (arg1 > csbi.dwSize.Y ? csbi.dwSize.Y : arg1) - 1;
6078 SetConsoleCursorPosition(handle, csbi.dwCursorPosition);
6082 pos.Y = (arg1 > csbi.dwSize.Y ? csbi.dwSize.Y : arg1) - 1;
6083 if (count < 2 || (arg1 = seq[1]) <= 0) arg1 = 1;
6084 pos.X = (arg1 > csbi.dwSize.X ? csbi.dwSize.X : arg1) - 1;
6085 SetConsoleCursorPosition(handle, pos);
6090 FillConsoleOutputCharacterW(handle, L
' ',
6091 csbi.dwSize.X * (csbi.dwSize.Y - csbi.dwCursorPosition.Y) - csbi.dwCursorPosition.X,
6092 csbi.dwCursorPosition, &written);
6096 pos.Y = csbi.dwCursorPosition.Y;
6097 FillConsoleOutputCharacterW(handle, L
' ',
6098 csbi.dwSize.X * csbi.dwCursorPosition.Y + csbi.dwCursorPosition.X,
6104 FillConsoleOutputCharacterW(handle, L
' ', csbi.dwSize.X * csbi.dwSize.Y, pos, &written);
6111 FillConsoleOutputCharacterW(handle, L
' ', csbi.dwSize.X - csbi.dwCursorPosition.X, csbi.dwCursorPosition, &written);
6115 pos.Y = csbi.dwCursorPosition.Y;
6116 FillConsoleOutputCharacterW(handle, L
' ', csbi.dwCursorPosition.X, pos, &written);
6120 pos.Y = csbi.dwCursorPosition.Y;
6121 FillConsoleOutputCharacterW(handle, L
' ', csbi.dwSize.X, pos, &written);
6129 SetConsoleCursorPosition(handle, s->
vt100.
saved);
6132 if (count >= 2 && seq[0] == -1 && seq[1] == 25) {
6133 CONSOLE_CURSOR_INFO cci;
6134 GetConsoleCursorInfo(handle, &cci);
6135 cci.bVisible =
TRUE;
6136 SetConsoleCursorInfo(handle, &cci);
6140 if (count >= 2 && seq[0] == -1 && seq[1] == 25) {
6141 CONSOLE_CURSOR_INFO cci;
6142 GetConsoleCursorInfo(handle, &cci);
6143 cci.bVisible =
FALSE;
6144 SetConsoleCursorInfo(handle, &cci);
6154 const WCHAR *ptr = *ptrp;
6155 long rest, len = *lenp;
6159 rest = *lenp - len - 1;
6164 if (len > 0 && *ptr != L
'[')
continue;
6173 rest = *lenp - len - 1;
6174 if (rest > 0) --rest;
6179 if (wc >= L
'0' && wc <= L
'9') {
6182 *seq = (*seq * 10) + (wc - L
'0');
6223 int save_errno =
errno;
6234 if (closesocket(sock) == SOCKET_ERROR) {
6244 memset(ol, 0,
sizeof(*ol));
6248 DWORD low = SetFilePointer((HANDLE)
_osfhnd(fd), 0, &high, method);
6249 #ifndef INVALID_SET_FILE_POINTER
6250 #define INVALID_SET_FILE_POINTER ((DWORD)-1)
6254 if (err != NO_ERROR) {
6260 ol->OffsetHigh = high;
6273 CloseHandle(ol->hEvent);
6276 LONG high = ol->OffsetHigh;
6278 if (low < ol->Offset)
6280 SetFilePointer((HANDLE)
_osfhnd(fd), low, &high, FILE_BEGIN);
6295 OVERLAPPED ol, *pol =
NULL;
6297 BOOL islineinput =
FALSE;
6304 if (_get_osfhandle(fd) == -1) {
6309 return _read(fd, buf, size);
6324 GetConsoleMode((HANDLE)
_osfhnd(fd),&mode);
6325 islineinput = (mode & ENABLE_LINE_INPUT) != 0;
6352 if (!ReadFile((HANDLE)
_osfhnd(fd), buf, len, &read, pol)) {
6353 err = GetLastError();
6354 if (err != ERROR_IO_PENDING) {
6355 if (pol) CloseHandle(ol.hEvent);
6356 if (err == ERROR_ACCESS_DENIED)
6358 else if (err == ERROR_BROKEN_PIPE || err == ERROR_HANDLE_EOF) {
6371 if (wait != WAIT_OBJECT_0) {
6372 if (wait == WAIT_OBJECT_0 + 1)
6376 CloseHandle(ol.hEvent);
6382 if (!GetOverlappedResult((HANDLE)
_osfhnd(fd), &ol, &read,
TRUE) &&
6383 (err = GetLastError()) != ERROR_HANDLE_EOF) {
6385 if (err != ERROR_BROKEN_PIPE) {
6389 CloseHandle(ol.hEvent);
6397 err = GetLastError();
6407 buf = (
char *)buf + read;
6408 if (err != ERROR_OPERATION_ABORTED &&
6409 !(isconsole && len == 1 && (!islineinput || *((
char *)buf - 1) ==
'\n')) && size > 0)
6432 OVERLAPPED ol, *pol =
NULL;
6438 if (_get_osfhandle(fd) == -1) {
6444 return _write(fd, buf, size);
6470 if (!WriteFile((HANDLE)
_osfhnd(fd), buf, len, &written, pol)) {
6471 err = GetLastError();
6472 if (err != ERROR_IO_PENDING) {
6473 if (pol) CloseHandle(ol.hEvent);
6474 if (err == ERROR_ACCESS_DENIED)
6485 if (wait != WAIT_OBJECT_0) {
6486 if (wait == WAIT_OBJECT_0 + 1)
6490 CloseHandle(ol.hEvent);
6496 if (!GetOverlappedResult((HANDLE)
_osfhnd(fd), &ol, &written,
6499 CloseHandle(ol.hEvent);
6512 if (written == len) {
6513 buf = (
const char *)buf + len;
6529 DWORD dwMode, reslen;
6533 const WCHAR *ptr, *next;
6537 if (disable)
return -1L;
6539 if (!GetConsoleMode(handle, &dwMode))
6552 case ENCINDEX_US_ASCII:
6553 case ENCINDEX_ASCII:
6555 case ENCINDEX_UTF_8:
6557 if (!ptr)
return -1L;
6559 case ENCINDEX_UTF_16LE:
6565 long curlen =
constat_parse(handle, s, (next = ptr, &next), &len);
6567 if (!WriteConsoleW(handle, ptr, curlen, &reslen,
NULL)) {
6568 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
6570 reslen = (
DWORD)-1L;
6577 if (wbuffer)
free(wbuffer);
6578 return (
long)reslen;
6587 tmp.QuadPart = ((LONG_LONG)time + (LONG_LONG)((1970-1601)*365.2425) * 24 * 60 * 60) * 10 * 1000 * 1000;
6588 ft->dwLowDateTime = tmp.LowPart;
6589 ft->dwHighDateTime = tmp.HighPart;
6598 FILETIME atime, mtime;
6599 struct stati64 stat;
6615 GetSystemTimeAsFileTime(&atime);
6620 const DWORD attr = GetFileAttributesW(path);
6621 if (attr != (
DWORD)-1 && (attr & FILE_ATTRIBUTE_READONLY))
6622 SetFileAttributesW(path, attr & ~FILE_ATTRIBUTE_READONLY);
6623 hFile = CreateFileW(path, GENERIC_WRITE, 0, 0, OPEN_EXISTING,
6624 FILE_FLAG_BACKUP_SEMANTICS, 0);
6625 if (hFile == INVALID_HANDLE_VALUE) {
6630 if (!SetFileTime(hFile,
NULL, &atime, &mtime)) {
6636 if (attr != (
DWORD)-1 && (attr & FILE_ATTRIBUTE_READONLY))
6637 SetFileAttributesW(path, attr);
6652 ret =
wutime(wpath, times);
6666 ret =
wutime(wpath, times);
6680 ret = _wchdir(wpath);
6692 if (CreateDirectoryW(wpath,
NULL) ==
FALSE) {
6696 if (_wchmod(wpath, mode) == -1) {
6697 RemoveDirectoryW(wpath);
6714 ret =
wmkdir(wpath, mode);
6728 ret =
wmkdir(wpath, mode);
6739 const DWORD attr = GetFileAttributesW(wpath);
6740 if (attr != (
DWORD)-1 && (attr & FILE_ATTRIBUTE_READONLY)) {
6741 SetFileAttributesW(wpath, attr & ~FILE_ATTRIBUTE_READONLY);
6743 if (RemoveDirectoryW(wpath) ==
FALSE) {
6746 if (attr != (
DWORD)-1 && (attr & FILE_ATTRIBUTE_READONLY)) {
6747 SetFileAttributesW(wpath, attr);
6788 const DWORD attr = GetFileAttributesW(path);
6789 if (attr != (
DWORD)-1 && (attr & FILE_ATTRIBUTE_READONLY)) {
6790 SetFileAttributesW(path, attr & ~FILE_ATTRIBUTE_READONLY);
6792 if (!DeleteFileW(path)) {
6795 if (attr != (
DWORD)-1 && (attr & FILE_ATTRIBUTE_READONLY)) {
6796 SetFileAttributesW(path, attr);
6840 ret = _wchmod(wpath, mode);
6845 #if !defined(__BORLANDC__)
6853 if (_get_osfhandle(fd) == -1) {
6856 if (!GetConsoleMode((HANDLE)
_osfhnd(fd), &mode)) {
6871 too_many_files(
void)
6874 for (f = _streams; f < _streams + _nfile; f++) {
6875 if (f->fd < 0)
return 0;
6883 rb_w32_fopen(
const char *path,
const char *mode)
6885 FILE *f = (
errno = 0, fopen(path, mode));
6887 if (too_many_files())
6895 rb_w32_fdopen(
int handle,
const char *
type)
6897 FILE *f = (
errno = 0, _fdopen(handle, (
char *)type));
6901 else if (too_many_files())
6909 rb_w32_fsopen(
const char *path,
const char *mode,
int shflags)
6911 FILE *f = (
errno = 0, _fsopen(path, mode, shflags));
6913 if (too_many_files())
6920 #if defined(_MSC_VER) && RUBY_MSVCRT_VERSION <= 60
6921 extern long _ftol(
double);
6931 _ftol2_sse(
double d)
6942 int *ip = (
int *)(&x + 1) - 1;
6951 typedef char *(WSAAPI inet_ntop_t)(
int,
void *,
char *,
size_t);
6952 inet_ntop_t *pInetNtop;
6955 return pInetNtop(af, (
void *)addr, numaddr, numaddr_len);
6959 memcpy(&in.s_addr, addr,
sizeof(in.s_addr));
6960 snprintf(numaddr, numaddr_len,
"%s", inet_ntoa(in));
6969 typedef int (WSAAPI inet_pton_t)(int,
const char*,
void *);
6970 inet_pton_t *pInetPton;
6973 return pInetPton(af, src, dst);
6985 #if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__GMTIME64_S)
6992 if (!FileTimeToSystemTime(&ft, st))
return -1;
7000 int y = st->wYear, m = st->wMonth, d = st->wDay;
7001 t->tm_sec = st->wSecond;
7002 t->tm_min = st->wMinute;
7003 t->tm_hour = st->wHour;
7004 t->tm_mday = st->wDay;
7005 t->tm_mon = st->wMonth - 1;
7006 t->tm_year = y - 1900;
7007 t->tm_wday = st->wDayOfWeek;
7015 d += 31 + 28 + (!(y % 4) && ((y % 100) || !(y % 400)));
7016 d += ((m - 3) * 153 + 2) / 5;
7026 TIME_ZONE_INFORMATION stdtz;
7029 if (!SystemTimeToTzSpecificLocalTime(tz, gst, lst))
return -1;
7031 GetTimeZoneInformation(&stdtz);
7034 if (tz->StandardBias == tz->DaylightBias)
return 0;
7035 if (!tz->StandardDate.wMonth)
return 0;
7036 if (!tz->DaylightDate.wMonth)
return 0;
7037 if (tz != &stdtz) stdtz = *tz;
7039 stdtz.StandardDate.wMonth = stdtz.DaylightDate.wMonth = 0;
7040 if (!SystemTimeToTzSpecificLocalTime(&stdtz, gst, &sst))
return 0;
7041 if (lst->wMinute == sst.wMinute && lst->wHour == sst.wHour)
7047 #ifdef HAVE__GMTIME64_S
7048 # ifndef HAVE__LOCALTIME64_S
7050 # define HAVE__LOCALTIME64_S 1
7052 # ifndef MINGW_HAS_SECURE_API
7053 _CRTIMP errno_t __cdecl _gmtime64_s(
struct tm* tm,
const __time64_t *time);
7054 _CRTIMP errno_t __cdecl _localtime64_s(
struct tm* tm,
const __time64_t *time);
7056 # define gmtime_s _gmtime64_s
7057 # define localtime_s _localtime64_s
7070 #if RUBY_MSVCRT_VERSION >= 80 || defined(HAVE__GMTIME64_S)
7071 e = gmtime_s(rp, tp);
7072 if (e != 0)
goto error;
7094 #if RUBY_MSVCRT_VERSION >= 80 || defined(HAVE__LOCALTIME64_S)
7095 e = localtime_s(rp, tp);
7099 SYSTEMTIME gst, lst;
7113 int len =
sizeof(tmp);
7114 int r = getsockopt((SOCKET)h, SOL_SOCKET, SO_DEBUG, (
char *)&tmp, &len);
7115 if (r != SOCKET_ERROR || WSAGetLastError() != WSAENOTSOCK) {
7118 flags &= ~O_NONBLOCK;
7146 #if !defined(__MINGW64__) && defined(__MINGW64_VERSION_MAJOR)
7152 rb_w32_pow(
double x,
double y)
7156 unsigned int default_control = _controlfp(0, 0);
7157 _controlfp(_PC_64, _MCW_PC);
7160 _controlfp(default_control, _MCW_PC);
struct tm * localtime_r(const time_t *tp, struct tm *rp)
static const char * NTLoginName
void setnetent(int stayopen)
rb_pid_t rb_w32_uspawn(int mode, const char *cmd, const char *prog)
static void regulate_path(WCHAR *path)
static int free_conlist(st_data_t key, st_data_t val, st_data_t arg)
static rb_pid_t w32_aspawn_flags(int mode, const char *prog, char *const *argv, DWORD flags, UINT cp)
static struct ChildRecord * FindFreeChildSlot(void)
char * rb_w32_ugetenv(const char *name)
rb_pid_t rb_w32_getppid(void)
rb_pid_t rb_w32_getpid(void)
static time_t filetime_to_unixtime(const FILETIME *ft)
static char * skipspace(char *ptr)
int rb_w32_wait_events_blocking(HANDLE *events, int num, DWORD timeout)
int rb_w32_map_errno(DWORD winerr)
static void init_stdhandle(void)
size_t strlen(const char *)
#define rb_w32_stati64(path, st)
int rb_w32_wait_events(HANDLE *events, int num, DWORD timeout)
int rb_w32_access(const char *path, int mode)
#define CSIDL_LOCAL_APPDATA
struct servent *WSAAPI rb_w32_getservbyport(int port, const char *proto)
int rb_w32_umkdir(const char *path, int mode)
EXTERN_C _CRTIMP ioinfo * __pioinfo[]
STATIC_ASSERT(std_handle,(STD_OUTPUT_HANDLE-STD_INPUT_HANDLE)==(STD_ERROR_HANDLE-STD_OUTPUT_HANDLE))
static int is_command_com(const char *interp)
void rb_w32_fd_dup(rb_fdset_t *dst, const rb_fdset_t *src)
DWORD(WINAPI * cilnA_t)(const NET_LUID *, char *, size_t)
static int max(int a, int b)
static void StartSockets(void)
static FARPROC get_wsa_extension_function(SOCKET s, GUID *guid)
#define access(path, mode)
static void init_func(void)
int rb_w32_pipe(int fds[2])
int rb_w32_wopen(const WCHAR *file, int oflag,...)
static ioinfo * _pioinfo(int)
static void catch_interrupt(void)
static void get_version(void)
static struct direct * readdir_internal(DIR *dirp, BOOL(*conv)(const WCHAR *, struct direct *, rb_encoding *), rb_encoding *enc)
static int compare(const struct timeval *t1, const struct timeval *t2)
ssize_t rb_w32_read(int fd, void *buf, size_t size)
VALUE rb_str_cat(VALUE, const char *, long)
static rb_pid_t poll_child_status(struct ChildRecord *child, int *stat_loc)
static int NtSocketsInitialized
int st_insert(st_table *, st_data_t, st_data_t)
int WSAAPI rb_w32_connect(int s, const struct sockaddr *addr, int addrlen)
DIR * rb_w32_uopendir(const char *filename)
int clock_getres(clockid_t clock_id, struct timespec *sp)
void rb_write_error2(const char *, long)
int rb_w32_io_cancelable_p(int fd)
void rb_w32_rewinddir(DIR *dirp)
static unsigned fileattr_to_unixmode(DWORD attr, const WCHAR *path)
void rb_w32_fdset(int fd, fd_set *set)
int rb_w32_truncate(const char *path, off_t length)
static void constat_apply(HANDLE handle, struct constat *s, WCHAR w)
static st_table * socklist
int rb_w32_unwrap_io_handle(int fd)
int WSAAPI rb_w32_bind(int s, const struct sockaddr *addr, int addrlen)
static int is_batch(const char *cmd)
struct servent *WSAAPI rb_w32_getservbyname(const char *name, const char *proto)
#define INVALID_SET_FILE_POINTER
void st_free_table(st_table *)
rb_pid_t rb_w32_aspawn(int mode, const char *prog, char *const *argv)
static int extract_fd(rb_fdset_t *dst, fd_set *src, int(*func)(SOCKET))
int WSAAPI rb_w32_accept(int s, struct sockaddr *addr, int *addrlen)
void freeifaddrs(struct ifaddrs *ifp)
static int is_socket(SOCKET)
static int wmkdir(const WCHAR *wpath, int mode)
static long filetime_to_clock(FILETIME *ft)
#define COPY_STAT(src, dest, size_cast)
static int check_if_dir(const char *file)
static int is_invalid_handle(SOCKET sock)
void rb_w32_seekdir(DIR *dirp, long loc)
int rb_w32_urmdir(const char *path)
static WCHAR * translate_wchar(WCHAR *p, int from, int to)
#define STRNDUPV(ptr, v, src, len)
static void exit_handler(void)
struct _NtCmdLineElement * next
int rb_w32_fclose(FILE *fp)
int rb_w32_open(const char *file, int oflag,...)
struct sockaddr * ifa_addr
#define msghdr_to_wsamsg(msg, wsamsg)
struct protoent * getprotoent(void)
static FARPROC get_proc_address(const char *module, const char *func, HANDLE *mh)
static int socklist_insert(SOCKET sock, int flag)
char * rb_w32_conv_from_wstr(const WCHAR *wstr, long *lenp, rb_encoding *enc)
ssize_t rb_w32_write(int fd, const void *buf, size_t size)
#define filecp_to_wstr(str, plen)
VALUE rb_str_conv_enc_opts(VALUE str, rb_encoding *from, rb_encoding *to, int ecflags, VALUE ecopts)
int WSAAPI rb_w32_sendto(int fd, const char *buf, int len, int flags, const struct sockaddr *to, int tolen)
static SOCKET open_ifs_socket(int af, int type, int protocol)
const char *WSAAPI rb_w32_inet_ntop(int af, const void *addr, char *numaddr, size_t numaddr_len)
RUBY_EXTERN void * memmove(void *, const void *, size_t)
int recvmsg(int fd, struct msghdr *msg, int flags)
struct protoent *WSAAPI rb_w32_getprotobynumber(int num)
static BOOL ruby_direct_conv(const WCHAR *file, struct direct *entry, rb_encoding *enc)
int rb_w32_wrap_io_handle(HANDLE h, int flags)
RUBY_SYMBOL_EXPORT_BEGIN typedef unsigned long st_data_t
int rb_w32_ustati64(const char *path, struct stati64 *st)
#define RUBY_CRITICAL(expr)
static uintptr_t flock_winnt(uintptr_t self, int argc, uintptr_t *argv)
static BOOL win32_direct_conv(const WCHAR *file, struct direct *entry, rb_encoding *dummy)
int fcntl(int fd, int cmd,...)
void * ruby_xcalloc(size_t n, size_t size)
int WSAAPI rb_w32_socket(int af, int type, int protocol)
static int internal_cmd_match(const char *cmdname, int nt)
int st_lookup(st_table *, st_data_t, st_data_t *)
#define wstr_to_utf8(str, plen)
#define MEMZERO(p, type, n)
unsigned long long uint64_t
VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc)
BOOL(WINAPI * cancel_io_t)(HANDLE)
struct ifaddrs * ifa_next
char * rb_w32_getenv(const char *name)
rb_pid_t waitpid(rb_pid_t pid, int *stat_loc, int options)
static int is_pipe(SOCKET sock)
uintptr_t(* asynchronous_func_t)(uintptr_t self, int argc, uintptr_t *argv)
#define END_FOREACH_CHILD
#define wstr_to_filecp(str, plen)
int rb_w32_stat(const char *path, struct stat *st)
int rb_w32_uaccess(const char *path, int mode)
static struct @171 errmap[]
#define ECONV_INVALID_REPLACE
long rb_w32_telldir(DIR *dirp)
static rb_pid_t w32_spawn(int mode, const char *cmd, const char *prog, UINT cp)
int rb_w32_fstat(int fd, struct stat *st)
int rb_econv_has_convpath_p(const char *from_encoding, const char *to_encoding)
static int copy_fd(fd_set *dst, fd_set *src)
static void CloseChildHandle(struct ChildRecord *child)
static int winnt_stat(const WCHAR *path, struct stati64 *st)
int rb_w32_uopen(const char *file, int oflag,...)
static char * w32_getenv(const char *name, UINT cp)
#define IOINFO_ARRAY_ELTS
int rb_w32_uchdir(const char *path)
void rb_w32_fd_copy(rb_fdset_t *dst, const fd_set *src, int max)
int st_delete(st_table *, st_data_t *, st_data_t *)
int link(const char *from, const char *to)
int WSAAPI rb_w32_ioctlsocket(int s, long cmd, u_long *argp)
struct netent * getnetbyname(const char *name)
static int wutime(const WCHAR *path, const struct utimbuf *times)
static HANDLE open_dir_handle(const WCHAR *filename, WIN32_FIND_DATAW *fd)
int rb_w32_isatty(int fd)
int rb_w32_sleep(unsigned long msec)
int rb_w32_rename(const char *from, const char *to)
#define ALLOCV_N(type, v, n)
int clock_gettime(clockid_t clock_id, struct timespec *sp)
UINT rb_w32_system_tmpdir(WCHAR *path, UINT len)
DWORD(WINAPI * cigl_t)(const GUID *, NET_LUID *)
int rb_w32_select_with_thread(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, struct timeval *timeout, void *th)
#define MAKE_SOCKDATA(af, fl)
int rb_w32_utime(const char *path, const struct utimbuf *times)
if((ID)(DISPID) nameid!=nameid)
static int setup_overlapped(OVERLAPPED *ol, int fd)
static int socklist_lookup(SOCKET sock, int *flagp)
uintptr_t(* func)(uintptr_t self, int argc, uintptr_t *argv)
rb_pid_t rb_w32_aspawn_flags(int mode, const char *prog, char *const *argv, DWORD flags)
void sethostent(int stayopen)
struct servent * getservent(void)
static int check_spawn_mode(int mode)
static cancel_io_t cancel_io
struct constat::@173 vt100
static cilnA_t pConvertInterfaceLuidToNameA
static void init_env(void)
static int filetime_to_timeval(const FILETIME *ft, struct timeval *tv)
static int is_not_socket(SOCKET sock)
SSL_METHOD *(* func)(void)
static int do_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, struct timeval *timeout)
static const char *const szInternalCmds[]
char * rb_w32_wstr_to_mbstr(UINT cp, const WCHAR *wstr, int clen, long *plen)
static int w32_stati64(const char *path, struct stati64 *st, UINT cp)
int WSAAPI rb_w32_getpeername(int s, struct sockaddr *addr, int *addrlen)
VALUE rb_sprintf(const char *format,...)
void setservent(int stayopen)
rb_pid_t rb_w32_spawn(int mode, const char *cmd, const char *prog)
DIR * rb_w32_opendir(const char *filename)
static long constat_parse(HANDLE h, struct constat *s, const WCHAR **ptrp, long *lenp)
static int has_redirection(const char *, UINT)
void rb_w32_sysinit(int *argc, char ***argv)
void rb_fatal(const char *fmt,...)
static struct ChildRecord * FindChildSlot(rb_pid_t pid)
static cigl_t pConvertInterfaceGuidToLuid
static char * translate_char(char *p, int from, int to, UINT cp)
int WSAAPI rb_w32_shutdown(int s, int how)
VALUE rb_str_vcatf(VALUE, const char *, va_list)
static int console_emulator_p(void)
static int finish_overlapped_socket(BOOL input, SOCKET s, WSAOVERLAPPED *wol, int result, DWORD *len, DWORD size)
static CRITICAL_SECTION select_mutex
struct netent * getnetent(void)
void setprotoent(int stayopen)
static int unixtime_to_systemtime(const time_t t, SYSTEMTIME *st)
int sendmsg(int fd, const struct msghdr *msg, int flags)
int ioctl(int i, int u,...)
unsigned char buf[MIME_BUF_SIZE]
static int rb_chsize(HANDLE h, off_t size)
struct tm * gmtime_r(const time_t *tp, struct tm *rp)
static int join_argv(char *cmd, char *const *argv, BOOL escape, UINT cp, int backslash)
int flock(int fd, int oper)
int __cdecl gettimeofday(struct timeval *tv, struct timezone *tz)
static int options(unsigned char *cp)
static int unixtime_to_filetime(time_t time, FILETIME *ft)
int rb_w32_getc(FILE *stream)
char * strchr(char *, char)
struct protoent *WSAAPI rb_w32_getprotobyname(const char *name)
#define utf8_to_wstr(str, plen)
int rb_w32_mkdir(const char *path, int mode)
RUBY_EXTERN size_t strlcpy(char *, const char *, size_t)
int WSAAPI rb_w32_inet_pton(int af, const char *src, void *dst)
int rb_w32_unlink(const char *path)
int WSAAPI rb_w32_gethostname(char *name, int len)
static int insert(const char *path, VALUE vinfo, void *enc)
static int isUNCRoot(const WCHAR *path)
int chown(const char *path, int owner, int group)
static rb_pid_t child_result(struct ChildRecord *child, int mode)
st_table * st_init_numtable(void)
int rb_w32_fstati64(int fd, struct stati64 *st)
static int internal_match(const void *key, const void *elem)
int rb_w32_putc(int c, FILE *stream)
static UINT get_system_directory(WCHAR *path, UINT len)
char * rb_w32_strerror(int e)
int rb_w32_time_subtract(struct timeval *rest, const struct timeval *wait)
static int is_internal_cmd(const char *cmd, int nt)
int rb_w32_uchown(const char *path, int owner, int group)
int rb_w32_rmdir(const char *path)
int WSAAPI rb_w32_listen(int s, int backlog)
int kill(int pid, int sig)
void * ruby_xmalloc(size_t size)
static void constat_delete(HANDLE h)
#define ECONV_UNDEF_REPLACE
#define ENCODING_GET(obj)
uintptr_t rb_w32_asynchronize(asynchronous_func_t func, uintptr_t self, int argc, uintptr_t *argv, uintptr_t intrval)
static int wstati64(const WCHAR *path, struct stati64 *st)
static struct ChildRecord * FindChildSlotByHandle(HANDLE h)
static NtCmdLineElement ** cmdglob(NtCmdLineElement *patt, NtCmdLineElement **tail)
char ** rb_w32_get_environ(void)
static DIR * opendir_internal(WCHAR *wpath, const char *filename)
rb_pid_t rb_w32_uaspawn_flags(int mode, const char *prog, char *const *argv, DWORD flags)
#define set_new_std_fd(newfd)
void rb_w32_closedir(DIR *dirp)
static int wlink(const WCHAR *from, const WCHAR *to)
static WORD constat_attr(int count, const int *seq, WORD attr, WORD default_attr)
VALUE rb_w32_special_folder(int type)
static int socklist_delete(SOCKET *sockp, int *flagp)
int rb_w32_uchmod(const char *path, int mode)
static DWORD WINAPI call_asynchronous(PVOID argp)
int WSAAPI rb_w32_send(int fd, const char *buf, int len, int flags)
static int wrmdir(const WCHAR *wpath)
static struct constat * constat_handle(HANDLE h)
#define _set_osfhnd(fh, osfh)
static int systemtime_to_localtime(TIME_ZONE_INFORMATION *tz, SYSTEMTIME *gst, SYSTEMTIME *lst)
struct hostent *WSAAPI rb_w32_gethostbyname(const char *name)
RUBY_EXTERN char * strerror(int)
static int is_console(SOCKET)
static int is_readable_pipe(SOCKET sock)
static void str2guid(const char *str, GUID *guid)
int socketpair(int af, int type, int protocol, int *sv)
int WSAAPI rb_w32_getsockname(int fd, struct sockaddr *addr, int *addrlen)
rb_encoding * rb_filesystem_encoding(void)
SOCKET rb_w32_get_osfhandle(int fh)
int rb_w32_uutime(const char *path, const struct utimbuf *times)
struct _NtCmdLineElement NtCmdLineElement
int ruby_brace_glob(const char *str, int flags, ruby_glob_func *func, VALUE arg)
#define set_env_val(vname)
int WSAAPI rb_w32_recv(int fd, char *buf, int len, int flags)
static int check_if_wdir(const WCHAR *wfile)
static int setfl(SOCKET sock, int arg)
rb_pid_t rb_w32_uaspawn(int mode, const char *prog, char *const *argv)
int getifaddrs(struct ifaddrs **ifap)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
int rb_w32_is_socket(int fd)
#define ENC_TO_ENCINDEX(enc)
static int wrename(const WCHAR *oldpath, const WCHAR *newpath)
long rb_w32_write_console(uintptr_t strarg, int fd)
static int dupfd(HANDLE hDup, char flags, int minfd)
RUBY_EXTERN size_t strlcat(char *, const char *, size_t)
#define COMMON_LVB_UNDERSCORE
RUBY_EXTERN int dup2(int, int)
static void move_to_next_entry(DIR *dirp)
static void systemtime_to_tm(const SYSTEMTIME *st, struct tm *t)
char * rb_w32_getcwd(char *buffer, int size)
rb_encoding * rb_ascii8bit_encoding(void)
#define yield_until(condition)
static int overlapped_socket_io(BOOL input, int fd, char *buf, int len, int flags, struct sockaddr *addr, int *addrlen)
static void version(void)
struct netent * getnetbyaddr(long net, int type)
static struct ChildRecord * CreateChild(const WCHAR *, const WCHAR *, SECURITY_ATTRIBUTES *, HANDLE, HANDLE, HANDLE, DWORD)
int WSAAPI rb_w32_select(int nfds, fd_set *rd, fd_set *wr, fd_set *ex, struct timeval *timeout)
int rb_w32_ulink(const char *from, const char *to)
int WSAAPI rb_w32_recvfrom(int fd, char *buf, int len, int flags, struct sockaddr *from, int *fromlen)
int rb_w32_dup2(int oldfd, int newfd)
static int socketpair_internal(int af, int type, int protocol, SOCKET *sv)
int WSAAPI rb_w32_getsockopt(int s, int level, int optname, char *optval, int *optlen)
int rb_w32_urename(const char *from, const char *to)
static void finish_overlapped(OVERLAPPED *ol, int fd, DWORD size)
struct direct * rb_w32_readdir(DIR *dirp, rb_encoding *enc)
void rb_w32_free_environ(char **env)
int WSAAPI rb_w32_setsockopt(int s, int level, int optname, const char *optval, int optlen)
static OSVERSIONINFO osver
int rb_w32_cmdvector(const char *cmd, char ***vec)
WCHAR * rb_w32_mbstr_to_wstr(UINT cp, const char *str, int clen, long *plen)
int rb_w32_fdisset(int fd, fd_set *set)
static int rb_w32_open_osfhandle(intptr_t osfhandle, int flags)
int rb_w32_uunlink(const char *path)
static int is_readable_console(SOCKET sock)
static BOOL get_special_folder(int n, WCHAR *env)
char rb_w32_fd_is_text(int fd)
static int eq(VALUE x, VALUE y)
int st_foreach(st_table *, int(*)(ANYARGS), st_data_t)
static ULONG(STDMETHODCALLTYPE AddRef)(IDispatch __RPC_FAR *This)
int rb_w32_check_interrupt(void *)
int rb_w32_times(struct tms *tmbuf)
void rb_w32_fdclr(int fd, fd_set *set)
int select(int num_fds, fd_set *in_fds, fd_set *out_fds, fd_set *ex_fds, struct timeval *timeout)
struct hostent *WSAAPI rb_w32_gethostbyaddr(const char *addr, int len, int type)
int rb_w32_ftruncate(int fd, off_t length)
static int check_valid_dir(const WCHAR *path)
static st_table * conlist
#define _set_osflags(fh, flags)
rb_encoding * rb_enc_from_index(int index)
static void constat_reset(HANDLE h)
static int wunlink(const WCHAR *path)