Ruby  3.1.4p223 (2023-03-30 revision HEAD)
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 */
19 static 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]))
26 static int
27 IS_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 
45 static inline void
46 replace_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. */
56 static inline size_t
57 remove_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 
87 void rb_enc_foreach_name(int (*func)(st_data_t name, st_data_t idx, st_data_t arg), st_data_t arg);
88 
89 static int
90 code_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 */
119 static UINT
120 code_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 */
149 static inline size_t
150 replace_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 
225 static inline size_t
226 user_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 
236 static VALUE
237 append_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 
261 VALUE
262 rb_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  CP_UTF8, rb_utf8_encoding());
270 
271  xfree(dir);
272  return result;
273 }
274 
275 VALUE
276 rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
277 {
278  size_t size = 0, whome_len = 0;
279  size_t buffer_len = 0;
280  long wpath_len = 0, wdir_len = 0;
281  wchar_t *wfullpath = NULL, *wpath = NULL, *wpath_pos = NULL;
282  wchar_t *wdir = NULL, *wdir_pos = NULL;
283  wchar_t *whome = NULL, *buffer = NULL, *buffer_pos = NULL;
284  UINT path_cp, cp;
285  VALUE path = fname, dir = dname;
286  wchar_t wfullpath_buffer[PATH_BUFFER_SIZE];
287  wchar_t path_drive = L'\0', dir_drive = L'\0';
288  int ignore_dir = 0;
289  rb_encoding *path_encoding;
290 
291  /* get path encoding */
292  if (NIL_P(dir)) {
293  path_encoding = rb_enc_get(path);
294  }
295  else {
296  path_encoding = rb_enc_check(path, dir);
297  }
298 
299  cp = path_cp = code_page(path_encoding);
300 
301  /* workaround invalid codepage */
302  if (path_cp == INVALID_CODE_PAGE) {
303  cp = CP_UTF8;
304  if (!NIL_P(path)) {
305  path = fix_string_encoding(path, path_encoding);
306  }
307  }
308 
309  /* convert char * to wchar_t */
310  if (!NIL_P(path)) {
311  const long path_len = RSTRING_LEN(path);
312 #if SIZEOF_INT < SIZEOF_LONG
313  if ((long)(int)path_len != path_len) {
314  rb_raise(rb_eRangeError, "path (%ld bytes) is too long",
315  path_len);
316  }
317 #endif
318  wpath = mbstr_to_wstr(cp, RSTRING_PTR(path), path_len, &wpath_len);
319  wpath_pos = wpath;
320  }
321 
322  /* determine if we need the user's home directory */
323  /* expand '~' only if NOT rb_file_absolute_path() where `abs_mode` is 1 */
324  if (abs_mode == 0 && wpath_len > 0 && wpath_pos[0] == L'~' &&
325  (wpath_len == 1 || IS_DIR_SEPARATOR_P(wpath_pos[1]))) {
326  whome = rb_w32_home_dir();
327  if (whome == NULL) {
328  free(wpath);
329  rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
330  }
331  whome_len = wcslen(whome);
332 
333  if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
334  free(wpath);
335  xfree(whome);
336  rb_raise(rb_eArgError, "non-absolute home");
337  }
338 
339  if (path_cp == INVALID_CODE_PAGE || rb_enc_str_asciionly_p(path)) {
340  /* use filesystem encoding if expanding home dir */
341  path_encoding = rb_filesystem_encoding();
342  cp = path_cp = code_page(path_encoding);
343  }
344 
345  /* ignores dir since we are expanding home */
346  ignore_dir = 1;
347 
348  /* exclude ~ from the result */
349  wpath_pos++;
350  wpath_len--;
351 
352  /* exclude separator if present */
353  if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
354  wpath_pos++;
355  wpath_len--;
356  }
357  }
358  else if (wpath_len >= 2 && wpath_pos[1] == L':') {
359  if (wpath_len >= 3 && IS_DIR_SEPARATOR_P(wpath_pos[2])) {
360  /* ignore dir since path contains a drive letter and a root slash */
361  ignore_dir = 1;
362  }
363  else {
364  /* determine if we ignore dir or not later */
365  path_drive = wpath_pos[0];
366  wpath_pos += 2;
367  wpath_len -= 2;
368  }
369  }
370  else if (abs_mode == 0 && wpath_len >= 2 && wpath_pos[0] == L'~') {
371  result = rb_str_new_cstr("can't find user ");
372  result = append_wstr(result, wpath_pos + 1, user_length_in_path(wpath_pos + 1, wpath_len - 1),
373  path_cp, path_encoding);
374 
375  if (wpath)
376  free(wpath);
377 
379  }
380 
381  /* convert dir */
382  if (!ignore_dir && !NIL_P(dir)) {
383  /* fix string encoding */
384  if (path_cp == INVALID_CODE_PAGE) {
385  dir = fix_string_encoding(dir, path_encoding);
386  }
387 
388  /* convert char * to wchar_t */
389  if (!NIL_P(dir)) {
390  const long dir_len = RSTRING_LEN(dir);
391 #if SIZEOF_INT < SIZEOF_LONG
392  if ((long)(int)dir_len != dir_len) {
393  if (wpath) free(wpath);
394  rb_raise(rb_eRangeError, "base directory (%ld bytes) is too long",
395  dir_len);
396  }
397 #endif
398  wdir = mbstr_to_wstr(cp, RSTRING_PTR(dir), dir_len, &wdir_len);
399  wdir_pos = wdir;
400  }
401 
402  if (abs_mode == 0 && wdir_len > 0 && wdir_pos[0] == L'~' &&
403  (wdir_len == 1 || IS_DIR_SEPARATOR_P(wdir_pos[1]))) {
404  whome = rb_w32_home_dir();
405  if (whome == NULL) {
406  free(wpath);
407  free(wdir);
408  rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
409  }
410  whome_len = wcslen(whome);
411 
412  if (!IS_ABSOLUTE_PATH_P(whome, whome_len)) {
413  free(wpath);
414  free(wdir);
415  xfree(whome);
416  rb_raise(rb_eArgError, "non-absolute home");
417  }
418 
419  /* exclude ~ from the result */
420  wdir_pos++;
421  wdir_len--;
422 
423  /* exclude separator if present */
424  if (wdir_len && IS_DIR_SEPARATOR_P(wdir_pos[0])) {
425  wdir_pos++;
426  wdir_len--;
427  }
428  }
429  else if (wdir_len >= 2 && wdir[1] == L':') {
430  dir_drive = wdir[0];
431  if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
432  wdir_len = 2;
433  }
434  }
435  else if (wdir_len >= 2 && IS_DIR_UNC_P(wdir)) {
436  /* UNC path */
437  if (wpath_len && IS_DIR_SEPARATOR_P(wpath_pos[0])) {
438  /* cut the UNC path tail to '//host/share' */
439  long separators = 0;
440  long pos = 2;
441  while (pos < wdir_len && separators < 2) {
442  if (IS_DIR_SEPARATOR_P(wdir[pos])) {
443  separators++;
444  }
445  pos++;
446  }
447  if (separators == 2)
448  wdir_len = pos - 1;
449  }
450  }
451  else if (abs_mode == 0 && wdir_len >= 2 && wdir_pos[0] == L'~') {
452  result = rb_str_new_cstr("can't find user ");
453  result = append_wstr(result, wdir_pos + 1, user_length_in_path(wdir_pos + 1, wdir_len - 1),
454  path_cp, path_encoding);
455  if (wpath)
456  free(wpath);
457 
458  if (wdir)
459  free(wdir);
460 
462  }
463  }
464 
465  /* determine if we ignore dir or not */
466  if (!ignore_dir && path_drive && dir_drive) {
467  if (towupper(path_drive) != towupper(dir_drive)) {
468  /* ignore dir since path drive is different from dir drive */
469  ignore_dir = 1;
470  wdir_len = 0;
471  dir_drive = 0;
472  }
473  }
474 
475  if (!ignore_dir && wpath_len >= 2 && IS_DIR_UNC_P(wpath)) {
476  /* ignore dir since path has UNC root */
477  ignore_dir = 1;
478  wdir_len = 0;
479  }
480  else if (!ignore_dir && wpath_len >= 1 && IS_DIR_SEPARATOR_P(wpath[0]) &&
481  !dir_drive && !(wdir_len >= 2 && IS_DIR_UNC_P(wdir))) {
482  /* ignore dir since path has root slash and dir doesn't have drive or UNC root */
483  ignore_dir = 1;
484  wdir_len = 0;
485  }
486 
487  buffer_len = wpath_len + 1 + wdir_len + 1 + whome_len + 1;
488 
489  buffer = buffer_pos = ALLOC_N(wchar_t, (buffer_len + 1));
490 
491  /* add home */
492  if (whome_len) {
493  wcsncpy(buffer_pos, whome, whome_len);
494  buffer_pos += whome_len;
495  }
496 
497  /* Add separator if required */
498  if (whome_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
499  buffer_pos[0] = L'\\';
500  buffer_pos++;
501  }
502  else if (!dir_drive && path_drive) {
503  *buffer_pos++ = path_drive;
504  *buffer_pos++ = L':';
505  }
506 
507  if (wdir_len) {
508  wcsncpy(buffer_pos, wdir_pos, wdir_len);
509  buffer_pos += wdir_len;
510  }
511 
512  /* add separator if required */
513  if (wdir_len && wcsrchr(L"\\/:", buffer_pos[-1]) == NULL) {
514  buffer_pos[0] = L'\\';
515  buffer_pos++;
516  }
517 
518  /* now deal with path */
519  if (wpath_len) {
520  wcsncpy(buffer_pos, wpath_pos, wpath_len);
521  buffer_pos += wpath_len;
522  }
523 
524  /* GetFullPathNameW requires at least "." to determine current directory */
525  if (wpath_len == 0) {
526  buffer_pos[0] = L'.';
527  buffer_pos++;
528  }
529 
530  /* Ensure buffer is NULL terminated */
531  buffer_pos[0] = L'\0';
532 
533  /* FIXME: Make this more robust */
534  /* Determine require buffer size */
535  size = GetFullPathNameW(buffer, PATH_BUFFER_SIZE, wfullpath_buffer, NULL);
536  if (size > PATH_BUFFER_SIZE) {
537  /* allocate more memory than allotted originally by PATH_BUFFER_SIZE */
538  wfullpath = ALLOC_N(wchar_t, size);
539  size = GetFullPathNameW(buffer, size, wfullpath, NULL);
540  }
541  else {
542  wfullpath = wfullpath_buffer;
543  }
544 
545  /* Remove any trailing slashes */
546  if (IS_DIR_SEPARATOR_P(wfullpath[size - 1]) &&
547  wfullpath[size - 2] != L':' &&
548  !(size == 2 && IS_DIR_UNC_P(wfullpath))) {
549  size -= 1;
550  wfullpath[size] = L'\0';
551  }
552 
553  /* Remove any trailing dot */
554  if (wfullpath[size - 1] == L'.') {
555  size -= 1;
556  wfullpath[size] = L'\0';
557  }
558 
559  /* removes trailing invalid ':$DATA' */
560  size = remove_invalid_alternative_data(wfullpath, size);
561 
562  /* Replace the trailing path to long name */
563  if (long_name) {
564  size_t bufsize = wfullpath == wfullpath_buffer ? PATH_BUFFER_SIZE : 0;
565  size = replace_to_long_name(&wfullpath, size, bufsize);
566  }
567 
568  /* sanitize backslashes with forwardslashes */
569  replace_wchar(wfullpath, L'\\', L'/');
570 
571  /* convert to VALUE and set the path encoding */
572  rb_str_set_len(result, 0);
573  result = append_wstr(result, wfullpath, size, path_cp, path_encoding);
574 
575  /* TODO: better cleanup */
576  if (buffer)
577  xfree(buffer);
578 
579  if (wpath)
580  free(wpath);
581 
582  if (wdir)
583  free(wdir);
584 
585  if (whome)
586  xfree(whome);
587 
588  if (wfullpath != wfullpath_buffer)
589  xfree(wfullpath);
590 
591  rb_enc_associate(result, path_encoding);
592  return result;
593 }
594 
595 VALUE
596 rb_readlink(VALUE path, rb_encoding *resultenc)
597 {
598  DWORD len;
599  VALUE wtmp = 0, wpathbuf, str;
600  rb_w32_reparse_buffer_t rbuf, *rp = &rbuf;
601  WCHAR *wpath, *wbuf;
602  rb_encoding *enc;
603  UINT cp, path_cp;
604  int e;
605 
606  FilePathValue(path);
607  enc = rb_enc_get(path);
608  cp = path_cp = code_page(enc);
609  if (cp == INVALID_CODE_PAGE) {
610  path = fix_string_encoding(path, enc);
611  cp = CP_UTF8;
612  }
613  len = MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), NULL, 0);
614  wpath = ALLOCV_N(WCHAR, wpathbuf, len+1);
615  MultiByteToWideChar(cp, 0, RSTRING_PTR(path), RSTRING_LEN(path), wpath, len);
616  wpath[len] = L'\0';
617  e = rb_w32_read_reparse_point(wpath, rp, sizeof(rbuf), &wbuf, &len);
618  if (e == ERROR_MORE_DATA) {
619  size_t size = rb_w32_reparse_buffer_size(len + 1);
620  rp = ALLOCV(wtmp, size);
621  e = rb_w32_read_reparse_point(wpath, rp, size, &wbuf, &len);
622  }
623  ALLOCV_END(wpathbuf);
624  if (e) {
625  ALLOCV_END(wtmp);
626  if (e != -1)
627  rb_syserr_fail_path(rb_w32_map_errno(e), path);
628  else /* not symlink; maybe volume mount point */
629  rb_syserr_fail_path(EINVAL, path);
630  }
631  enc = resultenc;
632  path_cp = code_page(enc);
633  len = lstrlenW(wbuf);
634  str = append_wstr(rb_enc_str_new(0, 0, enc), wbuf, len, path_cp, enc);
635  ALLOCV_END(wtmp);
636  return str;
637 }
638 
639 int
640 rb_file_load_ok(const char *path)
641 {
642  DWORD attr;
643  int ret = 1;
644  long len;
645  wchar_t* wpath;
646 
647  wpath = mbstr_to_wstr(CP_UTF8, path, -1, &len);
648  if (!wpath) return 0;
649 
650  attr = GetFileAttributesW(wpath);
651  if (attr == INVALID_FILE_ATTRIBUTES ||
652  (attr & FILE_ATTRIBUTE_DIRECTORY)) {
653  ret = 0;
654  }
655  else {
656  HANDLE h = CreateFileW(wpath, GENERIC_READ,
657  FILE_SHARE_READ | FILE_SHARE_WRITE,
658  NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
659  if (h != INVALID_HANDLE_VALUE) {
660  CloseHandle(h);
661  }
662  else {
663  ret = 0;
664  }
665  }
666  free(wpath);
667  return ret;
668 }
669 
670 int
671 rb_freopen(VALUE fname, const char *mode, FILE *file)
672 {
673  WCHAR *wname, wmode[4];
674  VALUE wtmp;
675  char *name;
676  long len;
677  int e = 0, n = MultiByteToWideChar(CP_ACP, 0, mode, -1, NULL, 0);
678  if (n > numberof(wmode)) return EINVAL;
679  MultiByteToWideChar(CP_ACP, 0, mode, -1, wmode, numberof(wmode));
680  RSTRING_GETMEM(fname, name, len);
681  n = rb_long2int(len);
682  len = MultiByteToWideChar(CP_UTF8, 0, name, n, NULL, 0);
683  wname = ALLOCV_N(WCHAR, wtmp, len + 1);
684  len = MultiByteToWideChar(CP_UTF8, 0, name, n, wname, len);
685  wname[len] = L'\0';
686  RB_GC_GUARD(fname);
687 #if RUBY_MSVCRT_VERSION < 80 && !defined(HAVE__WFREOPEN_S)
688  e = _wfreopen(wname, wmode, file) ? 0 : errno;
689 #else
690  {
691  FILE *newfp = 0;
692  e = _wfreopen_s(&newfp, wname, wmode, file);
693  }
694 #endif
695  ALLOCV_END(wtmp);
696  return e;
697 }
698 
699 void
700 Init_w32_codepage(void)
701 {
702  if (rb_code_page.count) return;
703  rb_enc_foreach_name(code_page_i, (st_data_t)&rb_code_page);
704 }
#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:3025
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:675
VALUE rb_eRangeError
RangeError exception.
Definition: error.c:1103
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:1150
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1100
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
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
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
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
Definition: encoding.c:1527
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
Definition: encoding.c:1072
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition: encoding.h:433
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
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
Definition: encoding.c:1592
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
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
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_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
static char * RSTRING_PTR(VALUE str)
Queries the contents pointer of the string.
Definition: rstring.h:497
#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
#define FilePathValue(v)
Ensures that the parameter object is a path.
Definition: ruby.h:90
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40