Ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e0ba6b95ab71a441357ed5484e33498)
file.c
1#if defined(__MINGW32__)
2/* before stdio.h in ruby/define.h */
3# define MINGW_HAS_SECURE_API 1
4#endif
5#include "ruby/ruby.h"
6#include "ruby/encoding.h"
7#include "internal.h"
8#include "internal/error.h"
9#include <winbase.h>
10#include <wchar.h>
11#include <shlwapi.h>
12#include "win32/file.h"
13
14#ifndef INVALID_FILE_ATTRIBUTES
15# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
16#endif
17
18/* cache 'encoding name' => 'code page' into a hash */
19static struct code_page_table {
20 USHORT *table;
21 unsigned int count;
22} rb_code_page;
23
24#define IS_DIR_SEPARATOR_P(c) (c == L'\\' || c == L'/')
25#define IS_DIR_UNC_P(c) (IS_DIR_SEPARATOR_P(c[0]) && IS_DIR_SEPARATOR_P(c[1]))
26static int
27IS_ABSOLUTE_PATH_P(const WCHAR *path, size_t len)
28{
29 if (len < 2) return FALSE;
30 if (ISALPHA(path[0]))
31 return len > 2 && path[1] == L':' && IS_DIR_SEPARATOR_P(path[2]);
32 else
33 return IS_DIR_UNC_P(path);
34}
35
36/* MultiByteToWideChar() doesn't work with code page 51932 */
37#define INVALID_CODE_PAGE 51932
38#define PATH_BUFFER_SIZE MAX_PATH * 2
39
40/* defined in win32/win32.c */
41#define system_code_page rb_w32_filecp
42#define mbstr_to_wstr rb_w32_mbstr_to_wstr
43#define wstr_to_mbstr rb_w32_wstr_to_mbstr
44
45static inline void
46replace_wchar(wchar_t *s, int find, int replace)
47{
48 while (*s != 0) {
49 if (*s == find)
50 *s = replace;
51 s++;
52 }
53}
54
55/* Remove trailing invalid ':$DATA' of the path. */
56static inline size_t
57remove_invalid_alternative_data(wchar_t *wfullpath, size_t size)
58{
59 static const wchar_t prime[] = L":$DATA";
60 enum { prime_len = (sizeof(prime) / sizeof(wchar_t)) -1 };
61
62 if (size <= prime_len || _wcsnicmp(wfullpath + size - prime_len, prime, prime_len) != 0)
63 return size;
64
65 /* alias of stream */
66 /* get rid of a bug of x64 VC++ */
67 if (wfullpath[size - (prime_len + 1)] == ':') {
68 /* remove trailing '::$DATA' */
69 size -= prime_len + 1; /* prime */
70 wfullpath[size] = L'\0';
71 }
72 else {
73 /* remove trailing ':$DATA' of paths like '/aa:a:$DATA' */
74 wchar_t *pos = wfullpath + size - (prime_len + 1);
75 while (!IS_DIR_SEPARATOR_P(*pos) && pos != wfullpath) {
76 if (*pos == L':') {
77 size -= prime_len; /* alternative */
78 wfullpath[size] = L'\0';
79 break;
80 }
81 pos--;
82 }
83 }
84 return size;
85}
86
87void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
88
89static int
90code_page_i(st_data_t name, st_data_t idx, st_data_t arg)
91{
92 const char *n = (const char *)name;
93 if (strncmp("CP", n, 2) == 0) {
94 int code_page = atoi(n + 2);
95 if (code_page != 0) {
96 struct code_page_table *cp = (struct code_page_table *)arg;
97 unsigned int count = cp->count;
98 USHORT *table = cp->table;
99 if (count <= idx) {
100 unsigned int i = count;
101 count = (((idx + 4) & ~31) | 28);
102 table = realloc(table, count * sizeof(*table));
103 if (!table) return ST_CONTINUE;
104 cp->count = count;
105 cp->table = table;
106 while (i < count) table[i++] = INVALID_CODE_PAGE;
107 }
108 table[idx] = (USHORT)code_page;
109 }
110 }
111 return ST_CONTINUE;
112}
113
114/*
115 Return code page number of the encoding.
116 Cache code page into a hash for performance since finding the code page in
117 Encoding#names is slow.
118*/
119static UINT
120code_page(rb_encoding *enc)
121{
122 int enc_idx;
123
124 if (!enc)
125 return system_code_page();
126
127 enc_idx = rb_enc_to_index(enc);
128
129 /* map US-ASCII and ASCII-8bit as code page 1252 (us-ascii) */
130 if (enc_idx == rb_usascii_encindex() || enc_idx == rb_ascii8bit_encindex()) {
131 return 1252;
132 }
133 if (enc_idx == rb_utf8_encindex()) {
134 return CP_UTF8;
135 }
136
137 if (0 <= enc_idx && (unsigned int)enc_idx < rb_code_page.count)
138 return rb_code_page.table[enc_idx];
139
140 return INVALID_CODE_PAGE;
141}
142
143#define fix_string_encoding(str, encoding) rb_str_conv_enc((str), (encoding), rb_utf8_encoding())
144
145/*
146 Replace the last part of the path to long name.
147 We try to avoid to call FindFirstFileW() since it takes long time.
148*/
149static inline size_t
150replace_to_long_name(wchar_t **wfullpath, size_t size, size_t buffer_size)
151{
152 WIN32_FIND_DATAW find_data;
153 HANDLE find_handle;
154
155 /*
156 Skip long name conversion if the path is already long name.
157 Short name is 8.3 format.
158 https://en.wikipedia.org/wiki/8.3_filename
159 This check can be skipped for directory components that have file
160 extensions longer than 3 characters, or total lengths longer than
161 12 characters.
162 https://msdn.microsoft.com/en-us/library/windows/desktop/aa364980(v=vs.85).aspx
163 */
164 size_t const max_short_name_size = 8 + 1 + 3;
165 size_t const max_extension_size = 3;
166 size_t path_len = 1, extension_len = 0;
167 wchar_t *pos = *wfullpath;
168
169 if (size == 3 && pos[1] == L':' && pos[2] == L'\\' && pos[3] == L'\0') {
170 /* root path doesn't need short name expansion */
171 return size;
172 }
173
174 /* skip long name conversion if path contains wildcard characters */
175 if (wcspbrk(pos, L"*?")) {
176 return size;
177 }
178
179 pos = *wfullpath + size - 1;
180 while (!IS_DIR_SEPARATOR_P(*pos) && pos != *wfullpath) {
181 if (!extension_len && *pos == L'.') {
182 extension_len = path_len - 1;
183 }
184 if (path_len > max_short_name_size || extension_len > max_extension_size) {
185 return size;
186 }
187 path_len++;
188 pos--;
189 }
190
191 if ((pos >= *wfullpath + 2) &&
192 (*wfullpath)[0] == L'\\' && (*wfullpath)[1] == L'\\') {
193 /* UNC path: no short file name, and needs Network Share
194 * Management functions instead of FindFirstFile. */
195 if (pos == *wfullpath + 2) {
196 /* //host only */
197 return size;
198 }
199 if (!wmemchr(*wfullpath + 2, L'\\', pos - *wfullpath - 2)) {
200 /* //host/share only */
201 return size;
202 }
203 }
204
205 find_handle = FindFirstFileW(*wfullpath, &find_data);
206 if (find_handle != INVALID_HANDLE_VALUE) {
207 size_t trail_pos = pos - *wfullpath + IS_DIR_SEPARATOR_P(*pos);
208 size_t file_len = wcslen(find_data.cFileName);
209 size_t oldsize = size;
210
211 FindClose(find_handle);
212 size = trail_pos + file_len;
213 if (size > (buffer_size ? buffer_size-1 : oldsize)) {
214 wchar_t *buf = ALLOC_N(wchar_t, (size + 1));
215 wcsncpy(buf, *wfullpath, trail_pos);
216 if (!buffer_size)
217 xfree(*wfullpath);
218 *wfullpath = buf;
219 }
220 wcsncpy(*wfullpath + trail_pos, find_data.cFileName, file_len + 1);
221 }
222 return size;
223}
224
225static inline size_t
226user_length_in_path(const wchar_t *wuser, size_t len)
227{
228 size_t i;
229
230 for (i = 0; i < len && !IS_DIR_SEPARATOR_P(wuser[i]); i++)
231 ;
232
233 return i;
234}
235
236static VALUE
237append_wstr(VALUE dst, const WCHAR *ws, ssize_t len, UINT cp, rb_encoding *enc)
238{
239 long olen, nlen = (long)len;
240
241 if (cp != INVALID_CODE_PAGE) {
242 if (len == -1) len = lstrlenW(ws);
243 nlen = WideCharToMultiByte(cp, 0, ws, len, NULL, 0, NULL, NULL);
244 olen = RSTRING_LEN(dst);
245 rb_str_modify_expand(dst, nlen);
246 WideCharToMultiByte(cp, 0, ws, len, RSTRING_PTR(dst) + olen, nlen, NULL, NULL);
247 rb_enc_associate(dst, enc);
248 rb_str_set_len(dst, olen + nlen);
249 }
250 else {
251 const int replaceflags = ECONV_UNDEF_REPLACE|ECONV_INVALID_REPLACE;
252 char *utf8str = wstr_to_mbstr(CP_UTF8, ws, (int)len, &nlen);
253 rb_econv_t *ec = rb_econv_open("UTF-8", rb_enc_name(enc), replaceflags);
254 dst = rb_econv_append(ec, utf8str, nlen, dst, replaceflags);
255 rb_econv_close(ec);
256 free(utf8str);
257 }
258 return dst;
259}
260
261VALUE
262rb_default_home_dir(VALUE result)
263{
264 WCHAR *dir = rb_w32_home_dir();
265 if (!dir) {
266 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
267 }
268 append_wstr(result, dir, -1,
269 rb_w32_filecp(), rb_filesystem_encoding());
270 xfree(dir);
271 return result;
272}
273
274VALUE
275rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
276{
277 size_t size = 0, whome_len = 0;
278 size_t buffer_len = 0;
279 long wpath_len = 0, wdir_len = 0;
280 wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
281 wchar_t *wdir = NULL, *wdir_pos = NULL;
282 wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
283 UINT path_cp, cp;
284 VALUE path = fname, dir = dname;
285 wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
286 wchar_t path_drive = L'\0', dir_drive = L'\0';
287 int ignore_dir = 0;
288 rb_encoding *path_encoding;
289
290 /* get path encoding */
291 if (NIL_P(dir)) {
292 path_encoding = rb_enc_get(path);
293 }
294 else {
295 path_encoding = rb_enc_check(path, dir);
296 }
297
298 cp = path_cp = code_page(path_encoding);
299
300 /* workaround invalid codepage */
301 if (path_cp == INVALID_CODE_PAGE) {
302 cp = CP_UTF8;
303 if (!NIL_P(path)) {
304 path = fix_string_encoding(path, path_encoding);
305 }
306 }
307
308 /* convert char * to wchar_t */
309 if (!NIL_P(path)) {
310 const long path_len = RSTRING_LEN(path);
311#if SIZEOF_INT < SIZEOF_LONG
312 if ((long)(int)path_len != path_len) {
313 rb_raise(rb_eRangeError, "path (%ld bytes) is too long",
314 path_len);
315 }
316#endif
317 wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
318 wpath_pos = wpath;
319 }
320
321 /* determine if we need the user's home directory */
322 /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
323 if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
324 (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
325 whome = rb_w32_home_dir();
326 if (whome == NULL) {
327 free(wpath);
328 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
329 }
330 whome_len = wcslen(whome);
331
332 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
333 free(wpath);
334 xfree(whome);
335 rb_raise(rb_eArgError, "non-absolute home");
336 }
337
338 if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
339 /* use filesystem encoding if expanding home dir */
340 path_encoding = rb_filesystem_encoding();
341 cp = path_cp = code_page(path_encoding);
342 }
343
344 /* ignores dir since we are expanding home */
345 ignore_dir = 1;
346
347 /* exclude ~ from the result */
348 wpath_pos++;
349 wpath_len--;
350
351 /* exclude separator if present */
352 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
353 wpath_pos++;
354 wpath_len--;
355 }
356 }
357 else if (wpath_len >= 2 && wpath_pos[1] == L':') {
358 if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
359 /* ignore dir since path contains a drive letter and a root slash */
360 ignore_dir = 1;
361 }
362 else {
363 /* determine if we ignore dir or not later */
364 path_drive = wpath_pos[0];
365 wpath_pos += 2;
366 wpath_len -= 2;
367 }
368 }
369 else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
370 result = rb_str_new_cstr("can't find user ");
371 result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
372 path_cp, path_encoding);
373
374 if (wpath)
375 free(wpath);
376
377 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
378 }
379
380 /* convert dir */
381 if (!ignore_dir && !NIL_P(dir)) {
382 /* fix string encoding */
383 if (path_cp == INVALID_CODE_PAGE) {
384 dir = fix_string_encoding(dir, path_encoding);
385 }
386
387 /* convert char * to wchar_t */
388 if (!NIL_P(dir)) {
389 const long dir_len = RSTRING_LEN(dir);
390#if SIZEOF_INT < SIZEOF_LONG
391 if ((long)(int)dir_len != dir_len) {
392 if (wpath) free(wpath);
393 rb_raise(rb_eRangeError, "base directory (%ld bytes) is too long",
394 dir_len);
395 }
396#endif
397 wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
398 wdir_pos = wdir;
399 }
400
401 if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
402 (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
403 whome = rb_w32_home_dir();
404 if (whome == NULL) {
405 free(wpath);
406 free(wdir);
407 rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
408 }
409 whome_len = wcslen(whome);
410
411 if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
412 free(wpath);
413 free(wdir);
414 xfree(whome);
415 rb_raise(rb_eArgError, "non-absolute home");
416 }
417
418 /* exclude ~ from the result */
419 wdir_pos++;
420 wdir_len--;
421
422 /* exclude separator if present */
423 if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
424 wdir_pos++;
425 wdir_len--;
426 }
427 }
428 else if (wdir_len >= 2 && wdir[1] == L':') {
429 dir_drive = wdir[0];
430 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
431 wdir_len = 2;
432 }
433 }
434 else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
435 /* UNC path */
436 if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
437 /* cut the UNC path tail to '//host/share' */
438 long separators = 0;
439 long pos = 2;
440 while (pos < wdir_len && separators < 2) {
441 if (IS_DIR_SEPARATOR_P(wdir[pos])) {
442 separators++;
443 }
444 pos++;
445 }
446 if (separators == 2)
447 wdir_len = pos - 1;
448 }
449 }
450 else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
451 result = rb_str_new_cstr("can't find user ");
452 result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
453 path_cp, path_encoding);
454 if (wpath)
455 free(wpath);
456
457 if (wdir)
458 free(wdir);
459
460 rb_exc_raise(rb_exc_new_str(rb_eArgError, result));
461 }
462 }
463
464 /* determine if we ignore dir or not */
465 if (!ignore_dir && path_drive && dir_drive) {
466 if (towupper(path_drive) != towupper(dir_drive)) {
467 /* ignore dir since path drive is different from dir drive */
468 ignore_dir = 1;
469 wdir_len = 0;
470 dir_drive = 0;
471 }
472 }
473
474 if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
475 /* ignore dir since path has UNC root */
476 ignore_dir = 1;
477 wdir_len = 0;
478 }
479 else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
480 !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
481 /* ignore dir since path has root slash and dir doesn't have drive or UNC root */
482 ignore_dir = 1;
483 wdir_len = 0;
484 }
485
486 buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
487
488 buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
489
490 /* add home */
491 if (whome_len) {
492 wcsncpy(buffer_pos, whome, whome_len);
493 buffer_pos += whome_len;
494 }
495
496 /* Add separator if required */
497 if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
498 buffer_pos[0] = L'\\';
499 buffer_pos++;
500 }
501 else if (!dir_drive && path_drive) {
502 *buffer_pos++ = path_drive;
503 *buffer_pos++ = L':';
504 }
505
506 if (wdir_len) {
507 wcsncpy(buffer_pos, wdir_pos, wdir_len);
508 buffer_pos += wdir_len;
509 }
510
511 /* add separator if required */
512 if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
513 buffer_pos[0] = L'\\';
514 buffer_pos++;
515 }
516
517 /* now deal with path */
518 if (wpath_len) {
519 wcsncpy(buffer_pos, wpath_pos, wpath_len);
520 buffer_pos += wpath_len;
521 }
522
523 /* GetFullPathNameW requires at least "." to determine current directory */
524 if (wpath_len == 0) {
525 buffer_pos[0] = L'.';
526 buffer_pos++;
527 }
528
529 /* Ensure buffer is NULL terminated */
530 buffer_pos[0] = L'\0';
531
532 /* FIXME: Make this more robust */
533 /* Determine require buffer size */
534 size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
535 if (size > PATH_BUFFER_SIZE) {
536 /* allocate more memory than allotted originally by PATH_BUFFER_SIZE */
537 wfullpath = ALLOC_N(wchar_t, size);
538 size = GetFullPathNameW(buffer, size, wfullpath, NULL);
539 }
540 else {
541 wfullpath = wfullpath_buffer;
542 }
543
544 /* Remove any trailing slashes */
545 if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
546 wfullpath[size - 2] != L':' &&
547 !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
548 size -= 1;
549 wfullpath[size] = L'\0';
550 }
551
552 /* Remove any trailing dot */
553 if (wfullpath[size - 1] == L'.') {
554 size -= 1;
555 wfullpath[size] = L'\0';
556 }
557
558 /* removes trailing invalid ':$DATA' */
559 size = remove_invalid_alternative_data(wfullpath, size);
560
561 /* Replace the trailing path to long name */
562 if (long_name) {
563 size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
564 size = replace_to_long_name(&wfullpath, size, bufsize);
565 }
566
567 /* sanitize backslashes with forwardslashes */
568 replace_wchar(wfullpath, L'\\', L'/');
569
570 /* convert to VALUE and set the path encoding */
571 rb_str_set_len(result, 0);
572 result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
573
574 /* TODO: better cleanup */
575 if (buffer)
576 xfree(buffer);
577
578 if (wpath)
579 free(wpath);
580
581 if (wdir)
582 free(wdir);
583
584 if (whome)
585 xfree(whome);
586
587 if (wfullpath != wfullpath_buffer)
588 xfree(wfullpath);
589
590 rb_enc_associate(result, path_encoding);
591 return result;
592}
593
594VALUE
595rb_readlink(VALUE path, rb_encoding *resultenc)
596{
597 DWORD len;
598 VALUE wtmp = 0, wpathbuf, str;
599 rb_w32_reparse_buffer_t rbuf, *rp = &rbuf;
600 WCHAR *wpath, *wbuf;
601 rb_encoding *enc;
602 UINT cp, path_cp;
603 int e;
604
605 FilePathValue(path);
606 enc = rb_enc_get(path);
607 cp = path_cp = code_page(enc);
608 if (cp == INVALID_CODE_PAGE) {
609 path = fix_string_encoding(path, enc);
610 cp = CP_UTF8;
611 }
612 len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), NULL, 0);
613 wpath = ALLOCV_N(WCHAR, wpathbuf, len+1);
614 MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), wpath, len);
615 wpath[len] = L'\0';
616 e = rb_w32_read_reparse_point(wpath, rp, sizeof(rbuf), &wbuf, &len);
617 if (e == ERROR_MORE_DATA) {
618 size_t size = rb_w32_reparse_buffer_size(len + 1);
619 rp = ALLOCV(wtmp, size);
620 e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
621 }
622 ALLOCV_END(wpathbuf);
623 if (e) {
624 ALLOCV_END(wtmp);
625 if (e != -1)
626 rb_syserr_fail_path(rb_w32_map_errno(e), path);
627 else /* not symlink; maybe volume mount point */
628 rb_syserr_fail_path(EINVAL, path);
629 }
630 enc = resultenc;
631 path_cp = code_page(enc);
632 len = lstrlenW(wbuf);
633 str = append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, path_cp, enc);
634 ALLOCV_END(wtmp);
635 return str;
636}
637
638int
639rb_file_load_ok(const char *path)
640{
641 DWORD attr;
642 int ret = 1;
643 long len;
644 wchar_t* wpath;
645
646 wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
647 if (!wpath) return 0;
648
649 attr = GetFileAttributesW(wpath);
650 if (attr == INVALID_FILE_ATTRIBUTES ||
651 (attr & FILE_ATTRIBUTE_DIRECTORY)) {
652 ret = 0;
653 }
654 else {
655 HANDLE h = CreateFileW(wpath, GENERIC_READ,
656 FILE_SHARE_READ | FILE_SHARE_WRITE,
657 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
658 if (h != INVALID_HANDLE_VALUE) {
659 CloseHandle(h);
660 }
661 else {
662 ret = 0;
663 }
664 }
665 free(wpath);
666 return ret;
667}
668
669int
670rb_freopen(VALUE fname, const char *mode, FILE *file)
671{
672 WCHAR *wname, wmode[4];
673 VALUE wtmp;
674 char *name;
675 long len;
676 int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
677 if (n > numberof(wmode)) return EINVAL;
678 MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
679 RSTRING_GETMEM(fname, name, len);
680 n = rb_long2int(len);
681 len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
682 wname = ALLOCV_N(WCHAR, wtmp, len + 1);
683 len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
684 wname[len] = L'\0';
685 RB_GC_GUARD(fname);
686#if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
687 e = _wfreopen(wname, wmode, file) ? 0 : errno;
688#else
689 {
690 FILE *newfp = 0;
691 e = _wfreopen_s(&newfp, wname, wmode, file);
692 }
693#endif
694 ALLOCV_END(wtmp);
695 return e;
696}
697
698void
699Init_w32_codepage(void)
700{
701 if (rb_code_page.count) return;
702 rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
703}
#define ALLOCV
Old name of RB_ALLOCV.
Definition: memory.h:398
#define xfree
Old name of ruby_xfree.
Definition: xmalloc.h:58
#define ECONV_UNDEF_REPLACE
Old name of RUBY_ECONV_UNDEF_REPLACE.
Definition: transcode.h:523
#define ECONV_INVALID_REPLACE
Old name of RUBY_ECONV_INVALID_REPLACE.
Definition: transcode.h:521
#define ALLOC_N
Old name of RB_ALLOC_N.
Definition: memory.h:393
#define ISALPHA
Old name of rb_isalpha.
Definition: ctype.h:92
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition: memory.h:399
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition: memory.h:400
void rb_raise(VALUE exc, const char *fmt,...)
Exception entry point.
Definition: error.c:3021
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:671
Encoding relates APIs.
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Identical to rb_enc_associate(), except it takes an encoding itself instead of its index.
Definition: encoding.c:1066
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
Definition: encoding.c:1592
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition: encoding.h:433
int rb_utf8_encindex(void)
Identical to rb_utf8_encoding(), except it returns the encoding's index instead of the encoding itsel...
Definition: encoding.c:1533
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
Definition: encoding.c:1072
int rb_ascii8bit_encindex(void)
Identical to rb_ascii8bit_encoding(), except it returns the encoding's index instead of the encoding ...
Definition: encoding.c:1521
int rb_enc_to_index(rb_encoding *enc)
Queries the index of the encoding.
Definition: encoding.c:197
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
Identical to rb_enc_compatible(), except it raises an exception instead of returning NULL.
Definition: encoding.c:1097
int rb_usascii_encindex(void)
Identical to rb_usascii_encoding(), except it returns the encoding's index instead of the encoding it...
Definition: encoding.c:1545
VALUE rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
Identical to rb_enc_str_new(), except it additionally takes an encoding.
Definition: string.c:940
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
Definition: string.c:790
rb_econv_t * rb_econv_open(const char *source_encoding, const char *destination_encoding, int ecflags)
Creates a new instance of struct rb_econv_t.
Definition: transcode.c:1072
void rb_econv_close(rb_econv_t *ec)
Destructs a converter.
Definition: transcode.c:1705
VALUE rb_econv_append(rb_econv_t *ec, const char *bytesrc, long bytesize, VALUE dst, int flags)
Converts the passed C's pointer according to the passed converter, then append the conversion result ...
Definition: transcode.c:1822
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition: string.c:3039
VALUE rb_str_new_cstr(const char *ptr)
Identical to rb_str_new(), except it assumes the passed pointer is a pointer to a C string.
Definition: string.c:952
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
Definition: string.c:2467
#define rb_long2int
Just another name of rb_long2int_inline.
Definition: long.h:62
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:161
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition: rstring.h:573
static long RSTRING_LEN(VALUE str)
Queries the length of the string.
Definition: rstring.h:483
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:497
#define FilePathValue(v)
Ensures that the parameter object is a path.
Definition: ruby.h:90