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