Ruby  3.1.4p223 (2023-03-30 revision HEAD)
file.c
1 /**********************************************************************
2 
3  file.c -
4 
5  $Author$
6  created at: Mon Nov 15 12:24:34 JST 1993
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9  Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10  Copyright (C) 2000 Information-technology Promotion Agency, Japan
11 
12 **********************************************************************/
13 
14 #include "ruby/internal/config.h"
15 
16 #ifdef _WIN32
17 # include "missing/file.h"
18 # include "ruby.h"
19 #endif
20 
21 #include <ctype.h>
22 #include <time.h>
23 
24 #ifdef __CYGWIN__
25 # include <windows.h>
26 # include <sys/cygwin.h>
27 # include <wchar.h>
28 #endif
29 
30 #ifdef __APPLE__
31 # if !(defined(__has_feature) && defined(__has_attribute))
32 /* Maybe a bug in SDK of Xcode 10.2.1 */
33 /* In this condition, <os/availability.h> does not define
34  * API_AVAILABLE and similar, but __API_AVAILABLE and similar which
35  * are defined in <Availability.h> */
36 # define API_AVAILABLE(...)
37 # define API_DEPRECATED(...)
38 # endif
39 # include <CoreFoundation/CFString.h>
40 #endif
41 
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 #endif
49 
50 #ifdef HAVE_SYS_FILE_H
51 # include <sys/file.h>
52 #else
53 int flock(int, int);
54 #endif
55 
56 #ifdef HAVE_SYS_PARAM_H
57 # include <sys/param.h>
58 #endif
59 #ifndef MAXPATHLEN
60 # define MAXPATHLEN 1024
61 #endif
62 
63 #ifdef HAVE_UTIME_H
64 # include <utime.h>
65 #elif defined HAVE_SYS_UTIME_H
66 # include <sys/utime.h>
67 #endif
68 
69 #ifdef HAVE_PWD_H
70 # include <pwd.h>
71 #endif
72 
73 #ifdef HAVE_SYS_SYSMACROS_H
74 # include <sys/sysmacros.h>
75 #endif
76 
77 #include <sys/types.h>
78 #include <sys/stat.h>
79 
80 #ifdef HAVE_SYS_MKDEV_H
81 # include <sys/mkdev.h>
82 #endif
83 
84 #if defined(HAVE_FCNTL_H)
85 # include <fcntl.h>
86 #endif
87 
88 #if defined(HAVE_SYS_TIME_H)
89 # include <sys/time.h>
90 #endif
91 
92 #if !defined HAVE_LSTAT && !defined lstat
93 # define lstat stat
94 #endif
95 
96 /* define system APIs */
97 #ifdef _WIN32
98 # include "win32/file.h"
99 # define STAT(p, s) rb_w32_ustati128((p), (s))
100 # undef lstat
101 # define lstat(p, s) rb_w32_ulstati128((p), (s))
102 # undef access
103 # define access(p, m) rb_w32_uaccess((p), (m))
104 # undef truncate
105 # define truncate(p, n) rb_w32_utruncate((p), (n))
106 # undef chmod
107 # define chmod(p, m) rb_w32_uchmod((p), (m))
108 # undef chown
109 # define chown(p, o, g) rb_w32_uchown((p), (o), (g))
110 # undef lchown
111 # define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
112 # undef utimensat
113 # define utimensat(s, p, t, f) rb_w32_uutimensat((s), (p), (t), (f))
114 # undef link
115 # define link(f, t) rb_w32_ulink((f), (t))
116 # undef unlink
117 # define unlink(p) rb_w32_uunlink(p)
118 # undef rename
119 # define rename(f, t) rb_w32_urename((f), (t))
120 # undef symlink
121 # define symlink(s, l) rb_w32_usymlink((s), (l))
122 
123 # ifdef HAVE_REALPATH
124 /* Don't use native realpath(3) on Windows, as the check for
125  absolute paths does not work for drive letters. */
126 # undef HAVE_REALPATH
127 # endif
128 #else
129 # define STAT(p, s) stat((p), (s))
130 #endif
131 
132 #if defined _WIN32 || defined __APPLE__
133 # define USE_OSPATH 1
134 # define TO_OSPATH(str) rb_str_encode_ospath(str)
135 #else
136 # define USE_OSPATH 0
137 # define TO_OSPATH(str) (str)
138 #endif
139 
140 /* utime may fail if time is out-of-range for the FS [ruby-dev:38277] */
141 #if defined DOSISH || defined __CYGWIN__
142 # define UTIME_EINVAL
143 #endif
144 
145 /* Solaris 10 realpath(3) doesn't support File.realpath */
146 #if defined HAVE_REALPATH && defined __sun && defined __SVR4
147 #undef HAVE_REALPATH
148 #endif
149 
150 #ifdef HAVE_REALPATH
151 # include <limits.h>
152 # include <stdlib.h>
153 #endif
154 
155 #include "dln.h"
156 #include "encindex.h"
157 #include "id.h"
158 #include "internal.h"
159 #include "internal/compilers.h"
160 #include "internal/dir.h"
161 #include "internal/error.h"
162 #include "internal/file.h"
163 #include "internal/io.h"
164 #include "internal/load.h"
165 #include "internal/object.h"
166 #include "internal/process.h"
167 #include "internal/thread.h"
168 #include "internal/vm.h"
169 #include "ruby/encoding.h"
170 #include "ruby/io.h"
171 #include "ruby/thread.h"
172 #include "ruby/util.h"
173 
177 
178 static VALUE
179 file_path_convert(VALUE name)
180 {
181 #ifndef _WIN32 /* non Windows == Unix */
182  int fname_encidx = ENCODING_GET(name);
183  int fs_encidx;
184  if (ENCINDEX_US_ASCII != fname_encidx &&
185  ENCINDEX_ASCII != fname_encidx &&
186  (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
188  !rb_enc_str_asciionly_p(name)) {
189  /* Don't call rb_filesystem_encoding() before US-ASCII and ASCII-8BIT */
190  /* fs_encoding should be ascii compatible */
191  rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
192  rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
193  name = rb_str_conv_enc(name, fname_encoding, fs_encoding);
194  }
195 #endif
196  return name;
197 }
198 
199 static rb_encoding *
200 check_path_encoding(VALUE str)
201 {
202  rb_encoding *enc = rb_enc_get(str);
203  if (!rb_enc_asciicompat(enc)) {
204  rb_raise(rb_eEncCompatError, "path name must be ASCII-compatible (%s): %"PRIsVALUE,
205  rb_enc_name(enc), rb_str_inspect(str));
206  }
207  return enc;
208 }
209 
210 VALUE
211 rb_get_path_check_to_string(VALUE obj)
212 {
213  VALUE tmp;
214  ID to_path;
215 
216  if (RB_TYPE_P(obj, T_STRING)) {
217  return obj;
218  }
219  CONST_ID(to_path, "to_path");
220  tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
221  StringValue(tmp);
222  return tmp;
223 }
224 
225 VALUE
226 rb_get_path_check_convert(VALUE obj)
227 {
228  obj = file_path_convert(obj);
229 
230  check_path_encoding(obj);
231  if (!rb_str_to_cstr(obj)) {
232  rb_raise(rb_eArgError, "path name contains null byte");
233  }
234 
235  return rb_str_new4(obj);
236 }
237 
238 VALUE
240 {
241  return rb_get_path(obj);
242 }
243 
244 VALUE
246 {
247  return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
248 }
249 
250 VALUE
252 {
253 #if USE_OSPATH
254  int encidx = ENCODING_GET(path);
255 #if 0 && defined _WIN32
256  if (encidx == ENCINDEX_ASCII) {
257  encidx = rb_filesystem_encindex();
258  }
259 #endif
260  if (encidx != ENCINDEX_ASCII && encidx != ENCINDEX_UTF_8) {
261  rb_encoding *enc = rb_enc_from_index(encidx);
262  rb_encoding *utf8 = rb_utf8_encoding();
263  path = rb_str_conv_enc(path, enc, utf8);
264  }
265 #endif
266  return path;
267 }
268 
269 #ifdef __APPLE__
270 # define NORMALIZE_UTF8PATH 1
271 
272 # ifdef HAVE_WORKING_FORK
273 static void
274 rb_CFString_class_initialize_before_fork(void)
275 {
276  /*
277  * Since macOS 13, CFString family API used in
278  * rb_str_append_normalized_ospath may internally use Objective-C classes
279  * (NSTaggedPointerString and NSPlaceholderMutableString) for small strings.
280  *
281  * On the other hand, Objective-C classes should not be used for the first
282  * time in a fork()'ed but not exec()'ed process. Violations for this rule
283  * can result deadlock during class initialization, so Objective-C runtime
284  * conservatively crashes on such cases by default.
285  *
286  * Therefore, we need to use CFString API to initialize Objective-C classes
287  * used internally *before* fork().
288  *
289  * For future changes, please note that this initialization process cannot
290  * be done in ctor because NSTaggedPointerString in CoreFoundation is enabled
291  * after CFStringInitializeTaggedStrings(), which is called during loading
292  * Objective-C runtime after ctor.
293  * For more details, see https://bugs.ruby-lang.org/issues/18912
294  */
295 
296  /* Enough small but non-empty ASCII string to fit in NSTaggedPointerString. */
297  const char small_str[] = "/";
298  long len = sizeof(small_str) - 1;
299 
300  const CFAllocatorRef alloc = kCFAllocatorDefault;
301  CFStringRef s = CFStringCreateWithBytesNoCopy(alloc,
302  (const UInt8 *)small_str,
303  len, kCFStringEncodingUTF8,
304  FALSE, kCFAllocatorNull);
305  CFMutableStringRef m = CFStringCreateMutableCopy(alloc, len, s);
306  CFRelease(m);
307  CFRelease(s);
308 }
309 # endif
310 
311 static VALUE
312 rb_str_append_normalized_ospath(VALUE str, const char *ptr, long len)
313 {
314  CFIndex buflen = 0;
315  CFRange all;
316  CFStringRef s = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault,
317  (const UInt8 *)ptr, len,
318  kCFStringEncodingUTF8, FALSE,
319  kCFAllocatorNull);
320  CFMutableStringRef m = CFStringCreateMutableCopy(kCFAllocatorDefault, len, s);
321  long oldlen = RSTRING_LEN(str);
322 
323  CFStringNormalize(m, kCFStringNormalizationFormC);
324  all = CFRangeMake(0, CFStringGetLength(m));
325  CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE, NULL, 0, &buflen);
326  rb_str_modify_expand(str, buflen);
327  CFStringGetBytes(m, all, kCFStringEncodingUTF8, '?', FALSE,
328  (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
329  rb_str_set_len(str, oldlen + buflen);
330  CFRelease(m);
331  CFRelease(s);
332  return str;
333 }
334 
335 VALUE
336 rb_str_normalize_ospath(const char *ptr, long len)
337 {
338  const char *p = ptr;
339  const char *e = ptr + len;
340  const char *p1 = p;
341  VALUE str = rb_str_buf_new(len);
342  rb_encoding *enc = rb_utf8_encoding();
343  rb_enc_associate(str, enc);
344 
345  while (p < e) {
346  int l, c;
347  int r = rb_enc_precise_mbclen(p, e, enc);
348  if (!MBCLEN_CHARFOUND_P(r)) {
349  /* invalid byte shall not happen but */
350  static const char invalid[3] = "\xEF\xBF\xBD";
351  rb_str_append_normalized_ospath(str, p1, p-p1);
352  rb_str_cat(str, invalid, sizeof(invalid));
353  p += 1;
354  p1 = p;
355  continue;
356  }
357  l = MBCLEN_CHARFOUND_LEN(r);
358  c = rb_enc_mbc_to_codepoint(p, e, enc);
359  if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
360  (0x2F800 <= c && c <= 0x2FAFF)) {
361  if (p - p1 > 0) {
362  rb_str_append_normalized_ospath(str, p1, p-p1);
363  }
364  rb_str_cat(str, p, l);
365  p += l;
366  p1 = p;
367  }
368  else {
369  p += l;
370  }
371  }
372  if (p - p1 > 0) {
373  rb_str_append_normalized_ospath(str, p1, p-p1);
374  }
375 
376  return str;
377 }
378 
379 static int
380 ignored_char_p(const char *p, const char *e, rb_encoding *enc)
381 {
382  unsigned char c;
383  if (p+3 > e) return 0;
384  switch ((unsigned char)*p) {
385  case 0xe2:
386  switch ((unsigned char)p[1]) {
387  case 0x80:
388  c = (unsigned char)p[2];
389  /* c >= 0x200c && c <= 0x200f */
390  if (c >= 0x8c && c <= 0x8f) return 3;
391  /* c >= 0x202a && c <= 0x202e */
392  if (c >= 0xaa && c <= 0xae) return 3;
393  return 0;
394  case 0x81:
395  c = (unsigned char)p[2];
396  /* c >= 0x206a && c <= 0x206f */
397  if (c >= 0xaa && c <= 0xaf) return 3;
398  return 0;
399  }
400  break;
401  case 0xef:
402  /* c == 0xfeff */
403  if ((unsigned char)p[1] == 0xbb &&
404  (unsigned char)p[2] == 0xbf)
405  return 3;
406  break;
407  }
408  return 0;
409 }
410 #else
411 # define NORMALIZE_UTF8PATH 0
412 #endif
413 
414 #define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
415 
417  const char *ptr;
418  VALUE path;
419 };
420 
421 struct apply_arg {
422  int i;
423  int argc;
424  int errnum;
425  int (*func)(const char *, void *);
426  void *arg;
427  struct apply_filename fn[FLEX_ARY_LEN];
428 };
429 
430 static void *
431 no_gvl_apply2files(void *ptr)
432 {
433  struct apply_arg *aa = ptr;
434 
435  for (aa->i = 0; aa->i < aa->argc; aa->i++) {
436  if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
437  aa->errnum = errno;
438  break;
439  }
440  }
441  return 0;
442 }
443 
444 #ifdef UTIME_EINVAL
445 NORETURN(static void utime_failed(struct apply_arg *));
446 static int utime_internal(const char *, void *);
447 #endif
448 
449 static VALUE
450 apply2files(int (*func)(const char *, void *), int argc, VALUE *argv, void *arg)
451 {
452  VALUE v;
453  const size_t size = sizeof(struct apply_filename);
454  const long len = (long)(offsetof(struct apply_arg, fn) + (size * argc));
455  struct apply_arg *aa = ALLOCV(v, len);
456 
457  aa->errnum = 0;
458  aa->argc = argc;
459  aa->arg = arg;
460  aa->func = func;
461 
462  for (aa->i = 0; aa->i < argc; aa->i++) {
463  VALUE path = rb_get_path(argv[aa->i]);
464 
465  path = rb_str_encode_ospath(path);
466  aa->fn[aa->i].ptr = RSTRING_PTR(path);
467  aa->fn[aa->i].path = path;
468  }
469 
470  rb_thread_call_without_gvl(no_gvl_apply2files, aa, RUBY_UBF_IO, 0);
471  if (aa->errnum) {
472 #ifdef UTIME_EINVAL
473  if (func == utime_internal) {
474  utime_failed(aa);
475  }
476 #endif
477  rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
478  }
479  if (v) {
480  ALLOCV_END(v);
481  }
482  return LONG2FIX(argc);
483 }
484 
485 /*
486  * call-seq:
487  * file.path -> filename
488  * file.to_path -> filename
489  *
490  * Returns the pathname used to create <i>file</i> as a string. Does
491  * not normalize the name.
492  *
493  * The pathname may not point to the file corresponding to <i>file</i>.
494  * For instance, the pathname becomes void when the file has been
495  * moved or deleted.
496  *
497  * This method raises IOError for a <i>file</i> created using
498  * File::Constants::TMPFILE because they don't have a pathname.
499  *
500  * File.new("testfile").path #=> "testfile"
501  * File.new("/tmp/../tmp/xxx", "w").path #=> "/tmp/../tmp/xxx"
502  *
503  */
504 
505 static VALUE
506 rb_file_path(VALUE obj)
507 {
508  rb_io_t *fptr;
509 
510  fptr = RFILE(rb_io_taint_check(obj))->fptr;
512 
513  if (NIL_P(fptr->pathv)) {
514  rb_raise(rb_eIOError, "File is unnamed (TMPFILE?)");
515  }
516 
517  return rb_str_dup(fptr->pathv);
518 }
519 
520 static size_t
521 stat_memsize(const void *p)
522 {
523  return sizeof(struct stat);
524 }
525 
526 static const rb_data_type_t stat_data_type = {
527  "stat",
528  {NULL, RUBY_TYPED_DEFAULT_FREE, stat_memsize,},
529  0, 0, RUBY_TYPED_FREE_IMMEDIATELY
530 };
531 
532 static VALUE
533 stat_new_0(VALUE klass, const struct stat *st)
534 {
535  struct stat *nst = 0;
536  VALUE obj = TypedData_Wrap_Struct(klass, &stat_data_type, 0);
537 
538  if (st) {
539  nst = ALLOC(struct stat);
540  *nst = *st;
541  RTYPEDDATA_DATA(obj) = nst;
542  }
543  return obj;
544 }
545 
546 VALUE
547 rb_stat_new(const struct stat *st)
548 {
549  return stat_new_0(rb_cStat, st);
550 }
551 
552 static struct stat*
553 get_stat(VALUE self)
554 {
555  struct stat* st;
556  TypedData_Get_Struct(self, struct stat, &stat_data_type, st);
557  if (!st) rb_raise(rb_eTypeError, "uninitialized File::Stat");
558  return st;
559 }
560 
561 static struct timespec stat_mtimespec(const struct stat *st);
562 
563 /*
564  * call-seq:
565  * stat <=> other_stat -> -1, 0, 1, nil
566  *
567  * Compares File::Stat objects by comparing their respective modification
568  * times.
569  *
570  * +nil+ is returned if +other_stat+ is not a File::Stat object
571  *
572  * f1 = File.new("f1", "w")
573  * sleep 1
574  * f2 = File.new("f2", "w")
575  * f1.stat <=> f2.stat #=> -1
576  */
577 
578 static VALUE
579 rb_stat_cmp(VALUE self, VALUE other)
580 {
581  if (rb_obj_is_kind_of(other, rb_obj_class(self))) {
582  struct timespec ts1 = stat_mtimespec(get_stat(self));
583  struct timespec ts2 = stat_mtimespec(get_stat(other));
584  if (ts1.tv_sec == ts2.tv_sec) {
585  if (ts1.tv_nsec == ts2.tv_nsec) return INT2FIX(0);
586  if (ts1.tv_nsec < ts2.tv_nsec) return INT2FIX(-1);
587  return INT2FIX(1);
588  }
589  if (ts1.tv_sec < ts2.tv_sec) return INT2FIX(-1);
590  return INT2FIX(1);
591  }
592  return Qnil;
593 }
594 
595 #define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
596 
597 #ifndef NUM2DEVT
598 # define NUM2DEVT(v) NUM2UINT(v)
599 #endif
600 #ifndef DEVT2NUM
601 # define DEVT2NUM(v) UINT2NUM(v)
602 #endif
603 #ifndef PRI_DEVT_PREFIX
604 # define PRI_DEVT_PREFIX ""
605 #endif
606 
607 /*
608  * call-seq:
609  * stat.dev -> integer
610  *
611  * Returns an integer representing the device on which <i>stat</i>
612  * resides.
613  *
614  * File.stat("testfile").dev #=> 774
615  */
616 
617 static VALUE
618 rb_stat_dev(VALUE self)
619 {
620 #if SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
621  return DEVT2NUM(get_stat(self)->st_dev);
622 #elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
623  return ULONG2NUM(get_stat(self)->st_dev);
624 #else
625  return ULL2NUM(get_stat(self)->st_dev);
626 #endif
627 }
628 
629 /*
630  * call-seq:
631  * stat.dev_major -> integer
632  *
633  * Returns the major part of <code>File_Stat#dev</code> or
634  * <code>nil</code>.
635  *
636  * File.stat("/dev/fd1").dev_major #=> 2
637  * File.stat("/dev/tty").dev_major #=> 5
638  */
639 
640 static VALUE
641 rb_stat_dev_major(VALUE self)
642 {
643 #if defined(major)
644  return UINT2NUM(major(get_stat(self)->st_dev));
645 #else
646  return Qnil;
647 #endif
648 }
649 
650 /*
651  * call-seq:
652  * stat.dev_minor -> integer
653  *
654  * Returns the minor part of <code>File_Stat#dev</code> or
655  * <code>nil</code>.
656  *
657  * File.stat("/dev/fd1").dev_minor #=> 1
658  * File.stat("/dev/tty").dev_minor #=> 0
659  */
660 
661 static VALUE
662 rb_stat_dev_minor(VALUE self)
663 {
664 #if defined(minor)
665  return UINT2NUM(minor(get_stat(self)->st_dev));
666 #else
667  return Qnil;
668 #endif
669 }
670 
671 /*
672  * call-seq:
673  * stat.ino -> integer
674  *
675  * Returns the inode number for <i>stat</i>.
676  *
677  * File.stat("testfile").ino #=> 1083669
678  *
679  */
680 
681 static VALUE
682 rb_stat_ino(VALUE self)
683 {
684 #ifdef HAVE_STRUCT_STAT_ST_INOHIGH
685  /* assume INTEGER_PACK_LSWORD_FIRST and st_inohigh is just next of st_ino */
686  return rb_integer_unpack(&get_stat(self)->st_ino, 2,
687  SIZEOF_STRUCT_STAT_ST_INO, 0,
690 #elif SIZEOF_STRUCT_STAT_ST_INO > SIZEOF_LONG
691  return ULL2NUM(get_stat(self)->st_ino);
692 #else
693  return ULONG2NUM(get_stat(self)->st_ino);
694 #endif
695 }
696 
697 /*
698  * call-seq:
699  * stat.mode -> integer
700  *
701  * Returns an integer representing the permission bits of
702  * <i>stat</i>. The meaning of the bits is platform dependent; on
703  * Unix systems, see <code>stat(2)</code>.
704  *
705  * File.chmod(0644, "testfile") #=> 1
706  * s = File.stat("testfile")
707  * sprintf("%o", s.mode) #=> "100644"
708  */
709 
710 static VALUE
711 rb_stat_mode(VALUE self)
712 {
713  return UINT2NUM(ST2UINT(get_stat(self)->st_mode));
714 }
715 
716 /*
717  * call-seq:
718  * stat.nlink -> integer
719  *
720  * Returns the number of hard links to <i>stat</i>.
721  *
722  * File.stat("testfile").nlink #=> 1
723  * File.link("testfile", "testfile.bak") #=> 0
724  * File.stat("testfile").nlink #=> 2
725  *
726  */
727 
728 static VALUE
729 rb_stat_nlink(VALUE self)
730 {
731  /* struct stat::st_nlink is nlink_t in POSIX. Not the case for Windows. */
732  const struct stat *ptr = get_stat(self);
733 
734  if (sizeof(ptr->st_nlink) <= sizeof(int)) {
735  return UINT2NUM((unsigned)ptr->st_nlink);
736  }
737  else if (sizeof(ptr->st_nlink) == sizeof(long)) {
738  return ULONG2NUM((unsigned long)ptr->st_nlink);
739  }
740  else if (sizeof(ptr->st_nlink) == sizeof(LONG_LONG)) {
741  return ULL2NUM((unsigned LONG_LONG)ptr->st_nlink);
742  }
743  else {
744  rb_bug(":FIXME: don't know what to do");
745  }
746 }
747 
748 /*
749  * call-seq:
750  * stat.uid -> integer
751  *
752  * Returns the numeric user id of the owner of <i>stat</i>.
753  *
754  * File.stat("testfile").uid #=> 501
755  *
756  */
757 
758 static VALUE
759 rb_stat_uid(VALUE self)
760 {
761  return UIDT2NUM(get_stat(self)->st_uid);
762 }
763 
764 /*
765  * call-seq:
766  * stat.gid -> integer
767  *
768  * Returns the numeric group id of the owner of <i>stat</i>.
769  *
770  * File.stat("testfile").gid #=> 500
771  *
772  */
773 
774 static VALUE
775 rb_stat_gid(VALUE self)
776 {
777  return GIDT2NUM(get_stat(self)->st_gid);
778 }
779 
780 /*
781  * call-seq:
782  * stat.rdev -> integer or nil
783  *
784  * Returns an integer representing the device type on which
785  * <i>stat</i> resides. Returns <code>nil</code> if the operating
786  * system doesn't support this feature.
787  *
788  * File.stat("/dev/fd1").rdev #=> 513
789  * File.stat("/dev/tty").rdev #=> 1280
790  */
791 
792 static VALUE
793 rb_stat_rdev(VALUE self)
794 {
795 #ifdef HAVE_STRUCT_STAT_ST_RDEV
796 # if SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
797  return DEVT2NUM(get_stat(self)->st_rdev);
798 # elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
799  return ULONG2NUM(get_stat(self)->st_rdev);
800 # else
801  return ULL2NUM(get_stat(self)->st_rdev);
802 # endif
803 #else
804  return Qnil;
805 #endif
806 }
807 
808 /*
809  * call-seq:
810  * stat.rdev_major -> integer
811  *
812  * Returns the major part of <code>File_Stat#rdev</code> or
813  * <code>nil</code>.
814  *
815  * File.stat("/dev/fd1").rdev_major #=> 2
816  * File.stat("/dev/tty").rdev_major #=> 5
817  */
818 
819 static VALUE
820 rb_stat_rdev_major(VALUE self)
821 {
822 #if defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
823  return UINT2NUM(major(get_stat(self)->st_rdev));
824 #else
825  return Qnil;
826 #endif
827 }
828 
829 /*
830  * call-seq:
831  * stat.rdev_minor -> integer
832  *
833  * Returns the minor part of <code>File_Stat#rdev</code> or
834  * <code>nil</code>.
835  *
836  * File.stat("/dev/fd1").rdev_minor #=> 1
837  * File.stat("/dev/tty").rdev_minor #=> 0
838  */
839 
840 static VALUE
841 rb_stat_rdev_minor(VALUE self)
842 {
843 #if defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
844  return UINT2NUM(minor(get_stat(self)->st_rdev));
845 #else
846  return Qnil;
847 #endif
848 }
849 
850 /*
851  * call-seq:
852  * stat.size -> integer
853  *
854  * Returns the size of <i>stat</i> in bytes.
855  *
856  * File.stat("testfile").size #=> 66
857  */
858 
859 static VALUE
860 rb_stat_size(VALUE self)
861 {
862  return OFFT2NUM(get_stat(self)->st_size);
863 }
864 
865 /*
866  * call-seq:
867  * stat.blksize -> integer or nil
868  *
869  * Returns the native file system's block size. Will return <code>nil</code>
870  * on platforms that don't support this information.
871  *
872  * File.stat("testfile").blksize #=> 4096
873  *
874  */
875 
876 static VALUE
877 rb_stat_blksize(VALUE self)
878 {
879 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
880  return ULONG2NUM(get_stat(self)->st_blksize);
881 #else
882  return Qnil;
883 #endif
884 }
885 
886 /*
887  * call-seq:
888  * stat.blocks -> integer or nil
889  *
890  * Returns the number of native file system blocks allocated for this
891  * file, or <code>nil</code> if the operating system doesn't
892  * support this feature.
893  *
894  * File.stat("testfile").blocks #=> 2
895  */
896 
897 static VALUE
898 rb_stat_blocks(VALUE self)
899 {
900 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
901 # if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
902  return ULL2NUM(get_stat(self)->st_blocks);
903 # else
904  return ULONG2NUM(get_stat(self)->st_blocks);
905 # endif
906 #else
907  return Qnil;
908 #endif
909 }
910 
911 static struct timespec
912 stat_atimespec(const struct stat *st)
913 {
914  struct timespec ts;
915  ts.tv_sec = st->st_atime;
916 #if defined(HAVE_STRUCT_STAT_ST_ATIM)
917  ts.tv_nsec = st->st_atim.tv_nsec;
918 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
919  ts.tv_nsec = st->st_atimespec.tv_nsec;
920 #elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
921  ts.tv_nsec = (long)st->st_atimensec;
922 #else
923  ts.tv_nsec = 0;
924 #endif
925  return ts;
926 }
927 
928 static VALUE
929 stat_time(const struct timespec ts)
930 {
931  return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
932 }
933 
934 static VALUE
935 stat_atime(const struct stat *st)
936 {
937  return stat_time(stat_atimespec(st));
938 }
939 
940 static struct timespec
941 stat_mtimespec(const struct stat *st)
942 {
943  struct timespec ts;
944  ts.tv_sec = st->st_mtime;
945 #if defined(HAVE_STRUCT_STAT_ST_MTIM)
946  ts.tv_nsec = st->st_mtim.tv_nsec;
947 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
948  ts.tv_nsec = st->st_mtimespec.tv_nsec;
949 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
950  ts.tv_nsec = (long)st->st_mtimensec;
951 #else
952  ts.tv_nsec = 0;
953 #endif
954  return ts;
955 }
956 
957 static VALUE
958 stat_mtime(const struct stat *st)
959 {
960  return stat_time(stat_mtimespec(st));
961 }
962 
963 static struct timespec
964 stat_ctimespec(const struct stat *st)
965 {
966  struct timespec ts;
967  ts.tv_sec = st->st_ctime;
968 #if defined(HAVE_STRUCT_STAT_ST_CTIM)
969  ts.tv_nsec = st->st_ctim.tv_nsec;
970 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
971  ts.tv_nsec = st->st_ctimespec.tv_nsec;
972 #elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
973  ts.tv_nsec = (long)st->st_ctimensec;
974 #else
975  ts.tv_nsec = 0;
976 #endif
977  return ts;
978 }
979 
980 static VALUE
981 stat_ctime(const struct stat *st)
982 {
983  return stat_time(stat_ctimespec(st));
984 }
985 
986 #define HAVE_STAT_BIRTHTIME
987 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
988 typedef struct stat statx_data;
989 static VALUE
990 stat_birthtime(const struct stat *st)
991 {
992  const struct timespec *ts = &st->st_birthtimespec;
993  return rb_time_nano_new(ts->tv_sec, ts->tv_nsec);
994 }
995 #elif defined(_WIN32)
996 typedef struct stat statx_data;
997 # define stat_birthtime stat_ctime
998 #else
999 # undef HAVE_STAT_BIRTHTIME
1000 #endif
1001 
1002 /*
1003  * call-seq:
1004  * stat.atime -> time
1005  *
1006  * Returns the last access time for this file as an object of class
1007  * Time.
1008  *
1009  * File.stat("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
1010  *
1011  */
1012 
1013 static VALUE
1014 rb_stat_atime(VALUE self)
1015 {
1016  return stat_atime(get_stat(self));
1017 }
1018 
1019 /*
1020  * call-seq:
1021  * stat.mtime -> aTime
1022  *
1023  * Returns the modification time of <i>stat</i>.
1024  *
1025  * File.stat("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
1026  *
1027  */
1028 
1029 static VALUE
1030 rb_stat_mtime(VALUE self)
1031 {
1032  return stat_mtime(get_stat(self));
1033 }
1034 
1035 /*
1036  * call-seq:
1037  * stat.ctime -> aTime
1038  *
1039  * Returns the change time for <i>stat</i> (that is, the time
1040  * directory information about the file was changed, not the file
1041  * itself).
1042  *
1043  * Note that on Windows (NTFS), returns creation time (birth time).
1044  *
1045  * File.stat("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
1046  *
1047  */
1048 
1049 static VALUE
1050 rb_stat_ctime(VALUE self)
1051 {
1052  return stat_ctime(get_stat(self));
1053 }
1054 
1055 #if defined(HAVE_STAT_BIRTHTIME)
1056 /*
1057  * call-seq:
1058  * stat.birthtime -> aTime
1059  *
1060  * Returns the birth time for <i>stat</i>.
1061  *
1062  * If the platform doesn't have birthtime, raises NotImplementedError.
1063  *
1064  * File.write("testfile", "foo")
1065  * sleep 10
1066  * File.write("testfile", "bar")
1067  * sleep 10
1068  * File.chmod(0644, "testfile")
1069  * sleep 10
1070  * File.read("testfile")
1071  * File.stat("testfile").birthtime #=> 2014-02-24 11:19:17 +0900
1072  * File.stat("testfile").mtime #=> 2014-02-24 11:19:27 +0900
1073  * File.stat("testfile").ctime #=> 2014-02-24 11:19:37 +0900
1074  * File.stat("testfile").atime #=> 2014-02-24 11:19:47 +0900
1075  *
1076  */
1077 
1078 static VALUE
1079 rb_stat_birthtime(VALUE self)
1080 {
1081  return stat_birthtime(get_stat(self));
1082 }
1083 #else
1084 # define rb_stat_birthtime rb_f_notimplement
1085 #endif
1086 
1087 /*
1088  * call-seq:
1089  * stat.inspect -> string
1090  *
1091  * Produce a nicely formatted description of <i>stat</i>.
1092  *
1093  * File.stat("/etc/passwd").inspect
1094  * #=> "#<File::Stat dev=0xe000005, ino=1078078, mode=0100644,
1095  * # nlink=1, uid=0, gid=0, rdev=0x0, size=1374, blksize=4096,
1096  * # blocks=8, atime=Wed Dec 10 10:16:12 CST 2003,
1097  * # mtime=Fri Sep 12 15:41:41 CDT 2003,
1098  * # ctime=Mon Oct 27 11:20:27 CST 2003,
1099  * # birthtime=Mon Aug 04 08:13:49 CDT 2003>"
1100  */
1101 
1102 static VALUE
1103 rb_stat_inspect(VALUE self)
1104 {
1105  VALUE str;
1106  size_t i;
1107  static const struct {
1108  const char *name;
1109  VALUE (*func)(VALUE);
1110  } member[] = {
1111  {"dev", rb_stat_dev},
1112  {"ino", rb_stat_ino},
1113  {"mode", rb_stat_mode},
1114  {"nlink", rb_stat_nlink},
1115  {"uid", rb_stat_uid},
1116  {"gid", rb_stat_gid},
1117  {"rdev", rb_stat_rdev},
1118  {"size", rb_stat_size},
1119  {"blksize", rb_stat_blksize},
1120  {"blocks", rb_stat_blocks},
1121  {"atime", rb_stat_atime},
1122  {"mtime", rb_stat_mtime},
1123  {"ctime", rb_stat_ctime},
1124 #if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1125  {"birthtime", rb_stat_birthtime},
1126 #endif
1127  };
1128 
1129  struct stat* st;
1130  TypedData_Get_Struct(self, struct stat, &stat_data_type, st);
1131  if (!st) {
1132  return rb_sprintf("#<%s: uninitialized>", rb_obj_classname(self));
1133  }
1134 
1135  str = rb_str_buf_new2("#<");
1136  rb_str_buf_cat2(str, rb_obj_classname(self));
1137  rb_str_buf_cat2(str, " ");
1138 
1139  for (i = 0; i < sizeof(member)/sizeof(member[0]); i++) {
1140  VALUE v;
1141 
1142  if (i > 0) {
1143  rb_str_buf_cat2(str, ", ");
1144  }
1145  rb_str_buf_cat2(str, member[i].name);
1146  rb_str_buf_cat2(str, "=");
1147  v = (*member[i].func)(self);
1148  if (i == 2) { /* mode */
1149  rb_str_catf(str, "0%lo", (unsigned long)NUM2ULONG(v));
1150  }
1151  else if (i == 0 || i == 6) { /* dev/rdev */
1152  rb_str_catf(str, "0x%"PRI_DEVT_PREFIX"x", NUM2DEVT(v));
1153  }
1154  else {
1155  rb_str_append(str, rb_inspect(v));
1156  }
1157  }
1158  rb_str_buf_cat2(str, ">");
1159 
1160  return str;
1161 }
1162 
1163 typedef struct no_gvl_stat_data {
1164  struct stat *st;
1165  union {
1166  const char *path;
1167  int fd;
1168  } file;
1170 
1171 static VALUE
1172 no_gvl_fstat(void *data)
1173 {
1174  no_gvl_stat_data *arg = data;
1175  return (VALUE)fstat(arg->file.fd, arg->st);
1176 }
1177 
1178 static int
1179 fstat_without_gvl(int fd, struct stat *st)
1180 {
1181  no_gvl_stat_data data;
1182 
1183  data.file.fd = fd;
1184  data.st = st;
1185 
1186  return (int)(VALUE)rb_thread_io_blocking_region(no_gvl_fstat, &data, fd);
1187 }
1188 
1189 static void *
1190 no_gvl_stat(void * data)
1191 {
1192  no_gvl_stat_data *arg = data;
1193  return (void *)(VALUE)STAT(arg->file.path, arg->st);
1194 }
1195 
1196 static int
1197 stat_without_gvl(const char *path, struct stat *st)
1198 {
1199  no_gvl_stat_data data;
1200 
1201  data.file.path = path;
1202  data.st = st;
1203 
1204  return (int)(VALUE)rb_thread_call_without_gvl(no_gvl_stat, &data,
1205  RUBY_UBF_IO, NULL);
1206 }
1207 
1208 #if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1209  defined(HAVE_STRUCT_STATX_STX_BTIME)
1210 
1211 # ifndef HAVE_STATX
1212 # ifdef HAVE_SYSCALL_H
1213 # include <syscall.h>
1214 # elif defined HAVE_SYS_SYSCALL_H
1215 # include <sys/syscall.h>
1216 # endif
1217 # if defined __linux__
1218 # include <linux/stat.h>
1219 static inline int
1220 statx(int dirfd, const char *pathname, int flags,
1221  unsigned int mask, struct statx *statxbuf)
1222 {
1223  return (int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1224 }
1225 # endif
1226 # endif
1227 
1228 typedef struct no_gvl_statx_data {
1229  struct statx *stx;
1230  int fd;
1231  const char *path;
1232  int flags;
1233  unsigned int mask;
1234 } no_gvl_statx_data;
1235 
1236 static VALUE
1237 io_blocking_statx(void *data)
1238 {
1239  no_gvl_statx_data *arg = data;
1240  return (VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1241 }
1242 
1243 static void *
1244 no_gvl_statx(void *data)
1245 {
1246  return (void *)io_blocking_statx(data);
1247 }
1248 
1249 static int
1250 statx_without_gvl(const char *path, struct statx *stx, unsigned int mask)
1251 {
1252  no_gvl_statx_data data = {stx, AT_FDCWD, path, 0, mask};
1253 
1254  /* call statx(2) with pathname */
1255  return (int)(VALUE)rb_thread_call_without_gvl(no_gvl_statx, &data,
1256  RUBY_UBF_IO, NULL);
1257 }
1258 
1259 static int
1260 fstatx_without_gvl(int fd, struct statx *stx, unsigned int mask)
1261 {
1262  no_gvl_statx_data data = {stx, fd, "", AT_EMPTY_PATH, mask};
1263 
1264  /* call statx(2) with fd */
1265  return (int)rb_thread_io_blocking_region(io_blocking_statx, &data, fd);
1266 }
1267 
1268 static int
1269 rb_statx(VALUE file, struct statx *stx, unsigned int mask)
1270 {
1271  VALUE tmp;
1272  int result;
1273 
1274  tmp = rb_check_convert_type_with_id(file, T_FILE, "IO", idTo_io);
1275  if (!NIL_P(tmp)) {
1276  rb_io_t *fptr;
1277  GetOpenFile(tmp, fptr);
1278  result = fstatx_without_gvl(fptr->fd, stx, mask);
1279  file = tmp;
1280  }
1281  else {
1282  FilePathValue(file);
1283  file = rb_str_encode_ospath(file);
1284  result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1285  }
1286  RB_GC_GUARD(file);
1287  return result;
1288 }
1289 
1290 # define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1291 
1292 NORETURN(static void statx_notimplement(const char *field_name));
1293 
1294 /* rb_notimplement() shows "function is unimplemented on this machine".
1295  It is not applicable to statx which behavior depends on the filesystem. */
1296 static void
1297 statx_notimplement(const char *field_name)
1298 {
1300  "%s is unimplemented on this filesystem",
1301  field_name);
1302 }
1303 
1304 static VALUE
1305 statx_birthtime(const struct statx *stx, VALUE fname)
1306 {
1307  if (!statx_has_birthtime(stx)) {
1308  /* birthtime is not supported on the filesystem */
1309  statx_notimplement("birthtime");
1310  }
1311  return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1312 }
1313 
1314 typedef struct statx statx_data;
1315 # define HAVE_STAT_BIRTHTIME
1316 
1317 #elif defined(HAVE_STAT_BIRTHTIME)
1318 # define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1319 # define fstatx_without_gvl(fd, st, mask) fstat_without_gvl(fd, st)
1320 # define statx_birthtime(st, fname) stat_birthtime(st)
1321 # define statx_has_birthtime(st) 1
1322 # define rb_statx(file, st, mask) rb_stat(file, st)
1323 #else
1324 # define statx_has_birthtime(st) 0
1325 #endif
1326 
1327 static int
1328 rb_stat(VALUE file, struct stat *st)
1329 {
1330  VALUE tmp;
1331  int result;
1332 
1333  tmp = rb_check_convert_type_with_id(file, T_FILE, "IO", idTo_io);
1334  if (!NIL_P(tmp)) {
1335  rb_io_t *fptr;
1336 
1337  GetOpenFile(tmp, fptr);
1338  result = fstat_without_gvl(fptr->fd, st);
1339  file = tmp;
1340  }
1341  else {
1342  FilePathValue(file);
1343  file = rb_str_encode_ospath(file);
1344  result = stat_without_gvl(RSTRING_PTR(file), st);
1345  }
1346  RB_GC_GUARD(file);
1347  return result;
1348 }
1349 
1350 /*
1351  * call-seq:
1352  * File.stat(file_name) -> stat
1353  *
1354  * Returns a File::Stat object for the named file (see File::Stat).
1355  *
1356  * File.stat("testfile").mtime #=> Tue Apr 08 12:58:04 CDT 2003
1357  *
1358  */
1359 
1360 static VALUE
1361 rb_file_s_stat(VALUE klass, VALUE fname)
1362 {
1363  struct stat st;
1364 
1365  FilePathValue(fname);
1366  fname = rb_str_encode_ospath(fname);
1367  if (stat_without_gvl(RSTRING_PTR(fname), &st) < 0) {
1368  rb_sys_fail_path(fname);
1369  }
1370  return rb_stat_new(&st);
1371 }
1372 
1373 /*
1374  * call-seq:
1375  * ios.stat -> stat
1376  *
1377  * Returns status information for <em>ios</em> as an object of type
1378  * File::Stat.
1379  *
1380  * f = File.new("testfile")
1381  * s = f.stat
1382  * "%o" % s.mode #=> "100644"
1383  * s.blksize #=> 4096
1384  * s.atime #=> Wed Apr 09 08:53:54 CDT 2003
1385  *
1386  */
1387 
1388 static VALUE
1389 rb_io_stat(VALUE obj)
1390 {
1391  rb_io_t *fptr;
1392  struct stat st;
1393 
1394  GetOpenFile(obj, fptr);
1395  if (fstat(fptr->fd, &st) == -1) {
1396  rb_sys_fail_path(fptr->pathv);
1397  }
1398  return rb_stat_new(&st);
1399 }
1400 
1401 #ifdef HAVE_LSTAT
1402 static void *
1403 no_gvl_lstat(void *ptr)
1404 {
1405  no_gvl_stat_data *arg = ptr;
1406  return (void *)(VALUE)lstat(arg->file.path, arg->st);
1407 }
1408 
1409 static int
1410 lstat_without_gvl(const char *path, struct stat *st)
1411 {
1412  no_gvl_stat_data data;
1413 
1414  data.file.path = path;
1415  data.st = st;
1416 
1417  return (int)(VALUE)rb_thread_call_without_gvl(no_gvl_lstat, &data,
1418  RUBY_UBF_IO, NULL);
1419 }
1420 #endif /* HAVE_LSTAT */
1421 
1422 /*
1423  * call-seq:
1424  * File.lstat(file_name) -> stat
1425  *
1426  * Same as File::stat, but does not follow the last symbolic link.
1427  * Instead, reports on the link itself.
1428  *
1429  * File.symlink("testfile", "link2test") #=> 0
1430  * File.stat("testfile").size #=> 66
1431  * File.lstat("link2test").size #=> 8
1432  * File.stat("link2test").size #=> 66
1433  *
1434  */
1435 
1436 static VALUE
1437 rb_file_s_lstat(VALUE klass, VALUE fname)
1438 {
1439 #ifdef HAVE_LSTAT
1440  struct stat st;
1441 
1442  FilePathValue(fname);
1443  fname = rb_str_encode_ospath(fname);
1444  if (lstat_without_gvl(StringValueCStr(fname), &st) == -1) {
1445  rb_sys_fail_path(fname);
1446  }
1447  return rb_stat_new(&st);
1448 #else
1449  return rb_file_s_stat(klass, fname);
1450 #endif
1451 }
1452 
1453 /*
1454  * call-seq:
1455  * file.lstat -> stat
1456  *
1457  * Same as IO#stat, but does not follow the last symbolic link.
1458  * Instead, reports on the link itself.
1459  *
1460  * File.symlink("testfile", "link2test") #=> 0
1461  * File.stat("testfile").size #=> 66
1462  * f = File.new("link2test")
1463  * f.lstat.size #=> 8
1464  * f.stat.size #=> 66
1465  */
1466 
1467 static VALUE
1468 rb_file_lstat(VALUE obj)
1469 {
1470 #ifdef HAVE_LSTAT
1471  rb_io_t *fptr;
1472  struct stat st;
1473  VALUE path;
1474 
1475  GetOpenFile(obj, fptr);
1476  if (NIL_P(fptr->pathv)) return Qnil;
1477  path = rb_str_encode_ospath(fptr->pathv);
1478  if (lstat_without_gvl(RSTRING_PTR(path), &st) == -1) {
1479  rb_sys_fail_path(fptr->pathv);
1480  }
1481  return rb_stat_new(&st);
1482 #else
1483  return rb_io_stat(obj);
1484 #endif
1485 }
1486 
1487 static int
1488 rb_group_member(GETGROUPS_T gid)
1489 {
1490 #if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1491  return FALSE;
1492 #else
1493  int rv = FALSE;
1494  int groups;
1495  VALUE v = 0;
1496  GETGROUPS_T *gary;
1497  int anum = -1;
1498 
1499  if (getgid() == gid || getegid() == gid)
1500  return TRUE;
1501 
1502  groups = getgroups(0, NULL);
1503  gary = ALLOCV_N(GETGROUPS_T, v, groups);
1504  anum = getgroups(groups, gary);
1505  while (--anum >= 0) {
1506  if (gary[anum] == gid) {
1507  rv = TRUE;
1508  break;
1509  }
1510  }
1511  if (v)
1512  ALLOCV_END(v);
1513 
1514  return rv;
1515 #endif
1516 }
1517 
1518 #ifndef S_IXUGO
1519 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1520 #endif
1521 
1522 #if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1523 #define USE_GETEUID 1
1524 #endif
1525 
1526 #ifndef HAVE_EACCESS
1527 int
1528 eaccess(const char *path, int mode)
1529 {
1530 #ifdef USE_GETEUID
1531  struct stat st;
1532  rb_uid_t euid;
1533 
1534  euid = geteuid();
1535 
1536  /* no setuid nor setgid. run shortcut. */
1537  if (getuid() == euid && getgid() == getegid())
1538  return access(path, mode);
1539 
1540  if (STAT(path, &st) < 0)
1541  return -1;
1542 
1543  if (euid == 0) {
1544  /* Root can read or write any file. */
1545  if (!(mode & X_OK))
1546  return 0;
1547 
1548  /* Root can execute any file that has any one of the execute
1549  bits set. */
1550  if (st.st_mode & S_IXUGO)
1551  return 0;
1552 
1553  return -1;
1554  }
1555 
1556  if (st.st_uid == euid) /* owner */
1557  mode <<= 6;
1558  else if (rb_group_member(st.st_gid))
1559  mode <<= 3;
1560 
1561  if ((int)(st.st_mode & mode) == mode) return 0;
1562 
1563  return -1;
1564 #else
1565  return access(path, mode);
1566 #endif
1567 }
1568 #endif
1569 
1570 struct access_arg {
1571  const char *path;
1572  int mode;
1573 };
1574 
1575 static void *
1576 nogvl_eaccess(void *ptr)
1577 {
1578  struct access_arg *aa = ptr;
1579 
1580  return (void *)(VALUE)eaccess(aa->path, aa->mode);
1581 }
1582 
1583 static int
1584 rb_eaccess(VALUE fname, int mode)
1585 {
1586  struct access_arg aa;
1587 
1588  FilePathValue(fname);
1589  fname = rb_str_encode_ospath(fname);
1590  aa.path = StringValueCStr(fname);
1591  aa.mode = mode;
1592 
1593  return (int)(VALUE)rb_thread_call_without_gvl(nogvl_eaccess, &aa,
1594  RUBY_UBF_IO, 0);
1595 }
1596 
1597 static void *
1598 nogvl_access(void *ptr)
1599 {
1600  struct access_arg *aa = ptr;
1601 
1602  return (void *)(VALUE)access(aa->path, aa->mode);
1603 }
1604 
1605 static int
1606 rb_access(VALUE fname, int mode)
1607 {
1608  struct access_arg aa;
1609 
1610  FilePathValue(fname);
1611  fname = rb_str_encode_ospath(fname);
1612  aa.path = StringValueCStr(fname);
1613  aa.mode = mode;
1614 
1615  return (int)(VALUE)rb_thread_call_without_gvl(nogvl_access, &aa,
1616  RUBY_UBF_IO, 0);
1617 }
1618 
1619 /*
1620  * Document-class: FileTest
1621  *
1622  * FileTest implements file test operations similar to those used in
1623  * File::Stat. It exists as a standalone module, and its methods are
1624  * also insinuated into the File class. (Note that this is not done
1625  * by inclusion: the interpreter cheats).
1626  *
1627  */
1628 
1629 /*
1630  * Document-method: directory?
1631  *
1632  * call-seq:
1633  * File.directory?(file_name) -> true or false
1634  *
1635  * Returns <code>true</code> if the named file is a directory,
1636  * or a symlink that points at a directory, and <code>false</code>
1637  * otherwise.
1638  *
1639  * _file_name_ can be an IO object.
1640  *
1641  * File.directory?(".")
1642  */
1643 
1644 VALUE
1646 {
1647 #ifndef S_ISDIR
1648 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1649 #endif
1650 
1651  struct stat st;
1652 
1653  if (rb_stat(fname, &st) < 0) return Qfalse;
1654  if (S_ISDIR(st.st_mode)) return Qtrue;
1655  return Qfalse;
1656 }
1657 
1658 /*
1659  * call-seq:
1660  * File.pipe?(file_name) -> true or false
1661  *
1662  * Returns <code>true</code> if the named file is a pipe.
1663  *
1664  * _file_name_ can be an IO object.
1665  */
1666 
1667 static VALUE
1668 rb_file_pipe_p(VALUE obj, VALUE fname)
1669 {
1670 #ifdef S_IFIFO
1671 # ifndef S_ISFIFO
1672 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1673 # endif
1674 
1675  struct stat st;
1676 
1677  if (rb_stat(fname, &st) < 0) return Qfalse;
1678  if (S_ISFIFO(st.st_mode)) return Qtrue;
1679 
1680 #endif
1681  return Qfalse;
1682 }
1683 
1684 /*
1685  * call-seq:
1686  * File.symlink?(file_name) -> true or false
1687  *
1688  * Returns <code>true</code> if the named file is a symbolic link.
1689  */
1690 
1691 static VALUE
1692 rb_file_symlink_p(VALUE obj, VALUE fname)
1693 {
1694 #ifndef S_ISLNK
1695 # ifdef _S_ISLNK
1696 # define S_ISLNK(m) _S_ISLNK(m)
1697 # else
1698 # ifdef _S_IFLNK
1699 # define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1700 # else
1701 # ifdef S_IFLNK
1702 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1703 # endif
1704 # endif
1705 # endif
1706 #endif
1707 
1708 #ifdef S_ISLNK
1709  struct stat st;
1710 
1711  FilePathValue(fname);
1712  fname = rb_str_encode_ospath(fname);
1713  if (lstat_without_gvl(StringValueCStr(fname), &st) < 0) return Qfalse;
1714  if (S_ISLNK(st.st_mode)) return Qtrue;
1715 #endif
1716 
1717  return Qfalse;
1718 }
1719 
1720 /*
1721  * call-seq:
1722  * File.socket?(file_name) -> true or false
1723  *
1724  * Returns <code>true</code> if the named file is a socket.
1725  *
1726  * _file_name_ can be an IO object.
1727  */
1728 
1729 static VALUE
1730 rb_file_socket_p(VALUE obj, VALUE fname)
1731 {
1732 #ifndef S_ISSOCK
1733 # ifdef _S_ISSOCK
1734 # define S_ISSOCK(m) _S_ISSOCK(m)
1735 # else
1736 # ifdef _S_IFSOCK
1737 # define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1738 # else
1739 # ifdef S_IFSOCK
1740 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1741 # endif
1742 # endif
1743 # endif
1744 #endif
1745 
1746 #ifdef S_ISSOCK
1747  struct stat st;
1748 
1749  if (rb_stat(fname, &st) < 0) return Qfalse;
1750  if (S_ISSOCK(st.st_mode)) return Qtrue;
1751 
1752 #endif
1753  return Qfalse;
1754 }
1755 
1756 /*
1757  * call-seq:
1758  * File.blockdev?(file_name) -> true or false
1759  *
1760  * Returns <code>true</code> if the named file is a block device.
1761  *
1762  * _file_name_ can be an IO object.
1763  */
1764 
1765 static VALUE
1766 rb_file_blockdev_p(VALUE obj, VALUE fname)
1767 {
1768 #ifndef S_ISBLK
1769 # ifdef S_IFBLK
1770 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1771 # else
1772 # define S_ISBLK(m) (0) /* anytime false */
1773 # endif
1774 #endif
1775 
1776 #ifdef S_ISBLK
1777  struct stat st;
1778 
1779  if (rb_stat(fname, &st) < 0) return Qfalse;
1780  if (S_ISBLK(st.st_mode)) return Qtrue;
1781 
1782 #endif
1783  return Qfalse;
1784 }
1785 
1786 /*
1787  * call-seq:
1788  * File.chardev?(file_name) -> true or false
1789  *
1790  * Returns <code>true</code> if the named file is a character device.
1791  *
1792  * _file_name_ can be an IO object.
1793  */
1794 static VALUE
1795 rb_file_chardev_p(VALUE obj, VALUE fname)
1796 {
1797 #ifndef S_ISCHR
1798 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1799 #endif
1800 
1801  struct stat st;
1802 
1803  if (rb_stat(fname, &st) < 0) return Qfalse;
1804  if (S_ISCHR(st.st_mode)) return Qtrue;
1805 
1806  return Qfalse;
1807 }
1808 
1809 /*
1810  * call-seq:
1811  * File.exist?(file_name) -> true or false
1812  *
1813  * Return <code>true</code> if the named file exists.
1814  *
1815  * _file_name_ can be an IO object.
1816  *
1817  * "file exists" means that stat() or fstat() system call is successful.
1818  */
1819 
1820 static VALUE
1821 rb_file_exist_p(VALUE obj, VALUE fname)
1822 {
1823  struct stat st;
1824 
1825  if (rb_stat(fname, &st) < 0) return Qfalse;
1826  return Qtrue;
1827 }
1828 
1829 /* :nodoc: */
1830 static VALUE
1831 rb_file_exists_p(VALUE obj, VALUE fname)
1832 {
1833  const char *s = "FileTest#exist?";
1834  if (obj == rb_mFileTest) {
1835  s = "FileTest.exist?";
1836  }
1837  else if (obj == rb_cFile ||
1838  (RB_TYPE_P(obj, T_CLASS) &&
1840  s = "File.exist?";
1841  }
1842  rb_warn_deprecated("%.*ss?", s, (int)(strlen(s)-1), s);
1843  return rb_file_exist_p(obj, fname);
1844 }
1845 
1846 /*
1847  * call-seq:
1848  * File.readable?(file_name) -> true or false
1849  *
1850  * Returns <code>true</code> if the named file is readable by the effective
1851  * user and group id of this process. See eaccess(3).
1852  *
1853  * Note that some OS-level security features may cause this to return true
1854  * even though the file is not readable by the effective user/group.
1855  */
1856 
1857 static VALUE
1858 rb_file_readable_p(VALUE obj, VALUE fname)
1859 {
1860  return RBOOL(rb_eaccess(fname, R_OK) >= 0);
1861 }
1862 
1863 /*
1864  * call-seq:
1865  * File.readable_real?(file_name) -> true or false
1866  *
1867  * Returns <code>true</code> if the named file is readable by the real
1868  * user and group id of this process. See access(3).
1869  *
1870  * Note that some OS-level security features may cause this to return true
1871  * even though the file is not readable by the real user/group.
1872  */
1873 
1874 static VALUE
1875 rb_file_readable_real_p(VALUE obj, VALUE fname)
1876 {
1877  return RBOOL(rb_access(fname, R_OK) >= 0);
1878 }
1879 
1880 #ifndef S_IRUGO
1881 # define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
1882 #endif
1883 
1884 #ifndef S_IWUGO
1885 # define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
1886 #endif
1887 
1888 /*
1889  * call-seq:
1890  * File.world_readable?(file_name) -> integer or nil
1891  *
1892  * If <i>file_name</i> is readable by others, returns an integer
1893  * representing the file permission bits of <i>file_name</i>. Returns
1894  * <code>nil</code> otherwise. The meaning of the bits is platform
1895  * dependent; on Unix systems, see <code>stat(2)</code>.
1896  *
1897  * _file_name_ can be an IO object.
1898  *
1899  * File.world_readable?("/etc/passwd") #=> 420
1900  * m = File.world_readable?("/etc/passwd")
1901  * sprintf("%o", m) #=> "644"
1902  */
1903 
1904 static VALUE
1905 rb_file_world_readable_p(VALUE obj, VALUE fname)
1906 {
1907 #ifdef S_IROTH
1908  struct stat st;
1909 
1910  if (rb_stat(fname, &st) < 0) return Qnil;
1911  if ((st.st_mode & (S_IROTH)) == S_IROTH) {
1912  return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
1913  }
1914 #endif
1915  return Qnil;
1916 }
1917 
1918 /*
1919  * call-seq:
1920  * File.writable?(file_name) -> true or false
1921  *
1922  * Returns <code>true</code> if the named file is writable by the effective
1923  * user and group id of this process. See eaccess(3).
1924  *
1925  * Note that some OS-level security features may cause this to return true
1926  * even though the file is not writable by the effective user/group.
1927  */
1928 
1929 static VALUE
1930 rb_file_writable_p(VALUE obj, VALUE fname)
1931 {
1932  return RBOOL(rb_eaccess(fname, W_OK) >= 0);
1933 }
1934 
1935 /*
1936  * call-seq:
1937  * File.writable_real?(file_name) -> true or false
1938  *
1939  * Returns <code>true</code> if the named file is writable by the real
1940  * user and group id of this process. See access(3).
1941  *
1942  * Note that some OS-level security features may cause this to return true
1943  * even though the file is not writable by the real user/group.
1944  */
1945 
1946 static VALUE
1947 rb_file_writable_real_p(VALUE obj, VALUE fname)
1948 {
1949  return RBOOL(rb_access(fname, W_OK) >= 0);
1950 }
1951 
1952 /*
1953  * call-seq:
1954  * File.world_writable?(file_name) -> integer or nil
1955  *
1956  * If <i>file_name</i> is writable by others, returns an integer
1957  * representing the file permission bits of <i>file_name</i>. Returns
1958  * <code>nil</code> otherwise. The meaning of the bits is platform
1959  * dependent; on Unix systems, see <code>stat(2)</code>.
1960  *
1961  * _file_name_ can be an IO object.
1962  *
1963  * File.world_writable?("/tmp") #=> 511
1964  * m = File.world_writable?("/tmp")
1965  * sprintf("%o", m) #=> "777"
1966  */
1967 
1968 static VALUE
1969 rb_file_world_writable_p(VALUE obj, VALUE fname)
1970 {
1971 #ifdef S_IWOTH
1972  struct stat st;
1973 
1974  if (rb_stat(fname, &st) < 0) return Qnil;
1975  if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
1976  return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
1977  }
1978 #endif
1979  return Qnil;
1980 }
1981 
1982 /*
1983  * call-seq:
1984  * File.executable?(file_name) -> true or false
1985  *
1986  * Returns <code>true</code> if the named file is executable by the effective
1987  * user and group id of this process. See eaccess(3).
1988  *
1989  * Windows does not support execute permissions separately from read
1990  * permissions. On Windows, a file is only considered executable if it ends in
1991  * .bat, .cmd, .com, or .exe.
1992  *
1993  * Note that some OS-level security features may cause this to return true
1994  * even though the file is not executable by the effective user/group.
1995  */
1996 
1997 static VALUE
1998 rb_file_executable_p(VALUE obj, VALUE fname)
1999 {
2000  return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2001 }
2002 
2003 /*
2004  * call-seq:
2005  * File.executable_real?(file_name) -> true or false
2006  *
2007  * Returns <code>true</code> if the named file is executable by the real
2008  * user and group id of this process. See access(3).
2009  *
2010  * Windows does not support execute permissions separately from read
2011  * permissions. On Windows, a file is only considered executable if it ends in
2012  * .bat, .cmd, .com, or .exe.
2013  *
2014  * Note that some OS-level security features may cause this to return true
2015  * even though the file is not executable by the real user/group.
2016  */
2017 
2018 static VALUE
2019 rb_file_executable_real_p(VALUE obj, VALUE fname)
2020 {
2021  return RBOOL(rb_access(fname, X_OK) >= 0);
2022 }
2023 
2024 #ifndef S_ISREG
2025 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2026 #endif
2027 
2028 /*
2029  * call-seq:
2030  * File.file?(file) -> true or false
2031  *
2032  * Returns +true+ if the named +file+ exists and is a regular file.
2033  *
2034  * +file+ can be an IO object.
2035  *
2036  * If the +file+ argument is a symbolic link, it will resolve the symbolic link
2037  * and use the file referenced by the link.
2038  */
2039 
2040 static VALUE
2041 rb_file_file_p(VALUE obj, VALUE fname)
2042 {
2043  struct stat st;
2044 
2045  if (rb_stat(fname, &st) < 0) return Qfalse;
2046  return RBOOL(S_ISREG(st.st_mode));
2047 }
2048 
2049 /*
2050  * call-seq:
2051  * File.zero?(file_name) -> true or false
2052  *
2053  * Returns <code>true</code> if the named file exists and has
2054  * a zero size.
2055  *
2056  * _file_name_ can be an IO object.
2057  */
2058 
2059 static VALUE
2060 rb_file_zero_p(VALUE obj, VALUE fname)
2061 {
2062  struct stat st;
2063 
2064  if (rb_stat(fname, &st) < 0) return Qfalse;
2065  return RBOOL(st.st_size == 0);
2066 }
2067 
2068 /*
2069  * call-seq:
2070  * File.size?(file_name) -> Integer or nil
2071  *
2072  * Returns +nil+ if +file_name+ doesn't exist or has zero size, the size of the
2073  * file otherwise.
2074  *
2075  * _file_name_ can be an IO object.
2076  */
2077 
2078 static VALUE
2079 rb_file_size_p(VALUE obj, VALUE fname)
2080 {
2081  struct stat st;
2082 
2083  if (rb_stat(fname, &st) < 0) return Qnil;
2084  if (st.st_size == 0) return Qnil;
2085  return OFFT2NUM(st.st_size);
2086 }
2087 
2088 /*
2089  * call-seq:
2090  * File.owned?(file_name) -> true or false
2091  *
2092  * Returns <code>true</code> if the named file exists and the
2093  * effective used id of the calling process is the owner of
2094  * the file.
2095  *
2096  * _file_name_ can be an IO object.
2097  */
2098 
2099 static VALUE
2100 rb_file_owned_p(VALUE obj, VALUE fname)
2101 {
2102  struct stat st;
2103 
2104  if (rb_stat(fname, &st) < 0) return Qfalse;
2105  return RBOOL(st.st_uid == geteuid());
2106 }
2107 
2108 static VALUE
2109 rb_file_rowned_p(VALUE obj, VALUE fname)
2110 {
2111  struct stat st;
2112 
2113  if (rb_stat(fname, &st) < 0) return Qfalse;
2114  return RBOOL(st.st_uid == getuid());
2115 }
2116 
2117 /*
2118  * call-seq:
2119  * File.grpowned?(file_name) -> true or false
2120  *
2121  * Returns <code>true</code> if the named file exists and the
2122  * effective group id of the calling process is the owner of
2123  * the file. Returns <code>false</code> on Windows.
2124  *
2125  * _file_name_ can be an IO object.
2126  */
2127 
2128 static VALUE
2129 rb_file_grpowned_p(VALUE obj, VALUE fname)
2130 {
2131 #ifndef _WIN32
2132  struct stat st;
2133 
2134  if (rb_stat(fname, &st) < 0) return Qfalse;
2135  if (rb_group_member(st.st_gid)) return Qtrue;
2136 #endif
2137  return Qfalse;
2138 }
2139 
2140 #if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2141 static VALUE
2142 check3rdbyte(VALUE fname, int mode)
2143 {
2144  struct stat st;
2145 
2146  if (rb_stat(fname, &st) < 0) return Qfalse;
2147  return RBOOL(st.st_mode & mode);
2148 }
2149 #endif
2150 
2151 /*
2152  * call-seq:
2153  * File.setuid?(file_name) -> true or false
2154  *
2155  * Returns <code>true</code> if the named file has the setuid bit set.
2156  *
2157  * _file_name_ can be an IO object.
2158  */
2159 
2160 static VALUE
2161 rb_file_suid_p(VALUE obj, VALUE fname)
2162 {
2163 #ifdef S_ISUID
2164  return check3rdbyte(fname, S_ISUID);
2165 #else
2166  return Qfalse;
2167 #endif
2168 }
2169 
2170 /*
2171  * call-seq:
2172  * File.setgid?(file_name) -> true or false
2173  *
2174  * Returns <code>true</code> if the named file has the setgid bit set.
2175  *
2176  * _file_name_ can be an IO object.
2177  */
2178 
2179 static VALUE
2180 rb_file_sgid_p(VALUE obj, VALUE fname)
2181 {
2182 #ifdef S_ISGID
2183  return check3rdbyte(fname, S_ISGID);
2184 #else
2185  return Qfalse;
2186 #endif
2187 }
2188 
2189 /*
2190  * call-seq:
2191  * File.sticky?(file_name) -> true or false
2192  *
2193  * Returns <code>true</code> if the named file has the sticky bit set.
2194  *
2195  * _file_name_ can be an IO object.
2196  */
2197 
2198 static VALUE
2199 rb_file_sticky_p(VALUE obj, VALUE fname)
2200 {
2201 #ifdef S_ISVTX
2202  return check3rdbyte(fname, S_ISVTX);
2203 #else
2204  return Qfalse;
2205 #endif
2206 }
2207 
2208 /*
2209  * call-seq:
2210  * File.identical?(file_1, file_2) -> true or false
2211  *
2212  * Returns <code>true</code> if the named files are identical.
2213  *
2214  * _file_1_ and _file_2_ can be an IO object.
2215  *
2216  * open("a", "w") {}
2217  * p File.identical?("a", "a") #=> true
2218  * p File.identical?("a", "./a") #=> true
2219  * File.link("a", "b")
2220  * p File.identical?("a", "b") #=> true
2221  * File.symlink("a", "c")
2222  * p File.identical?("a", "c") #=> true
2223  * open("d", "w") {}
2224  * p File.identical?("a", "d") #=> false
2225  */
2226 
2227 static VALUE
2228 rb_file_identical_p(VALUE obj, VALUE fname1, VALUE fname2)
2229 {
2230 #ifndef _WIN32
2231  struct stat st1, st2;
2232 
2233  if (rb_stat(fname1, &st1) < 0) return Qfalse;
2234  if (rb_stat(fname2, &st2) < 0) return Qfalse;
2235  if (st1.st_dev != st2.st_dev) return Qfalse;
2236  if (st1.st_ino != st2.st_ino) return Qfalse;
2237  return Qtrue;
2238 #else
2239  extern VALUE rb_w32_file_identical_p(VALUE, VALUE);
2240  return rb_w32_file_identical_p(fname1, fname2);
2241 #endif
2242 }
2243 
2244 /*
2245  * call-seq:
2246  * File.size(file_name) -> integer
2247  *
2248  * Returns the size of <code>file_name</code>.
2249  *
2250  * _file_name_ can be an IO object.
2251  */
2252 
2253 static VALUE
2254 rb_file_s_size(VALUE klass, VALUE fname)
2255 {
2256  struct stat st;
2257 
2258  if (rb_stat(fname, &st) < 0) {
2259  int e = errno;
2260  FilePathValue(fname);
2261  rb_syserr_fail_path(e, fname);
2262  }
2263  return OFFT2NUM(st.st_size);
2264 }
2265 
2266 static VALUE
2267 rb_file_ftype(const struct stat *st)
2268 {
2269  const char *t;
2270 
2271  if (S_ISREG(st->st_mode)) {
2272  t = "file";
2273  }
2274  else if (S_ISDIR(st->st_mode)) {
2275  t = "directory";
2276  }
2277  else if (S_ISCHR(st->st_mode)) {
2278  t = "characterSpecial";
2279  }
2280 #ifdef S_ISBLK
2281  else if (S_ISBLK(st->st_mode)) {
2282  t = "blockSpecial";
2283  }
2284 #endif
2285 #ifdef S_ISFIFO
2286  else if (S_ISFIFO(st->st_mode)) {
2287  t = "fifo";
2288  }
2289 #endif
2290 #ifdef S_ISLNK
2291  else if (S_ISLNK(st->st_mode)) {
2292  t = "link";
2293  }
2294 #endif
2295 #ifdef S_ISSOCK
2296  else if (S_ISSOCK(st->st_mode)) {
2297  t = "socket";
2298  }
2299 #endif
2300  else {
2301  t = "unknown";
2302  }
2303 
2304  return rb_usascii_str_new2(t);
2305 }
2306 
2307 /*
2308  * call-seq:
2309  * File.ftype(file_name) -> string
2310  *
2311  * Identifies the type of the named file; the return string is one of
2312  * ``<code>file</code>'', ``<code>directory</code>'',
2313  * ``<code>characterSpecial</code>'', ``<code>blockSpecial</code>'',
2314  * ``<code>fifo</code>'', ``<code>link</code>'',
2315  * ``<code>socket</code>'', or ``<code>unknown</code>''.
2316  *
2317  * File.ftype("testfile") #=> "file"
2318  * File.ftype("/dev/tty") #=> "characterSpecial"
2319  * File.ftype("/tmp/.X11-unix/X0") #=> "socket"
2320  */
2321 
2322 static VALUE
2323 rb_file_s_ftype(VALUE klass, VALUE fname)
2324 {
2325  struct stat st;
2326 
2327  FilePathValue(fname);
2328  fname = rb_str_encode_ospath(fname);
2329  if (lstat_without_gvl(StringValueCStr(fname), &st) == -1) {
2330  rb_sys_fail_path(fname);
2331  }
2332 
2333  return rb_file_ftype(&st);
2334 }
2335 
2336 /*
2337  * call-seq:
2338  * File.atime(file_name) -> time
2339  *
2340  * Returns the last access time for the named file as a Time object.
2341  *
2342  * _file_name_ can be an IO object.
2343  *
2344  * File.atime("testfile") #=> Wed Apr 09 08:51:48 CDT 2003
2345  *
2346  */
2347 
2348 static VALUE
2349 rb_file_s_atime(VALUE klass, VALUE fname)
2350 {
2351  struct stat st;
2352 
2353  if (rb_stat(fname, &st) < 0) {
2354  int e = errno;
2355  FilePathValue(fname);
2356  rb_syserr_fail_path(e, fname);
2357  }
2358  return stat_atime(&st);
2359 }
2360 
2361 /*
2362  * call-seq:
2363  * file.atime -> time
2364  *
2365  * Returns the last access time (a Time object) for <i>file</i>, or
2366  * epoch if <i>file</i> has not been accessed.
2367  *
2368  * File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
2369  *
2370  */
2371 
2372 static VALUE
2373 rb_file_atime(VALUE obj)
2374 {
2375  rb_io_t *fptr;
2376  struct stat st;
2377 
2378  GetOpenFile(obj, fptr);
2379  if (fstat(fptr->fd, &st) == -1) {
2380  rb_sys_fail_path(fptr->pathv);
2381  }
2382  return stat_atime(&st);
2383 }
2384 
2385 /*
2386  * call-seq:
2387  * File.mtime(file_name) -> time
2388  *
2389  * Returns the modification time for the named file as a Time object.
2390  *
2391  * _file_name_ can be an IO object.
2392  *
2393  * File.mtime("testfile") #=> Tue Apr 08 12:58:04 CDT 2003
2394  *
2395  */
2396 
2397 static VALUE
2398 rb_file_s_mtime(VALUE klass, VALUE fname)
2399 {
2400  struct stat st;
2401 
2402  if (rb_stat(fname, &st) < 0) {
2403  int e = errno;
2404  FilePathValue(fname);
2405  rb_syserr_fail_path(e, fname);
2406  }
2407  return stat_mtime(&st);
2408 }
2409 
2410 /*
2411  * call-seq:
2412  * file.mtime -> time
2413  *
2414  * Returns the modification time for <i>file</i>.
2415  *
2416  * File.new("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
2417  *
2418  */
2419 
2420 static VALUE
2421 rb_file_mtime(VALUE obj)
2422 {
2423  rb_io_t *fptr;
2424  struct stat st;
2425 
2426  GetOpenFile(obj, fptr);
2427  if (fstat(fptr->fd, &st) == -1) {
2428  rb_sys_fail_path(fptr->pathv);
2429  }
2430  return stat_mtime(&st);
2431 }
2432 
2433 /*
2434  * call-seq:
2435  * File.ctime(file_name) -> time
2436  *
2437  * Returns the change time for the named file (the time at which
2438  * directory information about the file was changed, not the file
2439  * itself).
2440  *
2441  * _file_name_ can be an IO object.
2442  *
2443  * Note that on Windows (NTFS), returns creation time (birth time).
2444  *
2445  * File.ctime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
2446  *
2447  */
2448 
2449 static VALUE
2450 rb_file_s_ctime(VALUE klass, VALUE fname)
2451 {
2452  struct stat st;
2453 
2454  if (rb_stat(fname, &st) < 0) {
2455  int e = errno;
2456  FilePathValue(fname);
2457  rb_syserr_fail_path(e, fname);
2458  }
2459  return stat_ctime(&st);
2460 }
2461 
2462 /*
2463  * call-seq:
2464  * file.ctime -> time
2465  *
2466  * Returns the change time for <i>file</i> (that is, the time directory
2467  * information about the file was changed, not the file itself).
2468  *
2469  * Note that on Windows (NTFS), returns creation time (birth time).
2470  *
2471  * File.new("testfile").ctime #=> Wed Apr 09 08:53:14 CDT 2003
2472  *
2473  */
2474 
2475 static VALUE
2476 rb_file_ctime(VALUE obj)
2477 {
2478  rb_io_t *fptr;
2479  struct stat st;
2480 
2481  GetOpenFile(obj, fptr);
2482  if (fstat(fptr->fd, &st) == -1) {
2483  rb_sys_fail_path(fptr->pathv);
2484  }
2485  return stat_ctime(&st);
2486 }
2487 
2488 /*
2489  * call-seq:
2490  * File.birthtime(file_name) -> time
2491  *
2492  * Returns the birth time for the named file.
2493  *
2494  * _file_name_ can be an IO object.
2495  *
2496  * File.birthtime("testfile") #=> Wed Apr 09 08:53:13 CDT 2003
2497  *
2498  * If the platform doesn't have birthtime, raises NotImplementedError.
2499  *
2500  */
2501 
2502 #if defined(HAVE_STAT_BIRTHTIME)
2503 RUBY_FUNC_EXPORTED VALUE
2504 rb_file_s_birthtime(VALUE klass, VALUE fname)
2505 {
2506  statx_data st;
2507 
2508  if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2509  int e = errno;
2510  FilePathValue(fname);
2511  rb_syserr_fail_path(e, fname);
2512  }
2513  return statx_birthtime(&st, fname);
2514 }
2515 #else
2516 # define rb_file_s_birthtime rb_f_notimplement
2517 #endif
2518 
2519 #if defined(HAVE_STAT_BIRTHTIME)
2520 /*
2521  * call-seq:
2522  * file.birthtime -> time
2523  *
2524  * Returns the birth time for <i>file</i>.
2525  *
2526  * File.new("testfile").birthtime #=> Wed Apr 09 08:53:14 CDT 2003
2527  *
2528  * If the platform doesn't have birthtime, raises NotImplementedError.
2529  *
2530  */
2531 
2532 static VALUE
2533 rb_file_birthtime(VALUE obj)
2534 {
2535  rb_io_t *fptr;
2536  statx_data st;
2537 
2538  GetOpenFile(obj, fptr);
2539  if (fstatx_without_gvl(fptr->fd, &st, STATX_BTIME) == -1) {
2540  rb_sys_fail_path(fptr->pathv);
2541  }
2542  return statx_birthtime(&st, fptr->pathv);
2543 }
2544 #else
2545 # define rb_file_birthtime rb_f_notimplement
2546 #endif
2547 
2548 /*
2549  * call-seq:
2550  * file.size -> integer
2551  *
2552  * Returns the size of <i>file</i> in bytes.
2553  *
2554  * File.new("testfile").size #=> 66
2555  *
2556  */
2557 
2558 off_t
2560 {
2561  if (RB_TYPE_P(file, T_FILE)) {
2562  rb_io_t *fptr;
2563  struct stat st;
2564 
2565  RB_IO_POINTER(file, fptr);
2566  if (fptr->mode & FMODE_WRITABLE) {
2567  rb_io_flush_raw(file, 0);
2568  }
2569 
2570  if (fstat(fptr->fd, &st) == -1) {
2571  rb_sys_fail_path(fptr->pathv);
2572  }
2573 
2574  return st.st_size;
2575  }
2576  else {
2577  return NUM2OFFT(rb_funcall(file, idSize, 0));
2578  }
2579 }
2580 
2581 static VALUE
2582 file_size(VALUE self)
2583 {
2584  return OFFT2NUM(rb_file_size(self));
2585 }
2586 
2587 static int
2588 chmod_internal(const char *path, void *mode)
2589 {
2590  return chmod(path, *(mode_t *)mode);
2591 }
2592 
2593 /*
2594  * call-seq:
2595  * File.chmod(mode_int, file_name, ... ) -> integer
2596  *
2597  * Changes permission bits on the named file(s) to the bit pattern
2598  * represented by <i>mode_int</i>. Actual effects are operating system
2599  * dependent (see the beginning of this section). On Unix systems, see
2600  * <code>chmod(2)</code> for details. Returns the number of files
2601  * processed.
2602  *
2603  * File.chmod(0644, "testfile", "out") #=> 2
2604  */
2605 
2606 static VALUE
2607 rb_file_s_chmod(int argc, VALUE *argv, VALUE _)
2608 {
2609  mode_t mode;
2610 
2611  apply2args(1);
2612  mode = NUM2MODET(*argv++);
2613 
2614  return apply2files(chmod_internal, argc, argv, &mode);
2615 }
2616 
2617 /*
2618  * call-seq:
2619  * file.chmod(mode_int) -> 0
2620  *
2621  * Changes permission bits on <i>file</i> to the bit pattern
2622  * represented by <i>mode_int</i>. Actual effects are platform
2623  * dependent; on Unix systems, see <code>chmod(2)</code> for details.
2624  * Follows symbolic links. Also see File#lchmod.
2625  *
2626  * f = File.new("out", "w");
2627  * f.chmod(0644) #=> 0
2628  */
2629 
2630 static VALUE
2631 rb_file_chmod(VALUE obj, VALUE vmode)
2632 {
2633  rb_io_t *fptr;
2634  mode_t mode;
2635 #if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2636  VALUE path;
2637 #endif
2638 
2639  mode = NUM2MODET(vmode);
2640 
2641  GetOpenFile(obj, fptr);
2642 #ifdef HAVE_FCHMOD
2643  if (fchmod(fptr->fd, mode) == -1) {
2644  if (HAVE_FCHMOD || errno != ENOSYS)
2645  rb_sys_fail_path(fptr->pathv);
2646  }
2647  else {
2648  if (!HAVE_FCHMOD) return INT2FIX(0);
2649  }
2650 #endif
2651 #if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2652  if (NIL_P(fptr->pathv)) return Qnil;
2653  path = rb_str_encode_ospath(fptr->pathv);
2654  if (chmod(RSTRING_PTR(path), mode) == -1)
2655  rb_sys_fail_path(fptr->pathv);
2656 #endif
2657 
2658  return INT2FIX(0);
2659 }
2660 
2661 #if defined(HAVE_LCHMOD)
2662 static int
2663 lchmod_internal(const char *path, void *mode)
2664 {
2665  return lchmod(path, *(mode_t *)mode);
2666 }
2667 
2668 /*
2669  * call-seq:
2670  * File.lchmod(mode_int, file_name, ...) -> integer
2671  *
2672  * Equivalent to File::chmod, but does not follow symbolic links (so
2673  * it will change the permissions associated with the link, not the
2674  * file referenced by the link). Often not available.
2675  *
2676  */
2677 
2678 static VALUE
2679 rb_file_s_lchmod(int argc, VALUE *argv, VALUE _)
2680 {
2681  mode_t mode;
2682 
2683  apply2args(1);
2684  mode = NUM2MODET(*argv++);
2685 
2686  return apply2files(lchmod_internal, argc, argv, &mode);
2687 }
2688 #else
2689 #define rb_file_s_lchmod rb_f_notimplement
2690 #endif
2691 
2692 static inline rb_uid_t
2693 to_uid(VALUE u)
2694 {
2695  if (NIL_P(u)) {
2696  return (rb_uid_t)-1;
2697  }
2698  return NUM2UIDT(u);
2699 }
2700 
2701 static inline rb_gid_t
2702 to_gid(VALUE g)
2703 {
2704  if (NIL_P(g)) {
2705  return (rb_gid_t)-1;
2706  }
2707  return NUM2GIDT(g);
2708 }
2709 
2710 struct chown_args {
2711  rb_uid_t owner;
2712  rb_gid_t group;
2713 };
2714 
2715 static int
2716 chown_internal(const char *path, void *arg)
2717 {
2718  struct chown_args *args = arg;
2719  return chown(path, args->owner, args->group);
2720 }
2721 
2722 /*
2723  * call-seq:
2724  * File.chown(owner_int, group_int, file_name, ...) -> integer
2725  *
2726  * Changes the owner and group of the named file(s) to the given
2727  * numeric owner and group id's. Only a process with superuser
2728  * privileges may change the owner of a file. The current owner of a
2729  * file may change the file's group to any group to which the owner
2730  * belongs. A <code>nil</code> or -1 owner or group id is ignored.
2731  * Returns the number of files processed.
2732  *
2733  * File.chown(nil, 100, "testfile")
2734  *
2735  */
2736 
2737 static VALUE
2738 rb_file_s_chown(int argc, VALUE *argv, VALUE _)
2739 {
2740  struct chown_args arg;
2741 
2742  apply2args(2);
2743  arg.owner = to_uid(*argv++);
2744  arg.group = to_gid(*argv++);
2745 
2746  return apply2files(chown_internal, argc, argv, &arg);
2747 }
2748 
2749 /*
2750  * call-seq:
2751  * file.chown(owner_int, group_int ) -> 0
2752  *
2753  * Changes the owner and group of <i>file</i> to the given numeric
2754  * owner and group id's. Only a process with superuser privileges may
2755  * change the owner of a file. The current owner of a file may change
2756  * the file's group to any group to which the owner belongs. A
2757  * <code>nil</code> or -1 owner or group id is ignored. Follows
2758  * symbolic links. See also File#lchown.
2759  *
2760  * File.new("testfile").chown(502, 1000)
2761  *
2762  */
2763 
2764 static VALUE
2765 rb_file_chown(VALUE obj, VALUE owner, VALUE group)
2766 {
2767  rb_io_t *fptr;
2768  rb_uid_t o;
2769  rb_gid_t g;
2770 #ifndef HAVE_FCHOWN
2771  VALUE path;
2772 #endif
2773 
2774  o = to_uid(owner);
2775  g = to_gid(group);
2776  GetOpenFile(obj, fptr);
2777 #ifndef HAVE_FCHOWN
2778  if (NIL_P(fptr->pathv)) return Qnil;
2779  path = rb_str_encode_ospath(fptr->pathv);
2780  if (chown(RSTRING_PTR(path), o, g) == -1)
2781  rb_sys_fail_path(fptr->pathv);
2782 #else
2783  if (fchown(fptr->fd, o, g) == -1)
2784  rb_sys_fail_path(fptr->pathv);
2785 #endif
2786 
2787  return INT2FIX(0);
2788 }
2789 
2790 #if defined(HAVE_LCHOWN)
2791 static int
2792 lchown_internal(const char *path, void *arg)
2793 {
2794  struct chown_args *args = arg;
2795  return lchown(path, args->owner, args->group);
2796 }
2797 
2798 /*
2799  * call-seq:
2800  * File.lchown(owner_int, group_int, file_name,..) -> integer
2801  *
2802  * Equivalent to File::chown, but does not follow symbolic
2803  * links (so it will change the owner associated with the link, not the
2804  * file referenced by the link). Often not available. Returns number
2805  * of files in the argument list.
2806  *
2807  */
2808 
2809 static VALUE
2810 rb_file_s_lchown(int argc, VALUE *argv, VALUE _)
2811 {
2812  struct chown_args arg;
2813 
2814  apply2args(2);
2815  arg.owner = to_uid(*argv++);
2816  arg.group = to_gid(*argv++);
2817 
2818  return apply2files(lchown_internal, argc, argv, &arg);
2819 }
2820 #else
2821 #define rb_file_s_lchown rb_f_notimplement
2822 #endif
2823 
2824 struct utime_args {
2825  const struct timespec* tsp;
2826  VALUE atime, mtime;
2827  int follow; /* Whether to act on symlinks (1) or their referent (0) */
2828 };
2829 
2830 #ifdef UTIME_EINVAL
2831 NORETURN(static void utime_failed(struct apply_arg *));
2832 
2833 static void
2834 utime_failed(struct apply_arg *aa)
2835 {
2836  int e = aa->errnum;
2837  VALUE path = aa->fn[aa->i].path;
2838  struct utime_args *ua = aa->arg;
2839 
2840  if (ua->tsp && e == EINVAL) {
2841  VALUE e[2], a = Qnil, m = Qnil;
2842  int d = 0;
2843  VALUE atime = ua->atime;
2844  VALUE mtime = ua->mtime;
2845 
2846  if (!NIL_P(atime)) {
2847  a = rb_inspect(atime);
2848  }
2849  if (!NIL_P(mtime) && mtime != atime && !rb_equal(atime, mtime)) {
2850  m = rb_inspect(mtime);
2851  }
2852  if (NIL_P(a)) e[0] = m;
2853  else if (NIL_P(m) || rb_str_cmp(a, m) == 0) e[0] = a;
2854  else {
2855  e[0] = rb_str_plus(a, rb_str_new_cstr(" or "));
2856  rb_str_append(e[0], m);
2857  d = 1;
2858  }
2859  if (!NIL_P(e[0])) {
2860  if (path) {
2861  if (!d) e[0] = rb_str_dup(e[0]);
2862  rb_str_append(rb_str_cat2(e[0], " for "), path);
2863  }
2864  e[1] = INT2FIX(EINVAL);
2866  }
2867  }
2868  rb_syserr_fail_path(e, path);
2869 }
2870 #endif
2871 
2872 #if defined(HAVE_UTIMES)
2873 
2874 static int
2875 utime_internal(const char *path, void *arg)
2876 {
2877  struct utime_args *v = arg;
2878  const struct timespec *tsp = v->tsp;
2879  struct timeval tvbuf[2], *tvp = NULL;
2880 
2881 #if defined(HAVE_UTIMENSAT)
2882  static int try_utimensat = 1;
2883 # ifdef AT_SYMLINK_NOFOLLOW
2884  static int try_utimensat_follow = 1;
2885 # else
2886  const int try_utimensat_follow = 0;
2887 # endif
2888  int flags = 0;
2889 
2890  if (v->follow ? try_utimensat_follow : try_utimensat) {
2891 # ifdef AT_SYMLINK_NOFOLLOW
2892  if (v->follow) {
2893  flags = AT_SYMLINK_NOFOLLOW;
2894  }
2895 # endif
2896 
2897  if (utimensat(AT_FDCWD, path, tsp, flags) < 0) {
2898  if (errno == ENOSYS) {
2899 # ifdef AT_SYMLINK_NOFOLLOW
2900  try_utimensat_follow = 0;
2901 # endif
2902  if (!v->follow)
2903  try_utimensat = 0;
2904  goto no_utimensat;
2905  }
2906  return -1; /* calls utime_failed */
2907  }
2908  return 0;
2909  }
2910 no_utimensat:
2911 #endif
2912 
2913  if (tsp) {
2914  tvbuf[0].tv_sec = tsp[0].tv_sec;
2915  tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
2916  tvbuf[1].tv_sec = tsp[1].tv_sec;
2917  tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
2918  tvp = tvbuf;
2919  }
2920 #ifdef HAVE_LUTIMES
2921  if (v->follow) return lutimes(path, tvp);
2922 #endif
2923  return utimes(path, tvp);
2924 }
2925 
2926 #else
2927 
2928 #if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
2929 struct utimbuf {
2930  long actime;
2931  long modtime;
2932 };
2933 #endif
2934 
2935 static int
2936 utime_internal(const char *path, void *arg)
2937 {
2938  struct utime_args *v = arg;
2939  const struct timespec *tsp = v->tsp;
2940  struct utimbuf utbuf, *utp = NULL;
2941  if (tsp) {
2942  utbuf.actime = tsp[0].tv_sec;
2943  utbuf.modtime = tsp[1].tv_sec;
2944  utp = &utbuf;
2945  }
2946  return utime(path, utp);
2947 }
2948 
2949 #endif
2950 
2951 static VALUE
2952 utime_internal_i(int argc, VALUE *argv, int follow)
2953 {
2954  struct utime_args args;
2955  struct timespec tss[2], *tsp = NULL;
2956 
2957  apply2args(2);
2958  args.atime = *argv++;
2959  args.mtime = *argv++;
2960 
2961  args.follow = follow;
2962 
2963  if (!NIL_P(args.atime) || !NIL_P(args.mtime)) {
2964  tsp = tss;
2965  tsp[0] = rb_time_timespec(args.atime);
2966  if (args.atime == args.mtime)
2967  tsp[1] = tsp[0];
2968  else
2969  tsp[1] = rb_time_timespec(args.mtime);
2970  }
2971  args.tsp = tsp;
2972 
2973  return apply2files(utime_internal, argc, argv, &args);
2974 }
2975 
2976 /*
2977  * call-seq:
2978  * File.utime(atime, mtime, file_name, ...) -> integer
2979  *
2980  * Sets the access and modification times of each named file to the
2981  * first two arguments. If a file is a symlink, this method acts upon
2982  * its referent rather than the link itself; for the inverse
2983  * behavior see File.lutime. Returns the number of file
2984  * names in the argument list.
2985  */
2986 
2987 static VALUE
2988 rb_file_s_utime(int argc, VALUE *argv, VALUE _)
2989 {
2990  return utime_internal_i(argc, argv, FALSE);
2991 }
2992 
2993 #if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
2994 
2995 /*
2996  * call-seq:
2997  * File.lutime(atime, mtime, file_name, ...) -> integer
2998  *
2999  * Sets the access and modification times of each named file to the
3000  * first two arguments. If a file is a symlink, this method acts upon
3001  * the link itself as opposed to its referent; for the inverse
3002  * behavior, see File.utime. Returns the number of file
3003  * names in the argument list.
3004  */
3005 
3006 static VALUE
3007 rb_file_s_lutime(int argc, VALUE *argv, VALUE _)
3008 {
3009  return utime_internal_i(argc, argv, TRUE);
3010 }
3011 #else
3012 #define rb_file_s_lutime rb_f_notimplement
3013 #endif
3014 
3015 #ifdef RUBY_FUNCTION_NAME_STRING
3016 # define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3017 #else
3018 # define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3019 #endif
3020 #define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3021 NORETURN(static void syserr_fail2_in(const char *,int,VALUE,VALUE));
3022 static void
3023 syserr_fail2_in(const char *func, int e, VALUE s1, VALUE s2)
3024 {
3025  VALUE str;
3026 #ifdef MAX_PATH
3027  const int max_pathlen = MAX_PATH;
3028 #else
3029  const int max_pathlen = MAXPATHLEN;
3030 #endif
3031 
3032  if (e == EEXIST) {
3033  rb_syserr_fail_path(e, rb_str_ellipsize(s2, max_pathlen));
3034  }
3035  str = rb_str_new_cstr("(");
3036  rb_str_append(str, rb_str_ellipsize(s1, max_pathlen));
3037  rb_str_cat2(str, ", ");
3038  rb_str_append(str, rb_str_ellipsize(s2, max_pathlen));
3039  rb_str_cat2(str, ")");
3040 #ifdef RUBY_FUNCTION_NAME_STRING
3041  rb_syserr_fail_path_in(func, e, str);
3042 #else
3043  rb_syserr_fail_path(e, str);
3044 #endif
3045 }
3046 
3047 #ifdef HAVE_LINK
3048 /*
3049  * call-seq:
3050  * File.link(old_name, new_name) -> 0
3051  *
3052  * Creates a new name for an existing file using a hard link. Will not
3053  * overwrite <i>new_name</i> if it already exists (raising a subclass
3054  * of SystemCallError). Not available on all platforms.
3055  *
3056  * File.link("testfile", ".testfile") #=> 0
3057  * IO.readlines(".testfile")[0] #=> "This is line one\n"
3058  */
3059 
3060 static VALUE
3061 rb_file_s_link(VALUE klass, VALUE from, VALUE to)
3062 {
3063  FilePathValue(from);
3064  FilePathValue(to);
3065  from = rb_str_encode_ospath(from);
3066  to = rb_str_encode_ospath(to);
3067 
3068  if (link(StringValueCStr(from), StringValueCStr(to)) < 0) {
3069  sys_fail2(from, to);
3070  }
3071  return INT2FIX(0);
3072 }
3073 #else
3074 #define rb_file_s_link rb_f_notimplement
3075 #endif
3076 
3077 #ifdef HAVE_SYMLINK
3078 /*
3079  * call-seq:
3080  * File.symlink(old_name, new_name) -> 0
3081  *
3082  * Creates a symbolic link called <i>new_name</i> for the existing file
3083  * <i>old_name</i>. Raises a NotImplemented exception on
3084  * platforms that do not support symbolic links.
3085  *
3086  * File.symlink("testfile", "link2test") #=> 0
3087  *
3088  */
3089 
3090 static VALUE
3091 rb_file_s_symlink(VALUE klass, VALUE from, VALUE to)
3092 {
3093  FilePathValue(from);
3094  FilePathValue(to);
3095  from = rb_str_encode_ospath(from);
3096  to = rb_str_encode_ospath(to);
3097 
3098  if (symlink(StringValueCStr(from), StringValueCStr(to)) < 0) {
3099  sys_fail2(from, to);
3100  }
3101  return INT2FIX(0);
3102 }
3103 #else
3104 #define rb_file_s_symlink rb_f_notimplement
3105 #endif
3106 
3107 #ifdef HAVE_READLINK
3108 /*
3109  * call-seq:
3110  * File.readlink(link_name) -> file_name
3111  *
3112  * Returns the name of the file referenced by the given link.
3113  * Not available on all platforms.
3114  *
3115  * File.symlink("testfile", "link2test") #=> 0
3116  * File.readlink("link2test") #=> "testfile"
3117  */
3118 
3119 static VALUE
3120 rb_file_s_readlink(VALUE klass, VALUE path)
3121 {
3122  return rb_readlink(path, rb_filesystem_encoding());
3123 }
3124 
3125 #ifndef _WIN32
3126 struct readlink_arg {
3127  const char *path;
3128  char *buf;
3129  size_t size;
3130 };
3131 
3132 static void *
3133 nogvl_readlink(void *ptr)
3134 {
3135  struct readlink_arg *ra = ptr;
3136 
3137  return (void *)(VALUE)readlink(ra->path, ra->buf, ra->size);
3138 }
3139 
3140 static ssize_t
3141 readlink_without_gvl(VALUE path, VALUE buf, size_t size)
3142 {
3143  struct readlink_arg ra;
3144 
3145  ra.path = RSTRING_PTR(path);
3146  ra.buf = RSTRING_PTR(buf);
3147  ra.size = size;
3148 
3149  return (ssize_t)rb_thread_call_without_gvl(nogvl_readlink, &ra,
3150  RUBY_UBF_IO, 0);
3151 }
3152 
3153 VALUE
3154 rb_readlink(VALUE path, rb_encoding *enc)
3155 {
3156  int size = 100;
3157  ssize_t rv;
3158  VALUE v;
3159 
3160  FilePathValue(path);
3161  path = rb_str_encode_ospath(path);
3162  v = rb_enc_str_new(0, size, enc);
3163  while ((rv = readlink_without_gvl(path, v, size)) == size
3164 #ifdef _AIX
3165  || (rv < 0 && errno == ERANGE) /* quirky behavior of GPFS */
3166 #endif
3167  ) {
3168  rb_str_modify_expand(v, size);
3169  size *= 2;
3170  rb_str_set_len(v, size);
3171  }
3172  if (rv < 0) {
3173  int e = errno;
3174  rb_str_resize(v, 0);
3175  rb_syserr_fail_path(e, path);
3176  }
3177  rb_str_resize(v, rv);
3178 
3179  return v;
3180 }
3181 #endif
3182 #else
3183 #define rb_file_s_readlink rb_f_notimplement
3184 #endif
3185 
3186 static int
3187 unlink_internal(const char *path, void *arg)
3188 {
3189  return unlink(path);
3190 }
3191 
3192 /*
3193  * call-seq:
3194  * File.delete(file_name, ...) -> integer
3195  * File.unlink(file_name, ...) -> integer
3196  *
3197  * Deletes the named files, returning the number of names
3198  * passed as arguments. Raises an exception on any error.
3199  * Since the underlying implementation relies on the
3200  * <code>unlink(2)</code> system call, the type of
3201  * exception raised depends on its error type (see
3202  * https://linux.die.net/man/2/unlink) and has the form of
3203  * e.g. Errno::ENOENT.
3204  *
3205  * See also Dir::rmdir.
3206  */
3207 
3208 static VALUE
3209 rb_file_s_unlink(int argc, VALUE *argv, VALUE klass)
3210 {
3211  return apply2files(unlink_internal, argc, argv, 0);
3212 }
3213 
3214 struct rename_args {
3215  const char *src;
3216  const char *dst;
3217 };
3218 
3219 static void *
3220 no_gvl_rename(void *ptr)
3221 {
3222  struct rename_args *ra = ptr;
3223 
3224  return (void *)(VALUE)rename(ra->src, ra->dst);
3225 }
3226 
3227 /*
3228  * call-seq:
3229  * File.rename(old_name, new_name) -> 0
3230  *
3231  * Renames the given file to the new name. Raises a SystemCallError
3232  * if the file cannot be renamed.
3233  *
3234  * File.rename("afile", "afile.bak") #=> 0
3235  */
3236 
3237 static VALUE
3238 rb_file_s_rename(VALUE klass, VALUE from, VALUE to)
3239 {
3240  struct rename_args ra;
3241  VALUE f, t;
3242 
3243  FilePathValue(from);
3244  FilePathValue(to);
3245  f = rb_str_encode_ospath(from);
3246  t = rb_str_encode_ospath(to);
3247  ra.src = StringValueCStr(f);
3248  ra.dst = StringValueCStr(t);
3249 #if defined __CYGWIN__
3250  errno = 0;
3251 #endif
3252  if ((int)(VALUE)rb_thread_call_without_gvl(no_gvl_rename, &ra,
3253  RUBY_UBF_IO, 0) < 0) {
3254  int e = errno;
3255 #if defined DOSISH
3256  switch (e) {
3257  case EEXIST:
3258  if (chmod(ra.dst, 0666) == 0 &&
3259  unlink(ra.dst) == 0 &&
3260  rename(ra.src, ra.dst) == 0)
3261  return INT2FIX(0);
3262  }
3263 #endif
3264  syserr_fail2(e, from, to);
3265  }
3266 
3267  return INT2FIX(0);
3268 }
3269 
3270 /*
3271  * call-seq:
3272  * File.umask() -> integer
3273  * File.umask(integer) -> integer
3274  *
3275  * Returns the current umask value for this process. If the optional
3276  * argument is given, set the umask to that value and return the
3277  * previous value. Umask values are <em>subtracted</em> from the
3278  * default permissions, so a umask of <code>0222</code> would make a
3279  * file read-only for everyone.
3280  *
3281  * File.umask(0006) #=> 18
3282  * File.umask #=> 6
3283  */
3284 
3285 static VALUE
3286 rb_file_s_umask(int argc, VALUE *argv, VALUE _)
3287 {
3288  mode_t omask = 0;
3289 
3290  switch (argc) {
3291  case 0:
3292  omask = umask(0);
3293  umask(omask);
3294  break;
3295  case 1:
3296  omask = umask(NUM2MODET(argv[0]));
3297  break;
3298  default:
3299  rb_error_arity(argc, 0, 1);
3300  }
3301  return MODET2NUM(omask);
3302 }
3303 
3304 #ifdef __CYGWIN__
3305 #undef DOSISH
3306 #endif
3307 #if defined __CYGWIN__ || defined DOSISH
3308 #define DOSISH_UNC
3309 #define DOSISH_DRIVE_LETTER
3310 #define FILE_ALT_SEPARATOR '\\'
3311 #endif
3312 #ifdef FILE_ALT_SEPARATOR
3313 #define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3314 # ifdef DOSISH
3315 static const char file_alt_separator[] = {FILE_ALT_SEPARATOR, '\0'};
3316 # endif
3317 #else
3318 #define isdirsep(x) ((x) == '/')
3319 #endif
3320 
3321 #ifndef USE_NTFS
3322 #if defined _WIN32
3323 #define USE_NTFS 1
3324 #else
3325 #define USE_NTFS 0
3326 #endif
3327 #endif
3328 #ifndef USE_NTFS_ADS
3329 # if USE_NTFS
3330 # define USE_NTFS_ADS 1
3331 # else
3332 # define USE_NTFS_ADS 0
3333 # endif
3334 #endif
3335 
3336 #if USE_NTFS
3337 #define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3338 #else
3339 #define istrailinggarbage(x) 0
3340 #endif
3341 #if USE_NTFS_ADS
3342 # define isADS(x) ((x) == ':')
3343 #else
3344 # define isADS(x) 0
3345 #endif
3346 
3347 #define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc)))
3348 #define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
3349 
3350 #if defined(DOSISH_UNC)
3351 #define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3352 #else
3353 #define has_unc(buf) 0
3354 #endif
3355 
3356 #ifdef DOSISH_DRIVE_LETTER
3357 static inline int
3358 has_drive_letter(const char *buf)
3359 {
3360  if (ISALPHA(buf[0]) && buf[1] == ':') {
3361  return 1;
3362  }
3363  else {
3364  return 0;
3365  }
3366 }
3367 
3368 #ifndef _WIN32
3369 static char*
3370 getcwdofdrv(int drv)
3371 {
3372  char drive[4];
3373  char *drvcwd, *oldcwd;
3374 
3375  drive[0] = drv;
3376  drive[1] = ':';
3377  drive[2] = '\0';
3378 
3379  /* the only way that I know to get the current directory
3380  of a particular drive is to change chdir() to that drive,
3381  so save the old cwd before chdir()
3382  */
3383  oldcwd = ruby_getcwd();
3384  if (chdir(drive) == 0) {
3385  drvcwd = ruby_getcwd();
3386  chdir(oldcwd);
3387  xfree(oldcwd);
3388  }
3389  else {
3390  /* perhaps the drive is not exist. we return only drive letter */
3391  drvcwd = strdup(drive);
3392  }
3393  return drvcwd;
3394 }
3395 
3396 static inline int
3397 not_same_drive(VALUE path, int drive)
3398 {
3399  const char *p = RSTRING_PTR(path);
3400  if (RSTRING_LEN(path) < 2) return 0;
3401  if (has_drive_letter(p)) {
3402  return TOLOWER(p[0]) != TOLOWER(drive);
3403  }
3404  else {
3405  return has_unc(p);
3406  }
3407 }
3408 #endif
3409 #endif
3410 
3411 static inline char *
3412 skiproot(const char *path, const char *end, rb_encoding *enc)
3413 {
3414 #ifdef DOSISH_DRIVE_LETTER
3415  if (path + 2 <= end && has_drive_letter(path)) path += 2;
3416 #endif
3417  while (path < end && isdirsep(*path)) path++;
3418  return (char *)path;
3419 }
3420 
3421 #define nextdirsep rb_enc_path_next
3422 char *
3423 rb_enc_path_next(const char *s, const char *e, rb_encoding *enc)
3424 {
3425  while (s < e && !isdirsep(*s)) {
3426  Inc(s, e, enc);
3427  }
3428  return (char *)s;
3429 }
3430 
3431 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3432 #define skipprefix rb_enc_path_skip_prefix
3433 #else
3434 #define skipprefix(path, end, enc) (path)
3435 #endif
3436 char *
3437 rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc)
3438 {
3439 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3440 #ifdef DOSISH_UNC
3441  if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3442  path += 2;
3443  while (path < end && isdirsep(*path)) path++;
3444  if ((path = rb_enc_path_next(path, end, enc)) < end && path[0] && path[1] && !isdirsep(path[1]))
3445  path = rb_enc_path_next(path + 1, end, enc);
3446  return (char *)path;
3447  }
3448 #endif
3449 #ifdef DOSISH_DRIVE_LETTER
3450  if (has_drive_letter(path))
3451  return (char *)(path + 2);
3452 #endif
3453 #endif
3454  return (char *)path;
3455 }
3456 
3457 static inline char *
3458 skipprefixroot(const char *path, const char *end, rb_encoding *enc)
3459 {
3460 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3461  char *p = skipprefix(path, end, enc);
3462  while (isdirsep(*p)) p++;
3463  return p;
3464 #else
3465  return skiproot(path, end, enc);
3466 #endif
3467 }
3468 
3469 #define strrdirsep rb_enc_path_last_separator
3470 char *
3471 rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc)
3472 {
3473  char *last = NULL;
3474  while (path < end) {
3475  if (isdirsep(*path)) {
3476  const char *tmp = path++;
3477  while (path < end && isdirsep(*path)) path++;
3478  if (path >= end) break;
3479  last = (char *)tmp;
3480  }
3481  else {
3482  Inc(path, end, enc);
3483  }
3484  }
3485  return last;
3486 }
3487 
3488 static char *
3489 chompdirsep(const char *path, const char *end, rb_encoding *enc)
3490 {
3491  while (path < end) {
3492  if (isdirsep(*path)) {
3493  const char *last = path++;
3494  while (path < end && isdirsep(*path)) path++;
3495  if (path >= end) return (char *)last;
3496  }
3497  else {
3498  Inc(path, end, enc);
3499  }
3500  }
3501  return (char *)path;
3502 }
3503 
3504 char *
3505 rb_enc_path_end(const char *path, const char *end, rb_encoding *enc)
3506 {
3507  if (path < end && isdirsep(*path)) path++;
3508  return chompdirsep(path, end, enc);
3509 }
3510 
3511 static rb_encoding *
3512 fs_enc_check(VALUE path1, VALUE path2)
3513 {
3514  rb_encoding *enc = rb_enc_check(path1, path2);
3515  int encidx = rb_enc_to_index(enc);
3516  if (encidx == ENCINDEX_US_ASCII) {
3517  encidx = rb_enc_get_index(path1);
3518  if (encidx == ENCINDEX_US_ASCII)
3519  encidx = rb_enc_get_index(path2);
3520  enc = rb_enc_from_index(encidx);
3521  }
3522  return enc;
3523 }
3524 
3525 #if USE_NTFS
3526 static char *
3527 ntfs_tail(const char *path, const char *end, rb_encoding *enc)
3528 {
3529  while (path < end && *path == '.') path++;
3530  while (path < end && !isADS(*path)) {
3531  if (istrailinggarbage(*path)) {
3532  const char *last = path++;
3533  while (path < end && istrailinggarbage(*path)) path++;
3534  if (path >= end || isADS(*path)) return (char *)last;
3535  }
3536  else if (isdirsep(*path)) {
3537  const char *last = path++;
3538  while (path < end && isdirsep(*path)) path++;
3539  if (path >= end) return (char *)last;
3540  if (isADS(*path)) path++;
3541  }
3542  else {
3543  Inc(path, end, enc);
3544  }
3545  }
3546  return (char *)path;
3547 }
3548 #endif
3549 
3550 #define BUFCHECK(cond) do {\
3551  bdiff = p - buf;\
3552  if (cond) {\
3553  do {buflen *= 2;} while (cond);\
3554  rb_str_resize(result, buflen);\
3555  buf = RSTRING_PTR(result);\
3556  p = buf + bdiff;\
3557  pend = buf + buflen;\
3558  }\
3559 } while (0)
3560 
3561 #define BUFINIT() (\
3562  p = buf = RSTRING_PTR(result),\
3563  buflen = RSTRING_LEN(result),\
3564  pend = p + buflen)
3565 
3566 #ifdef __APPLE__
3567 # define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
3568 #else
3569 # define SKIPPATHSEP(p) 1
3570 #endif
3571 
3572 #define BUFCOPY(srcptr, srclen) do { \
3573  const int skip = SKIPPATHSEP(p); \
3574  rb_str_set_len(result, p-buf+skip); \
3575  BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
3576  p += skip; \
3577  memcpy(p, (srcptr), (srclen)); \
3578  p += (srclen); \
3579 } while (0)
3580 
3581 #define WITH_ROOTDIFF(stmt) do { \
3582  long rootdiff = root - buf; \
3583  stmt; \
3584  root = buf + rootdiff; \
3585 } while (0)
3586 
3587 static VALUE
3588 copy_home_path(VALUE result, const char *dir)
3589 {
3590  char *buf;
3591 #if defined DOSISH || defined __CYGWIN__
3592  char *p, *bend;
3593  rb_encoding *enc;
3594 #endif
3595  long dirlen;
3596  int encidx;
3597 
3598  dirlen = strlen(dir);
3599  rb_str_resize(result, dirlen);
3600  memcpy(buf = RSTRING_PTR(result), dir, dirlen);
3601  encidx = rb_filesystem_encindex();
3602  rb_enc_associate_index(result, encidx);
3603 #if defined DOSISH || defined __CYGWIN__
3604  enc = rb_enc_from_index(encidx);
3605  for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, enc)) {
3606  if (*p == '\\') {
3607  *p = '/';
3608  }
3609  }
3610 #endif
3611  return result;
3612 }
3613 
3614 VALUE
3615 rb_home_dir_of(VALUE user, VALUE result)
3616 {
3617 #ifdef HAVE_PWD_H
3618  struct passwd *pwPtr;
3619 #else
3620  extern char *getlogin(void);
3621  const char *pwPtr = 0;
3622  # define endpwent() ((void)0)
3623 #endif
3624  const char *dir, *username = RSTRING_PTR(user);
3625  rb_encoding *enc = rb_enc_get(user);
3626 #if defined _WIN32
3627  rb_encoding *fsenc = rb_utf8_encoding();
3628 #else
3630 #endif
3631  if (enc != fsenc) {
3632  dir = username = RSTRING_PTR(rb_str_conv_enc(user, enc, fsenc));
3633  }
3634 
3635 #ifdef HAVE_PWD_H
3636  pwPtr = getpwnam(username);
3637 #else
3638  if (strcasecmp(username, getlogin()) == 0)
3639  dir = pwPtr = getenv("HOME");
3640 #endif
3641  if (!pwPtr) {
3642  endpwent();
3643  rb_raise(rb_eArgError, "user %"PRIsVALUE" doesn't exist", user);
3644  }
3645 #ifdef HAVE_PWD_H
3646  dir = pwPtr->pw_dir;
3647 #endif
3648  copy_home_path(result, dir);
3649  endpwent();
3650  return result;
3651 }
3652 
3653 #ifndef _WIN32
3654 VALUE
3655 rb_default_home_dir(VALUE result)
3656 {
3657  const char *dir = getenv("HOME");
3658 
3659 #if defined HAVE_PWD_H
3660  if (!dir) {
3661  /* We'll look up the user's default home dir in the password db by
3662  * login name, if possible, and failing that will fall back to looking
3663  * the information up by uid (as would be needed for processes that
3664  * are not a descendant of login(1) or a work-alike).
3665  *
3666  * While the lookup by uid is more likely to succeed (since we always
3667  * have a uid, but may or may not have a login name), we prefer first
3668  * looking up by name to accommodate the possibility of multiple login
3669  * names (each with its own record in the password database, so each
3670  * with a potentially different home directory) being mapped to the
3671  * same uid (as explicitly allowed for by POSIX; see getlogin(3posix)).
3672  */
3673  VALUE login_name = rb_getlogin();
3674 
3675 # if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
3676  /* This is a corner case, but for backward compatibility reasons we
3677  * want to emit this error if neither the lookup by login name nor
3678  * lookup by getuid() has a chance of succeeding.
3679  */
3680  if (NIL_P(login_name)) {
3681  rb_raise(rb_eArgError, "couldn't find login name -- expanding `~'");
3682  }
3683 # endif
3684 
3685  VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
3686  if (NIL_P(pw_dir)) {
3687  pw_dir = rb_getpwdiruid();
3688  if (NIL_P(pw_dir)) {
3689  rb_raise(rb_eArgError, "couldn't find home for uid `%ld'", (long)getuid());
3690  }
3691  }
3692 
3693  /* found it */
3694  copy_home_path(result, RSTRING_PTR(pw_dir));
3695  rb_str_resize(pw_dir, 0);
3696  return result;
3697  }
3698 #endif
3699  if (!dir) {
3700  rb_raise(rb_eArgError, "couldn't find HOME environment -- expanding `~'");
3701  }
3702  return copy_home_path(result, dir);
3703 }
3704 
3705 static VALUE
3706 ospath_new(const char *ptr, long len, rb_encoding *fsenc)
3707 {
3708 #if NORMALIZE_UTF8PATH
3709  VALUE path = rb_str_normalize_ospath(ptr, len);
3710  rb_enc_associate(path, fsenc);
3711  return path;
3712 #else
3713  return rb_enc_str_new(ptr, len, fsenc);
3714 #endif
3715 }
3716 
3717 static char *
3718 append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc)
3719 {
3720  char *buf, *cwdp = dir;
3721  VALUE dirname = Qnil;
3722  size_t dirlen = strlen(dir), buflen = rb_str_capacity(result);
3723 
3724  if (NORMALIZE_UTF8PATH || *enc != fsenc) {
3725  rb_encoding *direnc = fs_enc_check(fname, dirname = ospath_new(dir, dirlen, fsenc));
3726  if (direnc != fsenc) {
3727  dirname = rb_str_conv_enc(dirname, fsenc, direnc);
3728  RSTRING_GETMEM(dirname, cwdp, dirlen);
3729  }
3730  else if (NORMALIZE_UTF8PATH) {
3731  RSTRING_GETMEM(dirname, cwdp, dirlen);
3732  }
3733  *enc = direnc;
3734  }
3735  do {buflen *= 2;} while (dirlen > buflen);
3736  rb_str_resize(result, buflen);
3737  buf = RSTRING_PTR(result);
3738  memcpy(buf, cwdp, dirlen);
3739  xfree(dir);
3740  if (!NIL_P(dirname)) rb_str_resize(dirname, 0);
3741  rb_enc_associate(result, *enc);
3742  return buf + dirlen;
3743 }
3744 
3745 VALUE
3746 rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
3747 {
3748  const char *s, *b, *fend;
3749  char *buf, *p, *pend, *root;
3750  size_t buflen, bdiff;
3751  rb_encoding *enc, *fsenc = rb_filesystem_encoding();
3752 
3753  s = StringValuePtr(fname);
3754  fend = s + RSTRING_LEN(fname);
3755  enc = rb_enc_get(fname);
3756  BUFINIT();
3757 
3758  if (s[0] == '~' && abs_mode == 0) { /* execute only if NOT absolute_path() */
3759  long userlen = 0;
3760  if (isdirsep(s[1]) || s[1] == '\0') {
3761  buf = 0;
3762  b = 0;
3763  rb_str_set_len(result, 0);
3764  if (*++s) ++s;
3765  rb_default_home_dir(result);
3766  }
3767  else {
3768  s = nextdirsep(b = s, fend, enc);
3769  b++; /* b[0] is '~' */
3770  userlen = s - b;
3771  BUFCHECK(bdiff + userlen >= buflen);
3772  memcpy(p, b, userlen);
3773  ENC_CODERANGE_CLEAR(result);
3774  rb_str_set_len(result, userlen);
3775  rb_enc_associate(result, enc);
3776  rb_home_dir_of(result, result);
3777  buf = p + 1;
3778  p += userlen;
3779  }
3780  if (!rb_is_absolute_path(RSTRING_PTR(result))) {
3781  if (userlen) {
3782  rb_enc_raise(enc, rb_eArgError, "non-absolute home of %.*s%.0"PRIsVALUE,
3783  (int)userlen, b, fname);
3784  }
3785  else {
3786  rb_raise(rb_eArgError, "non-absolute home");
3787  }
3788  }
3789  BUFINIT();
3790  p = pend;
3791  }
3792 #ifdef DOSISH_DRIVE_LETTER
3793  /* skip drive letter */
3794  else if (has_drive_letter(s)) {
3795  if (isdirsep(s[2])) {
3796  /* specified drive letter, and full path */
3797  /* skip drive letter */
3798  BUFCHECK(bdiff + 2 >= buflen);
3799  memcpy(p, s, 2);
3800  p += 2;
3801  s += 2;
3802  rb_enc_copy(result, fname);
3803  }
3804  else {
3805  /* specified drive, but not full path */
3806  int same = 0;
3807  if (!NIL_P(dname) && !not_same_drive(dname, s[0])) {
3808  rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result);
3809  BUFINIT();
3810  if (has_drive_letter(p) && TOLOWER(p[0]) == TOLOWER(s[0])) {
3811  /* ok, same drive */
3812  same = 1;
3813  }
3814  }
3815  if (!same) {
3816  char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
3817  BUFINIT();
3818  p = e;
3819  }
3820  else {
3821  rb_enc_associate(result, enc = fs_enc_check(result, fname));
3822  p = pend;
3823  }
3824  p = chompdirsep(skiproot(buf, p, enc), p, enc);
3825  s += 2;
3826  }
3827  }
3828 #endif
3829  else if (!rb_is_absolute_path(s)) {
3830  if (!NIL_P(dname)) {
3831  rb_file_expand_path_internal(dname, Qnil, abs_mode, long_name, result);
3832  rb_enc_associate(result, fs_enc_check(result, fname));
3833  BUFINIT();
3834  p = pend;
3835  }
3836  else {
3837  char *e = append_fspath(result, fname, ruby_getcwd(), &enc, fsenc);
3838  BUFINIT();
3839  p = e;
3840  }
3841 #if defined DOSISH || defined __CYGWIN__
3842  if (isdirsep(*s)) {
3843  /* specified full path, but not drive letter nor UNC */
3844  /* we need to get the drive letter or UNC share name */
3845  p = skipprefix(buf, p, enc);
3846  }
3847  else
3848 #endif
3849  p = chompdirsep(skiproot(buf, p, enc), p, enc);
3850  }
3851  else {
3852  size_t len;
3853  b = s;
3854  do s++; while (isdirsep(*s));
3855  len = s - b;
3856  p = buf + len;
3857  BUFCHECK(bdiff >= buflen);
3858  memset(buf, '/', len);
3859  rb_str_set_len(result, len);
3860  rb_enc_associate(result, fs_enc_check(result, fname));
3861  }
3862  if (p > buf && p[-1] == '/')
3863  --p;
3864  else {
3865  rb_str_set_len(result, p-buf);
3866  BUFCHECK(bdiff + 1 >= buflen);
3867  *p = '/';
3868  }
3869 
3870  rb_str_set_len(result, p-buf+1);
3871  BUFCHECK(bdiff + 1 >= buflen);
3872  p[1] = 0;
3873  root = skipprefix(buf, p+1, enc);
3874 
3875  b = s;
3876  while (*s) {
3877  switch (*s) {
3878  case '.':
3879  if (b == s++) { /* beginning of path element */
3880  switch (*s) {
3881  case '\0':
3882  b = s;
3883  break;
3884  case '.':
3885  if (*(s+1) == '\0' || isdirsep(*(s+1))) {
3886  /* We must go back to the parent */
3887  char *n;
3888  *p = '\0';
3889  if (!(n = strrdirsep(root, p, enc))) {
3890  *p = '/';
3891  }
3892  else {
3893  p = n;
3894  }
3895  b = ++s;
3896  }
3897 #if USE_NTFS
3898  else {
3899  do ++s; while (istrailinggarbage(*s));
3900  }
3901 #endif
3902  break;
3903  case '/':
3904 #if defined DOSISH || defined __CYGWIN__
3905  case '\\':
3906 #endif
3907  b = ++s;
3908  break;
3909  default:
3910  /* ordinary path element, beginning don't move */
3911  break;
3912  }
3913  }
3914 #if USE_NTFS
3915  else {
3916  --s;
3917  case ' ': {
3918  const char *e = s;
3919  while (s < fend && istrailinggarbage(*s)) s++;
3920  if (s >= fend) {
3921  s = e;
3922  goto endpath;
3923  }
3924  }
3925  }
3926 #endif
3927  break;
3928  case '/':
3929 #if defined DOSISH || defined __CYGWIN__
3930  case '\\':
3931 #endif
3932  if (s > b) {
3933  WITH_ROOTDIFF(BUFCOPY(b, s-b));
3934  *p = '/';
3935  }
3936  b = ++s;
3937  break;
3938  default:
3939 #ifdef __APPLE__
3940  {
3941  int n = ignored_char_p(s, fend, enc);
3942  if (n) {
3943  if (s > b) {
3944  WITH_ROOTDIFF(BUFCOPY(b, s-b));
3945  *p = '\0';
3946  }
3947  b = s += n;
3948  break;
3949  }
3950  }
3951 #endif
3952  Inc(s, fend, enc);
3953  break;
3954  }
3955  }
3956 
3957  if (s > b) {
3958 #if USE_NTFS
3959 # if USE_NTFS_ADS
3960  static const char prime[] = ":$DATA";
3961  enum {prime_len = sizeof(prime) -1};
3962 # endif
3963  endpath:
3964 # if USE_NTFS_ADS
3965  if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
3966  /* alias of stream */
3967  /* get rid of a bug of x64 VC++ */
3968  if (isADS(*(s - (prime_len+1)))) {
3969  s -= prime_len + 1; /* prime */
3970  }
3971  else if (memchr(b, ':', s - prime_len - b)) {
3972  s -= prime_len; /* alternative */
3973  }
3974  }
3975 # endif
3976 #endif
3977  BUFCOPY(b, s-b);
3978  rb_str_set_len(result, p-buf);
3979  }
3980  if (p == skiproot(buf, p + !!*p, enc) - 1) p++;
3981 
3982 #if USE_NTFS
3983  *p = '\0';
3984  if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s, "*?")) {
3985  VALUE tmp, v;
3986  size_t len;
3987  int encidx;
3988  WCHAR *wstr;
3989  WIN32_FIND_DATAW wfd;
3990  HANDLE h;
3991 #ifdef __CYGWIN__
3992 #ifdef HAVE_CYGWIN_CONV_PATH
3993  char *w32buf = NULL;
3994  const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
3995 #else
3996  char w32buf[MAXPATHLEN];
3997 #endif
3998  const char *path;
3999  ssize_t bufsize;
4000  int lnk_added = 0, is_symlink = 0;
4001  struct stat st;
4002  p = (char *)s;
4003  len = strlen(p);
4004  if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4005  is_symlink = 1;
4006  if (len > 4 && STRCASECMP(p + len - 4, ".lnk") != 0) {
4007  lnk_added = 1;
4008  }
4009  }
4010  path = *buf ? buf : "/";
4011 #ifdef HAVE_CYGWIN_CONV_PATH
4012  bufsize = cygwin_conv_path(flags, path, NULL, 0);
4013  if (bufsize > 0) {
4014  bufsize += len;
4015  if (lnk_added) bufsize += 4;
4016  w32buf = ALLOCA_N(char, bufsize);
4017  if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4018  b = w32buf;
4019  }
4020  }
4021 #else
4022  bufsize = MAXPATHLEN;
4023  if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4024  b = w32buf;
4025  }
4026 #endif
4027  if (is_symlink && b == w32buf) {
4028  *p = '\\';
4029  strlcat(w32buf, p, bufsize);
4030  if (lnk_added) {
4031  strlcat(w32buf, ".lnk", bufsize);
4032  }
4033  }
4034  else {
4035  lnk_added = 0;
4036  }
4037  *p = '/';
4038 #endif
4039  rb_str_set_len(result, p - buf + strlen(p));
4040  encidx = ENCODING_GET(result);
4041  tmp = result;
4042  if (encidx != ENCINDEX_UTF_8 && rb_enc_str_coderange(result) != ENC_CODERANGE_7BIT) {
4043  tmp = rb_str_encode_ospath(result);
4044  }
4045  len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4046  wstr = ALLOCV_N(WCHAR, v, len);
4047  MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr, len);
4048  if (tmp != result) rb_str_set_len(tmp, 0);
4049  h = FindFirstFileW(wstr, &wfd);
4050  ALLOCV_END(v);
4051  if (h != INVALID_HANDLE_VALUE) {
4052  size_t wlen;
4053  FindClose(h);
4054  len = lstrlenW(wfd.cFileName);
4055 #ifdef __CYGWIN__
4056  if (lnk_added && len > 4 &&
4057  wcscasecmp(wfd.cFileName + len - 4, L".lnk") == 0) {
4058  wfd.cFileName[len -= 4] = L'\0';
4059  }
4060 #else
4061  p = (char *)s;
4062 #endif
4063  ++p;
4064  wlen = (int)len;
4065  len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4066  if (tmp == result) {
4067  BUFCHECK(bdiff + len >= buflen);
4068  WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p, len + 1, NULL, NULL);
4069  }
4070  else {
4071  rb_str_modify_expand(tmp, len);
4072  WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp), len + 1, NULL, NULL);
4073  rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp), len,
4074  rb_utf8_encoding(), 0, Qnil);
4075  BUFINIT();
4076  rb_str_resize(tmp, 0);
4077  }
4078  p += len;
4079  }
4080 #ifdef __CYGWIN__
4081  else {
4082  p += strlen(p);
4083  }
4084 #endif
4085  }
4086 #endif
4087 
4088  rb_str_set_len(result, p - buf);
4089  rb_enc_check(fname, result);
4090  ENC_CODERANGE_CLEAR(result);
4091  return result;
4092 }
4093 #endif /* _WIN32 */
4094 
4095 #define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, MAXPATHLEN + 2)
4096 
4097 static VALUE
4098 str_shrink(VALUE str)
4099 {
4100  rb_str_resize(str, RSTRING_LEN(str));
4101  return str;
4102 }
4103 
4104 #define expand_path(fname, dname, abs_mode, long_name, result) \
4105  str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4106 
4107 #define check_expand_path_args(fname, dname) \
4108  (((fname) = rb_get_path(fname)), \
4109  (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4110 
4111 static VALUE
4112 file_expand_path_1(VALUE fname)
4113 {
4114  return rb_file_expand_path_internal(fname, Qnil, 0, 0, EXPAND_PATH_BUFFER());
4115 }
4116 
4117 VALUE
4119 {
4120  check_expand_path_args(fname, dname);
4121  return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
4122 }
4123 
4124 VALUE
4125 rb_file_expand_path_fast(VALUE fname, VALUE dname)
4126 {
4127  return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
4128 }
4129 
4130 VALUE
4131 rb_file_s_expand_path(int argc, const VALUE *argv)
4132 {
4133  rb_check_arity(argc, 1, 2);
4134  return rb_file_expand_path(argv[0], argc > 1 ? argv[1] : Qnil);
4135 }
4136 
4137 /*
4138  * call-seq:
4139  * File.expand_path(file_name [, dir_string] ) -> abs_file_name
4140  *
4141  * Converts a pathname to an absolute pathname. Relative paths are
4142  * referenced from the current working directory of the process unless
4143  * +dir_string+ is given, in which case it will be used as the
4144  * starting point. The given pathname may start with a
4145  * ``<code>~</code>'', which expands to the process owner's home
4146  * directory (the environment variable +HOME+ must be set
4147  * correctly). ``<code>~</code><i>user</i>'' expands to the named
4148  * user's home directory.
4149  *
4150  * File.expand_path("~oracle/bin") #=> "/home/oracle/bin"
4151  *
4152  * A simple example of using +dir_string+ is as follows.
4153  * File.expand_path("ruby", "/usr/bin") #=> "/usr/bin/ruby"
4154  *
4155  * A more complex example which also resolves parent directory is as follows.
4156  * Suppose we are in bin/mygem and want the absolute path of lib/mygem.rb.
4157  *
4158  * File.expand_path("../../lib/mygem.rb", __FILE__)
4159  * #=> ".../path/to/project/lib/mygem.rb"
4160  *
4161  * So first it resolves the parent of __FILE__, that is bin/, then go to the
4162  * parent, the root of the project and appends +lib/mygem.rb+.
4163  */
4164 
4165 static VALUE
4166 s_expand_path(int c, const VALUE * v, VALUE _)
4167 {
4168  return rb_file_s_expand_path(c, v);
4169 }
4170 
4171 VALUE
4173 {
4174  check_expand_path_args(fname, dname);
4175  return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
4176 }
4177 
4178 VALUE
4179 rb_file_s_absolute_path(int argc, const VALUE *argv)
4180 {
4181  rb_check_arity(argc, 1, 2);
4182  return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] : Qnil);
4183 }
4184 
4185 /*
4186  * call-seq:
4187  * File.absolute_path(file_name [, dir_string] ) -> abs_file_name
4188  *
4189  * Converts a pathname to an absolute pathname. Relative paths are
4190  * referenced from the current working directory of the process unless
4191  * <i>dir_string</i> is given, in which case it will be used as the
4192  * starting point. If the given pathname starts with a ``<code>~</code>''
4193  * it is NOT expanded, it is treated as a normal directory name.
4194  *
4195  * File.absolute_path("~oracle/bin") #=> "<relative_path>/~oracle/bin"
4196  */
4197 
4198 static VALUE
4199 s_absolute_path(int c, const VALUE * v, VALUE _)
4200 {
4201  return rb_file_s_absolute_path(c, v);
4202 }
4203 
4204 /*
4205  * call-seq:
4206  * File.absolute_path?(file_name) -> true or false
4207  *
4208  * Returns <code>true</code> if +file_name+ is an absolute path, and
4209  * <code>false</code> otherwise.
4210  *
4211  * File.absolute_path?("c:/foo") #=> false (on Linux), true (on Windows)
4212  */
4213 
4214 static VALUE
4215 s_absolute_path_p(VALUE klass, VALUE fname)
4216 {
4217  VALUE path = rb_get_path(fname);
4218 
4219  if (!rb_is_absolute_path(RSTRING_PTR(path))) return Qfalse;
4220  return Qtrue;
4221 }
4222 
4223 enum rb_realpath_mode {
4224  RB_REALPATH_CHECK,
4225  RB_REALPATH_DIR,
4226  RB_REALPATH_STRICT,
4227  RB_REALPATH_MODE_MAX
4228 };
4229 
4230 static int
4231 realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE fallback,
4232  VALUE loopcheck, enum rb_realpath_mode mode, int last)
4233 {
4234  const char *pend = unresolved + strlen(unresolved);
4235  rb_encoding *enc = rb_enc_get(*resolvedp);
4236  ID resolving;
4237  CONST_ID(resolving, "resolving");
4238  while (unresolved < pend) {
4239  const char *testname = unresolved;
4240  const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4241  long testnamelen = unresolved_firstsep - unresolved;
4242  const char *unresolved_nextname = unresolved_firstsep;
4243  while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4244  unresolved_nextname++;
4245  unresolved = unresolved_nextname;
4246  if (testnamelen == 1 && testname[0] == '.') {
4247  }
4248  else if (testnamelen == 2 && testname[0] == '.' && testname[1] == '.') {
4249  if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4250  const char *resolved_str = RSTRING_PTR(*resolvedp);
4251  const char *resolved_names = resolved_str + *prefixlenp;
4252  const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), enc);
4253  long len = lastsep ? lastsep - resolved_names : 0;
4254  rb_str_resize(*resolvedp, *prefixlenp + len);
4255  }
4256  }
4257  else {
4258  VALUE checkval;
4259  VALUE testpath = rb_str_dup(*resolvedp);
4260  if (*prefixlenp < RSTRING_LEN(testpath))
4261  rb_str_cat2(testpath, "/");
4262 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4263  if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4264  const char *prefix = RSTRING_PTR(testpath);
4265  const char *last = rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4266  if (!isdirsep(*last)) rb_str_cat2(testpath, "/");
4267  }
4268 #endif
4269  rb_str_cat(testpath, testname, testnamelen);
4270  checkval = rb_hash_aref(loopcheck, testpath);
4271  if (!NIL_P(checkval)) {
4272  if (checkval == ID2SYM(resolving)) {
4273  if (mode == RB_REALPATH_CHECK) {
4274  errno = ELOOP;
4275  return -1;
4276  }
4277  rb_syserr_fail_path(ELOOP, testpath);
4278  }
4279  else {
4280  *resolvedp = rb_str_dup(checkval);
4281  }
4282  }
4283  else {
4284  struct stat sbuf;
4285  int ret;
4286  ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4287  if (ret == -1) {
4288  int e = errno;
4289  if (e == ENOENT && !NIL_P(fallback)) {
4290  if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4291  rb_str_replace(*resolvedp, fallback);
4292  return 0;
4293  }
4294  }
4295  if (mode == RB_REALPATH_CHECK) return -1;
4296  if (e == ENOENT) {
4297  if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4298  rb_syserr_fail_path(e, testpath);
4299  *resolvedp = testpath;
4300  break;
4301  }
4302  else {
4303  rb_syserr_fail_path(e, testpath);
4304  }
4305  }
4306 #ifdef HAVE_READLINK
4307  if (S_ISLNK(sbuf.st_mode)) {
4308  VALUE link;
4309  VALUE link_orig = Qnil;
4310  const char *link_prefix, *link_names;
4311  long link_prefixlen;
4312  rb_hash_aset(loopcheck, testpath, ID2SYM(resolving));
4313  link = rb_readlink(testpath, enc);
4314  link_prefix = RSTRING_PTR(link);
4315  link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4316  link_prefixlen = link_names - link_prefix;
4317  if (link_prefixlen > 0) {
4318  rb_encoding *tmpenc, *linkenc = rb_enc_get(link);
4319  link_orig = link;
4320  link = rb_str_subseq(link, 0, link_prefixlen);
4321  tmpenc = fs_enc_check(*resolvedp, link);
4322  if (tmpenc != linkenc) link = rb_str_conv_enc(link, linkenc, tmpenc);
4323  *resolvedp = link;
4324  *prefixlenp = link_prefixlen;
4325  }
4326  if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4327  loopcheck, mode, !*unresolved_firstsep))
4328  return -1;
4329  RB_GC_GUARD(link_orig);
4330  rb_hash_aset(loopcheck, testpath, rb_str_dup_frozen(*resolvedp));
4331  }
4332  else
4333 #endif
4334  {
4335  VALUE s = rb_str_dup_frozen(testpath);
4336  rb_hash_aset(loopcheck, s, s);
4337  *resolvedp = testpath;
4338  }
4339  }
4340  }
4341  }
4342  return 0;
4343 }
4344 
4345 static VALUE
4346 rb_check_realpath_emulate(VALUE basedir, VALUE path, rb_encoding *origenc, enum rb_realpath_mode mode)
4347 {
4348  long prefixlen;
4349  VALUE resolved;
4350  VALUE unresolved_path;
4351  VALUE loopcheck;
4352  VALUE curdir = Qnil;
4353 
4354  rb_encoding *enc;
4355  char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4356  char *ptr, *prefixptr = NULL, *pend;
4357  long len;
4358 
4359  unresolved_path = rb_str_dup_frozen(path);
4360 
4361  if (!NIL_P(basedir)) {
4362  FilePathValue(basedir);
4363  basedir = TO_OSPATH(rb_str_dup_frozen(basedir));
4364  }
4365 
4366  enc = rb_enc_get(unresolved_path);
4367  unresolved_path = TO_OSPATH(unresolved_path);
4368  RSTRING_GETMEM(unresolved_path, ptr, len);
4369  path_names = skipprefixroot(ptr, ptr + len, rb_enc_get(unresolved_path));
4370  if (ptr != path_names) {
4371  resolved = rb_str_subseq(unresolved_path, 0, path_names - ptr);
4372  goto root_found;
4373  }
4374 
4375  if (!NIL_P(basedir)) {
4376  RSTRING_GETMEM(basedir, ptr, len);
4377  basedir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(basedir));
4378  if (ptr != basedir_names) {
4379  resolved = rb_str_subseq(basedir, 0, basedir_names - ptr);
4380  goto root_found;
4381  }
4382  }
4383 
4384  curdir = rb_dir_getwd_ospath();
4385  RSTRING_GETMEM(curdir, ptr, len);
4386  curdir_names = skipprefixroot(ptr, ptr + len, rb_enc_get(curdir));
4387  resolved = rb_str_subseq(curdir, 0, curdir_names - ptr);
4388 
4389  root_found:
4390  RSTRING_GETMEM(resolved, prefixptr, prefixlen);
4391  pend = prefixptr + prefixlen;
4392  ptr = chompdirsep(prefixptr, pend, enc);
4393  if (ptr < pend) {
4394  prefixlen = ++ptr - prefixptr;
4395  rb_str_set_len(resolved, prefixlen);
4396  }
4397 #ifdef FILE_ALT_SEPARATOR
4398  while (prefixptr < ptr) {
4399  if (*prefixptr == FILE_ALT_SEPARATOR) {
4400  *prefixptr = '/';
4401  }
4402  Inc(prefixptr, pend, enc);
4403  }
4404 #endif
4405 
4406  switch (rb_enc_to_index(enc)) {
4407  case ENCINDEX_ASCII:
4408  case ENCINDEX_US_ASCII:
4410  }
4411 
4412  loopcheck = rb_hash_new();
4413  if (curdir_names) {
4414  if (realpath_rec(&prefixlen, &resolved, curdir_names, Qnil, loopcheck, mode, 0))
4415  return Qnil;
4416  }
4417  if (basedir_names) {
4418  if (realpath_rec(&prefixlen, &resolved, basedir_names, Qnil, loopcheck, mode, 0))
4419  return Qnil;
4420  }
4421  if (realpath_rec(&prefixlen, &resolved, path_names, Qnil, loopcheck, mode, 1))
4422  return Qnil;
4423 
4424  if (origenc && origenc != rb_enc_get(resolved)) {
4425  if (rb_enc_str_asciionly_p(resolved)) {
4426  rb_enc_associate(resolved, origenc);
4427  }
4428  else {
4429  resolved = rb_str_conv_enc(resolved, NULL, origenc);
4430  }
4431  }
4432 
4433  RB_GC_GUARD(unresolved_path);
4434  RB_GC_GUARD(curdir);
4435  return resolved;
4436 }
4437 
4438 static VALUE rb_file_join(VALUE ary);
4439 
4440 #ifndef HAVE_REALPATH
4441 static VALUE
4442 rb_check_realpath_emulate_try(VALUE arg)
4443 {
4444  VALUE *args = (VALUE *)arg;
4445  return rb_check_realpath_emulate(args[0], args[1], (rb_encoding *)args[2], RB_REALPATH_CHECK);
4446 }
4447 
4448 static VALUE
4449 rb_check_realpath_emulate_rescue(VALUE arg, VALUE exc)
4450 {
4451  return Qnil;
4452 }
4453 #endif /* HAVE_REALPATH */
4454 
4455 static VALUE
4456 rb_check_realpath_internal(VALUE basedir, VALUE path, rb_encoding *origenc, enum rb_realpath_mode mode)
4457 {
4458 #ifdef HAVE_REALPATH
4459  VALUE unresolved_path;
4460  char *resolved_ptr = NULL;
4461  VALUE resolved;
4462 
4463  if (mode == RB_REALPATH_DIR) {
4464  return rb_check_realpath_emulate(basedir, path, origenc, mode);
4465  }
4466 
4467  unresolved_path = rb_str_dup_frozen(path);
4468  if (*RSTRING_PTR(unresolved_path) != '/' && !NIL_P(basedir)) {
4469  unresolved_path = rb_file_join(rb_assoc_new(basedir, unresolved_path));
4470  }
4471  if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
4472 
4473  if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), NULL)) == NULL) {
4474  /* glibc realpath(3) does not allow /path/to/file.rb/../other_file.rb,
4475  returning ENOTDIR in that case.
4476  glibc realpath(3) can also return ENOENT for paths that exist,
4477  such as /dev/fd/5.
4478  Fallback to the emulated approach in either of those cases. */
4479  if (errno == ENOTDIR ||
4480  (errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
4481  return rb_check_realpath_emulate(basedir, path, origenc, mode);
4482 
4483  }
4484  if (mode == RB_REALPATH_CHECK) {
4485  return Qnil;
4486  }
4487  rb_sys_fail_path(unresolved_path);
4488  }
4489  resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
4490  free(resolved_ptr);
4491 
4492 # if !defined(__LINUX__) && !defined(__APPLE__)
4493  /* As `resolved` is a String in the filesystem encoding, no
4494  * conversion is needed */
4495  struct stat st;
4496  if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
4497  if (mode == RB_REALPATH_CHECK) {
4498  return Qnil;
4499  }
4500  rb_sys_fail_path(unresolved_path);
4501  }
4502 # endif
4503 
4504  if (origenc && origenc != rb_enc_get(resolved)) {
4505  if (!rb_enc_str_asciionly_p(resolved)) {
4506  resolved = rb_str_conv_enc(resolved, NULL, origenc);
4507  }
4508  rb_enc_associate(resolved, origenc);
4509  }
4510 
4511  if (rb_enc_str_coderange(resolved) == ENC_CODERANGE_BROKEN) {
4513  if (rb_enc_str_coderange(resolved) == ENC_CODERANGE_BROKEN) {
4515  }
4516  }
4517 
4518  RB_GC_GUARD(unresolved_path);
4519  return resolved;
4520 #else
4521  if (mode == RB_REALPATH_CHECK) {
4522  VALUE arg[3];
4523  arg[0] = basedir;
4524  arg[1] = path;
4525  arg[2] = (VALUE)origenc;
4526 
4527  return rb_rescue(rb_check_realpath_emulate_try, (VALUE)arg,
4528  rb_check_realpath_emulate_rescue, Qnil);
4529  }
4530  else {
4531  return rb_check_realpath_emulate(basedir, path, origenc, mode);
4532  }
4533 #endif /* HAVE_REALPATH */
4534 }
4535 
4536 VALUE
4537 rb_realpath_internal(VALUE basedir, VALUE path, int strict)
4538 {
4539  const enum rb_realpath_mode mode =
4540  strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
4541  return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
4542 }
4543 
4544 VALUE
4545 rb_check_realpath(VALUE basedir, VALUE path, rb_encoding *enc)
4546 {
4547  return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
4548 }
4549 
4550 /*
4551  * call-seq:
4552  * File.realpath(pathname [, dir_string]) -> real_pathname
4553  *
4554  * Returns the real (absolute) pathname of _pathname_ in the actual
4555  * filesystem not containing symlinks or useless dots.
4556  *
4557  * If _dir_string_ is given, it is used as a base directory
4558  * for interpreting relative pathname instead of the current directory.
4559  *
4560  * All components of the pathname must exist when this method is
4561  * called.
4562  */
4563 static VALUE
4564 rb_file_s_realpath(int argc, VALUE *argv, VALUE klass)
4565 {
4566  VALUE basedir = (rb_check_arity(argc, 1, 2) > 1) ? argv[1] : Qnil;
4567  VALUE path = argv[0];
4568  FilePathValue(path);
4569  return rb_realpath_internal(basedir, path, 1);
4570 }
4571 
4572 /*
4573  * call-seq:
4574  * File.realdirpath(pathname [, dir_string]) -> real_pathname
4575  *
4576  * Returns the real (absolute) pathname of _pathname_ in the actual filesystem.
4577  * The real pathname doesn't contain symlinks or useless dots.
4578  *
4579  * If _dir_string_ is given, it is used as a base directory
4580  * for interpreting relative pathname instead of the current directory.
4581  *
4582  * The last component of the real pathname can be nonexistent.
4583  */
4584 static VALUE
4585 rb_file_s_realdirpath(int argc, VALUE *argv, VALUE klass)
4586 {
4587  VALUE basedir = (rb_check_arity(argc, 1, 2) > 1) ? argv[1] : Qnil;
4588  VALUE path = argv[0];
4589  FilePathValue(path);
4590  return rb_realpath_internal(basedir, path, 0);
4591 }
4592 
4593 static size_t
4594 rmext(const char *p, long l0, long l1, const char *e, long l2, rb_encoding *enc)
4595 {
4596  int len1, len2;
4597  unsigned int c;
4598  const char *s, *last;
4599 
4600  if (!e || !l2) return 0;
4601 
4602  c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
4603  if (rb_enc_ascget(e + len1, e + l2, &len2, enc) == '*' && len1 + len2 == l2) {
4604  if (c == '.') return l0;
4605  s = p;
4606  e = p + l1;
4607  last = e;
4608  while (s < e) {
4609  if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
4610  s += len1;
4611  }
4612  return last - p;
4613  }
4614  if (l1 < l2) return l1;
4615 
4616  s = p+l1-l2;
4617  if (rb_enc_left_char_head(p, s, p+l1, enc) != s) return 0;
4618 #if CASEFOLD_FILESYSTEM
4619 #define fncomp strncasecmp
4620 #else
4621 #define fncomp strncmp
4622 #endif
4623  if (fncomp(s, e, l2) == 0) {
4624  return l1-l2;
4625  }
4626  return 0;
4627 }
4628 
4629 const char *
4630 ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc)
4631 {
4632  const char *p, *q, *e, *end;
4633 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4634  const char *root;
4635 #endif
4636  long f = 0, n = -1;
4637 
4638  end = name + (alllen ? (size_t)*alllen : strlen(name));
4639  name = skipprefix(name, end, enc);
4640 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4641  root = name;
4642 #endif
4643  while (isdirsep(*name))
4644  name++;
4645  if (!*name) {
4646  p = name - 1;
4647  f = 1;
4648 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4649  if (name != root) {
4650  /* has slashes */
4651  }
4652 #ifdef DOSISH_DRIVE_LETTER
4653  else if (*p == ':') {
4654  p++;
4655  f = 0;
4656  }
4657 #endif
4658 #ifdef DOSISH_UNC
4659  else {
4660  p = "/";
4661  }
4662 #endif
4663 #endif
4664  }
4665  else {
4666  if (!(p = strrdirsep(name, end, enc))) {
4667  p = name;
4668  }
4669  else {
4670  while (isdirsep(*p)) p++; /* skip last / */
4671  }
4672 #if USE_NTFS
4673  n = ntfs_tail(p, end, enc) - p;
4674 #else
4675  n = chompdirsep(p, end, enc) - p;
4676 #endif
4677  for (q = p; q - p < n && *q == '.'; q++);
4678  for (e = 0; q - p < n; Inc(q, end, enc)) {
4679  if (*q == '.') e = q;
4680  }
4681  if (e) f = e - p;
4682  else f = n;
4683  }
4684 
4685  if (baselen)
4686  *baselen = f;
4687  if (alllen)
4688  *alllen = n;
4689  return p;
4690 }
4691 
4692 /*
4693  * call-seq:
4694  * File.basename(file_name [, suffix] ) -> base_name
4695  *
4696  * Returns the last component of the filename given in
4697  * <i>file_name</i> (after first stripping trailing separators),
4698  * which can be formed using both File::SEPARATOR and
4699  * File::ALT_SEPARATOR as the separator when File::ALT_SEPARATOR is
4700  * not <code>nil</code>. If <i>suffix</i> is given and present at the
4701  * end of <i>file_name</i>, it is removed. If <i>suffix</i> is ".*",
4702  * any extension will be removed.
4703  *
4704  * File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
4705  * File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"
4706  * File.basename("/home/gumby/work/ruby.rb", ".*") #=> "ruby"
4707  */
4708 
4709 static VALUE
4710 rb_file_s_basename(int argc, VALUE *argv, VALUE _)
4711 {
4712  VALUE fname, fext, basename;
4713  const char *name, *p;
4714  long f, n;
4715  rb_encoding *enc;
4716 
4717  fext = Qnil;
4718  if (rb_check_arity(argc, 1, 2) == 2) {
4719  fext = argv[1];
4720  StringValue(fext);
4721  enc = check_path_encoding(fext);
4722  }
4723  fname = argv[0];
4724  FilePathStringValue(fname);
4725  if (NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
4726  enc = rb_enc_get(fname);
4727  fext = Qnil;
4728  }
4729  if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname)))
4730  return rb_str_new_shared(fname);
4731 
4732  p = ruby_enc_find_basename(name, &f, &n, enc);
4733  if (n >= 0) {
4734  if (NIL_P(fext)) {
4735  f = n;
4736  }
4737  else {
4738  const char *fp;
4739  fp = StringValueCStr(fext);
4740  if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
4741  f = n;
4742  }
4743  RB_GC_GUARD(fext);
4744  }
4745  if (f == RSTRING_LEN(fname)) return rb_str_new_shared(fname);
4746  }
4747 
4748  basename = rb_str_new(p, f);
4749  rb_enc_copy(basename, fname);
4750  return basename;
4751 }
4752 
4753 static VALUE rb_file_dirname_n(VALUE fname, int n);
4754 
4755 /*
4756  * call-seq:
4757  * File.dirname(file_name, level = 1) -> dir_name
4758  *
4759  * Returns all components of the filename given in <i>file_name</i>
4760  * except the last one (after first stripping trailing separators).
4761  * The filename can be formed using both File::SEPARATOR and
4762  * File::ALT_SEPARATOR as the separator when File::ALT_SEPARATOR is
4763  * not <code>nil</code>.
4764  *
4765  * File.dirname("/home/gumby/work/ruby.rb") #=> "/home/gumby/work"
4766  *
4767  * If +level+ is given, removes the last +level+ components, not only
4768  * one.
4769  *
4770  * File.dirname("/home/gumby/work/ruby.rb", 2) #=> "/home/gumby"
4771  * File.dirname("/home/gumby/work/ruby.rb", 4) #=> "/"
4772  */
4773 
4774 static VALUE
4775 rb_file_s_dirname(int argc, VALUE *argv, VALUE klass)
4776 {
4777  int n = 1;
4778  if ((argc = rb_check_arity(argc, 1, 2)) > 1) {
4779  n = NUM2INT(argv[1]);
4780  }
4781  return rb_file_dirname_n(argv[0], n);
4782 }
4783 
4784 VALUE
4786 {
4787  return rb_file_dirname_n(fname, 1);
4788 }
4789 
4790 static VALUE
4791 rb_file_dirname_n(VALUE fname, int n)
4792 {
4793  const char *name, *root, *p, *end;
4794  VALUE dirname;
4795  rb_encoding *enc;
4796  VALUE sepsv = 0;
4797  const char **seps;
4798 
4799  if (n < 0) rb_raise(rb_eArgError, "negative level: %d", n);
4800  FilePathStringValue(fname);
4801  name = StringValueCStr(fname);
4802  end = name + RSTRING_LEN(fname);
4803  enc = rb_enc_get(fname);
4804  root = skiproot(name, end, enc);
4805 #ifdef DOSISH_UNC
4806  if (root > name + 1 && isdirsep(*name))
4807  root = skipprefix(name = root - 2, end, enc);
4808 #else
4809  if (root > name + 1)
4810  name = root - 1;
4811 #endif
4812  if (n > (end - root + 1) / 2) {
4813  p = root;
4814  }
4815  else {
4816  int i;
4817  switch (n) {
4818  case 0:
4819  p = end;
4820  break;
4821  case 1:
4822  if (!(p = strrdirsep(root, end, enc))) p = root;
4823  break;
4824  default:
4825  seps = ALLOCV_N(const char *, sepsv, n);
4826  for (i = 0; i < n; ++i) seps[i] = root;
4827  i = 0;
4828  for (p = root; p < end; ) {
4829  if (isdirsep(*p)) {
4830  const char *tmp = p++;
4831  while (p < end && isdirsep(*p)) p++;
4832  if (p >= end) break;
4833  seps[i++] = tmp;
4834  if (i == n) i = 0;
4835  }
4836  else {
4837  Inc(p, end, enc);
4838  }
4839  }
4840  p = seps[i];
4841  ALLOCV_END(sepsv);
4842  break;
4843  }
4844  }
4845  if (p == name)
4846  return rb_usascii_str_new2(".");
4847 #ifdef DOSISH_DRIVE_LETTER
4848  if (has_drive_letter(name) && isdirsep(*(name + 2))) {
4849  const char *top = skiproot(name + 2, end, enc);
4850  dirname = rb_str_new(name, 3);
4851  rb_str_cat(dirname, top, p - top);
4852  }
4853  else
4854 #endif
4855  dirname = rb_str_new(name, p - name);
4856 #ifdef DOSISH_DRIVE_LETTER
4857  if (has_drive_letter(name) && root == name + 2 && p - name == 2)
4858  rb_str_cat(dirname, ".", 1);
4859 #endif
4860  rb_enc_copy(dirname, fname);
4861  return dirname;
4862 }
4863 
4864 /*
4865  * accept a String, and return the pointer of the extension.
4866  * if len is passed, set the length of extension to it.
4867  * returned pointer is in ``name'' or NULL.
4868  * returns *len
4869  * no dot NULL 0
4870  * dotfile top 0
4871  * end with dot dot 1
4872  * .ext dot len of .ext
4873  * .ext:stream dot len of .ext without :stream (NT only)
4874  *
4875  */
4876 const char *
4877 ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
4878 {
4879  const char *p, *e, *end = name + (len ? *len : (long)strlen(name));
4880 
4881  p = strrdirsep(name, end, enc); /* get the last path component */
4882  if (!p)
4883  p = name;
4884  else
4885  do name = ++p; while (isdirsep(*p));
4886 
4887  e = 0;
4888  while (*p && *p == '.') p++;
4889  while (*p) {
4890  if (*p == '.' || istrailinggarbage(*p)) {
4891 #if USE_NTFS
4892  const char *last = p++, *dot = last;
4893  while (istrailinggarbage(*p)) {
4894  if (*p == '.') dot = p;
4895  p++;
4896  }
4897  if (!*p || isADS(*p)) {
4898  p = last;
4899  break;
4900  }
4901  if (*last == '.' || dot > last) e = dot;
4902  continue;
4903 #else
4904  e = p; /* get the last dot of the last component */
4905 #endif
4906  }
4907 #if USE_NTFS
4908  else if (isADS(*p)) {
4909  break;
4910  }
4911 #endif
4912  else if (isdirsep(*p))
4913  break;
4914  Inc(p, end, enc);
4915  }
4916 
4917  if (len) {
4918  /* no dot, or the only dot is first or end? */
4919  if (!e || e == name)
4920  *len = 0;
4921  else if (e+1 == p)
4922  *len = 1;
4923  else
4924  *len = p - e;
4925  }
4926  return e;
4927 }
4928 
4929 /*
4930  * call-seq:
4931  * File.extname(path) -> string
4932  *
4933  * Returns the extension (the portion of file name in +path+
4934  * starting from the last period).
4935  *
4936  * If +path+ is a dotfile, or starts with a period, then the starting
4937  * dot is not dealt with the start of the extension.
4938  *
4939  * An empty string will also be returned when the period is the last character
4940  * in +path+.
4941  *
4942  * On Windows, trailing dots are truncated.
4943  *
4944  * File.extname("test.rb") #=> ".rb"
4945  * File.extname("a/b/d/test.rb") #=> ".rb"
4946  * File.extname(".a/b/d/test.rb") #=> ".rb"
4947  * File.extname("foo.") #=> "" on Windows
4948  * File.extname("foo.") #=> "." on non-Windows
4949  * File.extname("test") #=> ""
4950  * File.extname(".profile") #=> ""
4951  * File.extname(".profile.sh") #=> ".sh"
4952  *
4953  */
4954 
4955 static VALUE
4956 rb_file_s_extname(VALUE klass, VALUE fname)
4957 {
4958  const char *name, *e;
4959  long len;
4960  VALUE extname;
4961 
4962  FilePathStringValue(fname);
4963  name = StringValueCStr(fname);
4964  len = RSTRING_LEN(fname);
4965  e = ruby_enc_find_extname(name, &len, rb_enc_get(fname));
4966  if (len < 1)
4967  return rb_str_new(0, 0);
4968  extname = rb_str_subseq(fname, e - name, len); /* keep the dot, too! */
4969  return extname;
4970 }
4971 
4972 /*
4973  * call-seq:
4974  * File.path(path) -> string
4975  *
4976  * Returns the string representation of the path
4977  *
4978  * File.path("/dev/null") #=> "/dev/null"
4979  * File.path(Pathname.new("/tmp")) #=> "/tmp"
4980  *
4981  */
4982 
4983 static VALUE
4984 rb_file_s_path(VALUE klass, VALUE fname)
4985 {
4986  return rb_get_path(fname);
4987 }
4988 
4989 /*
4990  * call-seq:
4991  * File.split(file_name) -> array
4992  *
4993  * Splits the given string into a directory and a file component and
4994  * returns them in a two-element array. See also File::dirname and
4995  * File::basename.
4996  *
4997  * File.split("/home/gumby/.profile") #=> ["/home/gumby", ".profile"]
4998  */
4999 
5000 static VALUE
5001 rb_file_s_split(VALUE klass, VALUE path)
5002 {
5003  FilePathStringValue(path); /* get rid of converting twice */
5004  return rb_assoc_new(rb_file_dirname(path), rb_file_s_basename(1,&path,Qundef));
5005 }
5006 
5007 static VALUE
5008 file_inspect_join(VALUE ary, VALUE arg, int recur)
5009 {
5010  if (recur || ary == arg) rb_raise(rb_eArgError, "recursive array");
5011  return rb_file_join(arg);
5012 }
5013 
5014 static VALUE
5015 rb_file_join(VALUE ary)
5016 {
5017  long len, i;
5018  VALUE result, tmp;
5019  const char *name, *tail;
5020  int checked = TRUE;
5021  rb_encoding *enc;
5022 
5023  if (RARRAY_LEN(ary) == 0) return rb_str_new(0, 0);
5024 
5025  len = 1;
5026  for (i=0; i<RARRAY_LEN(ary); i++) {
5027  tmp = RARRAY_AREF(ary, i);
5028  if (RB_TYPE_P(tmp, T_STRING)) {
5029  check_path_encoding(tmp);
5030  len += RSTRING_LEN(tmp);
5031  }
5032  else {
5033  len += 10;
5034  }
5035  }
5036  len += RARRAY_LEN(ary) - 1;
5037  result = rb_str_buf_new(len);
5038  RBASIC_CLEAR_CLASS(result);
5039  for (i=0; i<RARRAY_LEN(ary); i++) {
5040  tmp = RARRAY_AREF(ary, i);
5041  switch (OBJ_BUILTIN_TYPE(tmp)) {
5042  case T_STRING:
5043  if (!checked) check_path_encoding(tmp);
5044  StringValueCStr(tmp);
5045  break;
5046  case T_ARRAY:
5047  if (ary == tmp) {
5048  rb_raise(rb_eArgError, "recursive array");
5049  }
5050  else {
5051  tmp = rb_exec_recursive(file_inspect_join, ary, tmp);
5052  }
5053  break;
5054  default:
5055  FilePathStringValue(tmp);
5056  checked = FALSE;
5057  }
5058  RSTRING_GETMEM(result, name, len);
5059  if (i == 0) {
5060  rb_enc_copy(result, tmp);
5061  }
5062  else {
5063  tail = chompdirsep(name, name + len, rb_enc_get(result));
5064  if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) {
5065  rb_str_set_len(result, tail - name);
5066  }
5067  else if (!*tail) {
5068  rb_str_cat(result, "/", 1);
5069  }
5070  }
5071  enc = fs_enc_check(result, tmp);
5072  rb_str_buf_append(result, tmp);
5073  rb_enc_associate(result, enc);
5074  }
5075  RBASIC_SET_CLASS_RAW(result, rb_cString);
5076 
5077  return result;
5078 }
5079 
5080 /*
5081  * call-seq:
5082  * File.join(string, ...) -> string
5083  *
5084  * Returns a new string formed by joining the strings using
5085  * <code>"/"</code>.
5086  *
5087  * File.join("usr", "mail", "gumby") #=> "usr/mail/gumby"
5088  *
5089  */
5090 
5091 static VALUE
5092 rb_file_s_join(VALUE klass, VALUE args)
5093 {
5094  return rb_file_join(args);
5095 }
5096 
5097 #if defined(HAVE_TRUNCATE) || defined(HAVE_CHSIZE)
5098 struct truncate_arg {
5099  const char *path;
5100 #if defined(HAVE_TRUNCATE)
5101 #define NUM2POS(n) NUM2OFFT(n)
5102  off_t pos;
5103 #else
5104 #define NUM2POS(n) NUM2LONG(n)
5105  long pos;
5106 #endif
5107 };
5108 
5109 static void *
5110 nogvl_truncate(void *ptr)
5111 {
5112  struct truncate_arg *ta = ptr;
5113 #ifdef HAVE_TRUNCATE
5114  return (void *)(VALUE)truncate(ta->path, ta->pos);
5115 #else /* defined(HAVE_CHSIZE) */
5116  {
5117  int tmpfd = rb_cloexec_open(ta->path, 0, 0);
5118 
5119  if (tmpfd < 0)
5120  return (void *)-1;
5121  rb_update_max_fd(tmpfd);
5122  if (chsize(tmpfd, ta->pos) < 0) {
5123  int e = errno;
5124  close(tmpfd);
5125  errno = e;
5126  return (void *)-1;
5127  }
5128  close(tmpfd);
5129  return 0;
5130  }
5131 #endif
5132 }
5133 
5134 /*
5135  * call-seq:
5136  * File.truncate(file_name, integer) -> 0
5137  *
5138  * Truncates the file <i>file_name</i> to be at most <i>integer</i>
5139  * bytes long. Not available on all platforms.
5140  *
5141  * f = File.new("out", "w")
5142  * f.write("1234567890") #=> 10
5143  * f.close #=> nil
5144  * File.truncate("out", 5) #=> 0
5145  * File.size("out") #=> 5
5146  *
5147  */
5148 
5149 static VALUE
5150 rb_file_s_truncate(VALUE klass, VALUE path, VALUE len)
5151 {
5152  struct truncate_arg ta;
5153  int r;
5154 
5155  ta.pos = NUM2POS(len);
5156  FilePathValue(path);
5157  path = rb_str_encode_ospath(path);
5158  ta.path = StringValueCStr(path);
5159 
5160  r = (int)(VALUE)rb_thread_call_without_gvl(nogvl_truncate, &ta,
5161  RUBY_UBF_IO, NULL);
5162  if (r < 0)
5163  rb_sys_fail_path(path);
5164  return INT2FIX(0);
5165 #undef NUM2POS
5166 }
5167 #else
5168 #define rb_file_s_truncate rb_f_notimplement
5169 #endif
5170 
5171 #if defined(HAVE_FTRUNCATE) || defined(HAVE_CHSIZE)
5172 struct ftruncate_arg {
5173  int fd;
5174 #if defined(HAVE_FTRUNCATE)
5175 #define NUM2POS(n) NUM2OFFT(n)
5176  off_t pos;
5177 #else
5178 #define NUM2POS(n) NUM2LONG(n)
5179  long pos;
5180 #endif
5181 };
5182 
5183 static VALUE
5184 nogvl_ftruncate(void *ptr)
5185 {
5186  struct ftruncate_arg *fa = ptr;
5187 
5188 #ifdef HAVE_FTRUNCATE
5189  return (VALUE)ftruncate(fa->fd, fa->pos);
5190 #else /* defined(HAVE_CHSIZE) */
5191  return (VALUE)chsize(fa->fd, fa->pos);
5192 #endif
5193 }
5194 
5195 /*
5196  * call-seq:
5197  * file.truncate(integer) -> 0
5198  *
5199  * Truncates <i>file</i> to at most <i>integer</i> bytes. The file
5200  * must be opened for writing. Not available on all platforms.
5201  *
5202  * f = File.new("out", "w")
5203  * f.syswrite("1234567890") #=> 10
5204  * f.truncate(5) #=> 0
5205  * f.close() #=> nil
5206  * File.size("out") #=> 5
5207  */
5208 
5209 static VALUE
5210 rb_file_truncate(VALUE obj, VALUE len)
5211 {
5212  rb_io_t *fptr;
5213  struct ftruncate_arg fa;
5214 
5215  fa.pos = NUM2POS(len);
5216  GetOpenFile(obj, fptr);
5217  if (!(fptr->mode & FMODE_WRITABLE)) {
5218  rb_raise(rb_eIOError, "not opened for writing");
5219  }
5220  rb_io_flush_raw(obj, 0);
5221  fa.fd = fptr->fd;
5222  if ((int)rb_thread_io_blocking_region(nogvl_ftruncate, &fa, fa.fd) < 0) {
5223  rb_sys_fail_path(fptr->pathv);
5224  }
5225  return INT2FIX(0);
5226 #undef NUM2POS
5227 }
5228 #else
5229 #define rb_file_truncate rb_f_notimplement
5230 #endif
5231 
5232 # ifndef LOCK_SH
5233 # define LOCK_SH 1
5234 # endif
5235 # ifndef LOCK_EX
5236 # define LOCK_EX 2
5237 # endif
5238 # ifndef LOCK_NB
5239 # define LOCK_NB 4
5240 # endif
5241 # ifndef LOCK_UN
5242 # define LOCK_UN 8
5243 # endif
5244 
5245 #ifdef __CYGWIN__
5246 #include <winerror.h>
5247 #endif
5248 
5249 static VALUE
5250 rb_thread_flock(void *data)
5251 {
5252 #ifdef __CYGWIN__
5253  int old_errno = errno;
5254 #endif
5255  int *op = data, ret = flock(op[0], op[1]);
5256 
5257 #ifdef __CYGWIN__
5258  if (GetLastError() == ERROR_NOT_LOCKED) {
5259  ret = 0;
5260  errno = old_errno;
5261  }
5262 #endif
5263  return (VALUE)ret;
5264 }
5265 
5266 /*
5267  * call-seq:
5268  * file.flock(locking_constant) -> 0 or false
5269  *
5270  * Locks or unlocks a file according to <i>locking_constant</i> (a
5271  * logical <em>or</em> of the values in the table below).
5272  * Returns <code>false</code> if File::LOCK_NB is specified and the
5273  * operation would otherwise have blocked. Not available on all
5274  * platforms.
5275  *
5276  * Locking constants (in class File):
5277  *
5278  * LOCK_EX | Exclusive lock. Only one process may hold an
5279  * | exclusive lock for a given file at a time.
5280  * ----------+------------------------------------------------
5281  * LOCK_NB | Don't block when locking. May be combined
5282  * | with other lock options using logical or.
5283  * ----------+------------------------------------------------
5284  * LOCK_SH | Shared lock. Multiple processes may each hold a
5285  * | shared lock for a given file at the same time.
5286  * ----------+------------------------------------------------
5287  * LOCK_UN | Unlock.
5288  *
5289  * Example:
5290  *
5291  * # update a counter using write lock
5292  * # don't use "w" because it truncates the file before lock.
5293  * File.open("counter", File::RDWR|File::CREAT, 0644) {|f|
5294  * f.flock(File::LOCK_EX)
5295  * value = f.read.to_i + 1
5296  * f.rewind
5297  * f.write("#{value}\n")
5298  * f.flush
5299  * f.truncate(f.pos)
5300  * }
5301  *
5302  * # read the counter using read lock
5303  * File.open("counter", "r") {|f|
5304  * f.flock(File::LOCK_SH)
5305  * p f.read
5306  * }
5307  *
5308  */
5309 
5310 static VALUE
5311 rb_file_flock(VALUE obj, VALUE operation)
5312 {
5313  rb_io_t *fptr;
5314  int op[2], op1;
5315  struct timeval time;
5316 
5317  op[1] = op1 = NUM2INT(operation);
5318  GetOpenFile(obj, fptr);
5319  op[0] = fptr->fd;
5320 
5321  if (fptr->mode & FMODE_WRITABLE) {
5322  rb_io_flush_raw(obj, 0);
5323  }
5324  while ((int)rb_thread_io_blocking_region(rb_thread_flock, op, fptr->fd) < 0) {
5325  int e = errno;
5326  switch (e) {
5327  case EAGAIN:
5328  case EACCES:
5329 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5330  case EWOULDBLOCK:
5331 #endif
5332  if (op1 & LOCK_NB) return Qfalse;
5333 
5334  time.tv_sec = 0;
5335  time.tv_usec = 100 * 1000; /* 0.1 sec */
5336  rb_thread_wait_for(time);
5337  rb_io_check_closed(fptr);
5338  continue;
5339 
5340  case EINTR:
5341 #if defined(ERESTART)
5342  case ERESTART:
5343 #endif
5344  break;
5345 
5346  default:
5347  rb_syserr_fail_path(e, fptr->pathv);
5348  }
5349  }
5350  return INT2FIX(0);
5351 }
5352 
5353 static void
5354 test_check(int n, int argc, VALUE *argv)
5355 {
5356  int i;
5357 
5358  n+=1;
5359  rb_check_arity(argc, n, n);
5360  for (i=1; i<n; i++) {
5361  if (!RB_TYPE_P(argv[i], T_FILE)) {
5362  FilePathValue(argv[i]);
5363  }
5364  }
5365 }
5366 
5367 #define CHECK(n) test_check((n), argc, argv)
5368 
5369 /*
5370  * call-seq:
5371  * test(cmd, file1 [, file2] ) -> obj
5372  *
5373  * Uses the character +cmd+ to perform various tests on +file1+ (first
5374  * table below) or on +file1+ and +file2+ (second table).
5375  *
5376  * File tests on a single file:
5377  *
5378  * Cmd Returns Meaning
5379  * "A" | Time | Last access time for file1
5380  * "b" | boolean | True if file1 is a block device
5381  * "c" | boolean | True if file1 is a character device
5382  * "C" | Time | Last change time for file1
5383  * "d" | boolean | True if file1 exists and is a directory
5384  * "e" | boolean | True if file1 exists
5385  * "f" | boolean | True if file1 exists and is a regular file
5386  * "g" | boolean | True if file1 has the \CF{setgid} bit
5387  * | | set (false under NT)
5388  * "G" | boolean | True if file1 exists and has a group
5389  * | | ownership equal to the caller's group
5390  * "k" | boolean | True if file1 exists and has the sticky bit set
5391  * "l" | boolean | True if file1 exists and is a symbolic link
5392  * "M" | Time | Last modification time for file1
5393  * "o" | boolean | True if file1 exists and is owned by
5394  * | | the caller's effective uid
5395  * "O" | boolean | True if file1 exists and is owned by
5396  * | | the caller's real uid
5397  * "p" | boolean | True if file1 exists and is a fifo
5398  * "r" | boolean | True if file1 is readable by the effective
5399  * | | uid/gid of the caller
5400  * "R" | boolean | True if file is readable by the real
5401  * | | uid/gid of the caller
5402  * "s" | int/nil | If file1 has nonzero size, return the size,
5403  * | | otherwise return nil
5404  * "S" | boolean | True if file1 exists and is a socket
5405  * "u" | boolean | True if file1 has the setuid bit set
5406  * "w" | boolean | True if file1 exists and is writable by
5407  * | | the effective uid/gid
5408  * "W" | boolean | True if file1 exists and is writable by
5409  * | | the real uid/gid
5410  * "x" | boolean | True if file1 exists and is executable by
5411  * | | the effective uid/gid
5412  * "X" | boolean | True if file1 exists and is executable by
5413  * | | the real uid/gid
5414  * "z" | boolean | True if file1 exists and has a zero length
5415  *
5416  * Tests that take two files:
5417  *
5418  * "-" | boolean | True if file1 and file2 are identical
5419  * "=" | boolean | True if the modification times of file1
5420  * | | and file2 are equal
5421  * "<" | boolean | True if the modification time of file1
5422  * | | is prior to that of file2
5423  * ">" | boolean | True if the modification time of file1
5424  * | | is after that of file2
5425  */
5426 
5427 static VALUE
5428 rb_f_test(int argc, VALUE *argv, VALUE _)
5429 {
5430  int cmd;
5431 
5432  if (argc == 0) rb_check_arity(argc, 2, 3);
5433  cmd = NUM2CHR(argv[0]);
5434  if (cmd == 0) {
5435  goto unknown;
5436  }
5437  if (strchr("bcdefgGkloOprRsSuwWxXz", cmd)) {
5438  CHECK(1);
5439  switch (cmd) {
5440  case 'b':
5441  return rb_file_blockdev_p(0, argv[1]);
5442 
5443  case 'c':
5444  return rb_file_chardev_p(0, argv[1]);
5445 
5446  case 'd':
5447  return rb_file_directory_p(0, argv[1]);
5448 
5449  case 'e':
5450  return rb_file_exist_p(0, argv[1]);
5451 
5452  case 'f':
5453  return rb_file_file_p(0, argv[1]);
5454 
5455  case 'g':
5456  return rb_file_sgid_p(0, argv[1]);
5457 
5458  case 'G':
5459  return rb_file_grpowned_p(0, argv[1]);
5460 
5461  case 'k':
5462  return rb_file_sticky_p(0, argv[1]);
5463 
5464  case 'l':
5465  return rb_file_symlink_p(0, argv[1]);
5466 
5467  case 'o':
5468  return rb_file_owned_p(0, argv[1]);
5469 
5470  case 'O':
5471  return rb_file_rowned_p(0, argv[1]);
5472 
5473  case 'p':
5474  return rb_file_pipe_p(0, argv[1]);
5475 
5476  case 'r':
5477  return rb_file_readable_p(0, argv[1]);
5478 
5479  case 'R':
5480  return rb_file_readable_real_p(0, argv[1]);
5481 
5482  case 's':
5483  return rb_file_size_p(0, argv[1]);
5484 
5485  case 'S':
5486  return rb_file_socket_p(0, argv[1]);
5487 
5488  case 'u':
5489  return rb_file_suid_p(0, argv[1]);
5490 
5491  case 'w':
5492  return rb_file_writable_p(0, argv[1]);
5493 
5494  case 'W':
5495  return rb_file_writable_real_p(0, argv[1]);
5496 
5497  case 'x':
5498  return rb_file_executable_p(0, argv[1]);
5499 
5500  case 'X':
5501  return rb_file_executable_real_p(0, argv[1]);
5502 
5503  case 'z':
5504  return rb_file_zero_p(0, argv[1]);
5505  }
5506  }
5507 
5508  if (strchr("MAC", cmd)) {
5509  struct stat st;
5510  VALUE fname = argv[1];
5511 
5512  CHECK(1);
5513  if (rb_stat(fname, &st) == -1) {
5514  int e = errno;
5515  FilePathValue(fname);
5516  rb_syserr_fail_path(e, fname);
5517  }
5518 
5519  switch (cmd) {
5520  case 'A':
5521  return stat_atime(&st);
5522  case 'M':
5523  return stat_mtime(&st);
5524  case 'C':
5525  return stat_ctime(&st);
5526  }
5527  }
5528 
5529  if (cmd == '-') {
5530  CHECK(2);
5531  return rb_file_identical_p(0, argv[1], argv[2]);
5532  }
5533 
5534  if (strchr("=<>", cmd)) {
5535  struct stat st1, st2;
5536  struct timespec t1, t2;
5537 
5538  CHECK(2);
5539  if (rb_stat(argv[1], &st1) < 0) return Qfalse;
5540  if (rb_stat(argv[2], &st2) < 0) return Qfalse;
5541 
5542  t1 = stat_mtimespec(&st1);
5543  t2 = stat_mtimespec(&st2);
5544 
5545  switch (cmd) {
5546  case '=':
5547  if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec) return Qtrue;
5548  return Qfalse;
5549 
5550  case '>':
5551  if (t1.tv_sec > t2.tv_sec) return Qtrue;
5552  if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec) return Qtrue;
5553  return Qfalse;
5554 
5555  case '<':
5556  if (t1.tv_sec < t2.tv_sec) return Qtrue;
5557  if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec) return Qtrue;
5558  return Qfalse;
5559  }
5560  }
5561  unknown:
5562  /* unknown command */
5563  if (ISPRINT(cmd)) {
5564  rb_raise(rb_eArgError, "unknown command '%s%c'", cmd == '\'' || cmd == '\\' ? "\\" : "", cmd);
5565  }
5566  else {
5567  rb_raise(rb_eArgError, "unknown command \"\\x%02X\"", cmd);
5568  }
5570 }
5571 
5572 
5573 /*
5574  * Document-class: File::Stat
5575  *
5576  * Objects of class File::Stat encapsulate common status information
5577  * for File objects. The information is recorded at the moment the
5578  * File::Stat object is created; changes made to the file after that
5579  * point will not be reflected. File::Stat objects are returned by
5580  * IO#stat, File::stat, File#lstat, and File::lstat. Many of these
5581  * methods return platform-specific values, and not all values are
5582  * meaningful on all systems. See also Kernel#test.
5583  */
5584 
5585 static VALUE
5586 rb_stat_s_alloc(VALUE klass)
5587 {
5588  return stat_new_0(klass, 0);
5589 }
5590 
5591 /*
5592  * call-seq:
5593  *
5594  * File::Stat.new(file_name) -> stat
5595  *
5596  * Create a File::Stat object for the given file name (raising an
5597  * exception if the file doesn't exist).
5598  */
5599 
5600 static VALUE
5601 rb_stat_init(VALUE obj, VALUE fname)
5602 {
5603  struct stat st, *nst;
5604 
5605  FilePathValue(fname);
5606  fname = rb_str_encode_ospath(fname);
5607  if (STAT(StringValueCStr(fname), &st) == -1) {
5608  rb_sys_fail_path(fname);
5609  }
5610  if (DATA_PTR(obj)) {
5611  xfree(DATA_PTR(obj));
5612  DATA_PTR(obj) = NULL;
5613  }
5614  nst = ALLOC(struct stat);
5615  *nst = st;
5616  DATA_PTR(obj) = nst;
5617 
5618  return Qnil;
5619 }
5620 
5621 /* :nodoc: */
5622 static VALUE
5623 rb_stat_init_copy(VALUE copy, VALUE orig)
5624 {
5625  struct stat *nst;
5626 
5627  if (!OBJ_INIT_COPY(copy, orig)) return copy;
5628  if (DATA_PTR(copy)) {
5629  xfree(DATA_PTR(copy));
5630  DATA_PTR(copy) = 0;
5631  }
5632  if (DATA_PTR(orig)) {
5633  nst = ALLOC(struct stat);
5634  *nst = *(struct stat*)DATA_PTR(orig);
5635  DATA_PTR(copy) = nst;
5636  }
5637 
5638  return copy;
5639 }
5640 
5641 /*
5642  * call-seq:
5643  * stat.ftype -> string
5644  *
5645  * Identifies the type of <i>stat</i>. The return string is one of:
5646  * ``<code>file</code>'', ``<code>directory</code>'',
5647  * ``<code>characterSpecial</code>'', ``<code>blockSpecial</code>'',
5648  * ``<code>fifo</code>'', ``<code>link</code>'',
5649  * ``<code>socket</code>'', or ``<code>unknown</code>''.
5650  *
5651  * File.stat("/dev/tty").ftype #=> "characterSpecial"
5652  *
5653  */
5654 
5655 static VALUE
5656 rb_stat_ftype(VALUE obj)
5657 {
5658  return rb_file_ftype(get_stat(obj));
5659 }
5660 
5661 /*
5662  * call-seq:
5663  * stat.directory? -> true or false
5664  *
5665  * Returns <code>true</code> if <i>stat</i> is a directory,
5666  * <code>false</code> otherwise.
5667  *
5668  * File.stat("testfile").directory? #=> false
5669  * File.stat(".").directory? #=> true
5670  */
5671 
5672 static VALUE
5673 rb_stat_d(VALUE obj)
5674 {
5675  if (S_ISDIR(get_stat(obj)->st_mode)) return Qtrue;
5676  return Qfalse;
5677 }
5678 
5679 /*
5680  * call-seq:
5681  * stat.pipe? -> true or false
5682  *
5683  * Returns <code>true</code> if the operating system supports pipes and
5684  * <i>stat</i> is a pipe; <code>false</code> otherwise.
5685  */
5686 
5687 static VALUE
5688 rb_stat_p(VALUE obj)
5689 {
5690 #ifdef S_IFIFO
5691  if (S_ISFIFO(get_stat(obj)->st_mode)) return Qtrue;
5692 
5693 #endif
5694  return Qfalse;
5695 }
5696 
5697 /*
5698  * call-seq:
5699  * stat.symlink? -> true or false
5700  *
5701  * Returns <code>true</code> if <i>stat</i> is a symbolic link,
5702  * <code>false</code> if it isn't or if the operating system doesn't
5703  * support this feature. As File::stat automatically follows symbolic
5704  * links, #symlink? will always be <code>false</code> for an object
5705  * returned by File::stat.
5706  *
5707  * File.symlink("testfile", "alink") #=> 0
5708  * File.stat("alink").symlink? #=> false
5709  * File.lstat("alink").symlink? #=> true
5710  *
5711  */
5712 
5713 static VALUE
5714 rb_stat_l(VALUE obj)
5715 {
5716 #ifdef S_ISLNK
5717  if (S_ISLNK(get_stat(obj)->st_mode)) return Qtrue;
5718 #endif
5719  return Qfalse;
5720 }
5721 
5722 /*
5723  * call-seq:
5724  * stat.socket? -> true or false
5725  *
5726  * Returns <code>true</code> if <i>stat</i> is a socket,
5727  * <code>false</code> if it isn't or if the operating system doesn't
5728  * support this feature.
5729  *
5730  * File.stat("testfile").socket? #=> false
5731  *
5732  */
5733 
5734 static VALUE
5735 rb_stat_S(VALUE obj)
5736 {
5737 #ifdef S_ISSOCK
5738  if (S_ISSOCK(get_stat(obj)->st_mode)) return Qtrue;
5739 
5740 #endif
5741  return Qfalse;
5742 }
5743 
5744 /*
5745  * call-seq:
5746  * stat.blockdev? -> true or false
5747  *
5748  * Returns <code>true</code> if the file is a block device,
5749  * <code>false</code> if it isn't or if the operating system doesn't
5750  * support this feature.
5751  *
5752  * File.stat("testfile").blockdev? #=> false
5753  * File.stat("/dev/hda1").blockdev? #=> true
5754  *
5755  */
5756 
5757 static VALUE
5758 rb_stat_b(VALUE obj)
5759 {
5760 #ifdef S_ISBLK
5761  if (S_ISBLK(get_stat(obj)->st_mode)) return Qtrue;
5762 
5763 #endif
5764  return Qfalse;
5765 }
5766 
5767 /*
5768  * call-seq:
5769  * stat.chardev? -> true or false
5770  *
5771  * Returns <code>true</code> if the file is a character device,
5772  * <code>false</code> if it isn't or if the operating system doesn't
5773  * support this feature.
5774  *
5775  * File.stat("/dev/tty").chardev? #=> true
5776  *
5777  */
5778 
5779 static VALUE
5780 rb_stat_c(VALUE obj)
5781 {
5782  if (S_ISCHR(get_stat(obj)->st_mode)) return Qtrue;
5783 
5784  return Qfalse;
5785 }
5786 
5787 /*
5788  * call-seq:
5789  * stat.owned? -> true or false
5790  *
5791  * Returns <code>true</code> if the effective user id of the process is
5792  * the same as the owner of <i>stat</i>.
5793  *
5794  * File.stat("testfile").owned? #=> true
5795  * File.stat("/etc/passwd").owned? #=> false
5796  *
5797  */
5798 
5799 static VALUE
5800 rb_stat_owned(VALUE obj)
5801 {
5802  if (get_stat(obj)->st_uid == geteuid()) return Qtrue;
5803  return Qfalse;
5804 }
5805 
5806 static VALUE
5807 rb_stat_rowned(VALUE obj)
5808 {
5809  if (get_stat(obj)->st_uid == getuid()) return Qtrue;
5810  return Qfalse;
5811 }
5812 
5813 /*
5814  * call-seq:
5815  * stat.grpowned? -> true or false
5816  *
5817  * Returns true if the effective group id of the process is the same as
5818  * the group id of <i>stat</i>. On Windows NT, returns <code>false</code>.
5819  *
5820  * File.stat("testfile").grpowned? #=> true
5821  * File.stat("/etc/passwd").grpowned? #=> false
5822  *
5823  */
5824 
5825 static VALUE
5826 rb_stat_grpowned(VALUE obj)
5827 {
5828 #ifndef _WIN32
5829  if (rb_group_member(get_stat(obj)->st_gid)) return Qtrue;
5830 #endif
5831  return Qfalse;
5832 }
5833 
5834 /*
5835  * call-seq:
5836  * stat.readable? -> true or false
5837  *
5838  * Returns <code>true</code> if <i>stat</i> is readable by the
5839  * effective user id of this process.
5840  *
5841  * File.stat("testfile").readable? #=> true
5842  *
5843  */
5844 
5845 static VALUE
5846 rb_stat_r(VALUE obj)
5847 {
5848  struct stat *st = get_stat(obj);
5849 
5850 #ifdef USE_GETEUID
5851  if (geteuid() == 0) return Qtrue;
5852 #endif
5853 #ifdef S_IRUSR
5854  if (rb_stat_owned(obj))
5855  return RBOOL(st->st_mode & S_IRUSR);
5856 #endif
5857 #ifdef S_IRGRP
5858  if (rb_stat_grpowned(obj))
5859  return RBOOL(st->st_mode & S_IRGRP);
5860 #endif
5861 #ifdef S_IROTH
5862  if (!(st->st_mode & S_IROTH)) return Qfalse;
5863 #endif
5864  return Qtrue;
5865 }
5866 
5867 /*
5868  * call-seq:
5869  * stat.readable_real? -> true or false
5870  *
5871  * Returns <code>true</code> if <i>stat</i> is readable by the real
5872  * user id of this process.
5873  *
5874  * File.stat("testfile").readable_real? #=> true
5875  *
5876  */
5877 
5878 static VALUE
5879 rb_stat_R(VALUE obj)
5880 {
5881  struct stat *st = get_stat(obj);
5882 
5883 #ifdef USE_GETEUID
5884  if (getuid() == 0) return Qtrue;
5885 #endif
5886 #ifdef S_IRUSR
5887  if (rb_stat_rowned(obj))
5888  return RBOOL(st->st_mode & S_IRUSR);
5889 #endif
5890 #ifdef S_IRGRP
5891  if (rb_group_member(get_stat(obj)->st_gid))
5892  return RBOOL(st->st_mode & S_IRGRP);
5893 #endif
5894 #ifdef S_IROTH
5895  if (!(st->st_mode & S_IROTH)) return Qfalse;
5896 #endif
5897  return Qtrue;
5898 }
5899 
5900 /*
5901  * call-seq:
5902  * stat.world_readable? -> integer or nil
5903  *
5904  * If <i>stat</i> is readable by others, returns an integer
5905  * representing the file permission bits of <i>stat</i>. Returns
5906  * <code>nil</code> otherwise. The meaning of the bits is platform
5907  * dependent; on Unix systems, see <code>stat(2)</code>.
5908  *
5909  * m = File.stat("/etc/passwd").world_readable? #=> 420
5910  * sprintf("%o", m) #=> "644"
5911  */
5912 
5913 static VALUE
5914 rb_stat_wr(VALUE obj)
5915 {
5916 #ifdef S_IROTH
5917  struct stat *st = get_stat(obj);
5918  if ((st->st_mode & (S_IROTH)) == S_IROTH) {
5919  return UINT2NUM(st->st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
5920  }
5921  else {
5922  return Qnil;
5923  }
5924 #endif
5925 }
5926 
5927 /*
5928  * call-seq:
5929  * stat.writable? -> true or false
5930  *
5931  * Returns <code>true</code> if <i>stat</i> is writable by the
5932  * effective user id of this process.
5933  *
5934  * File.stat("testfile").writable? #=> true
5935  *
5936  */
5937 
5938 static VALUE
5939 rb_stat_w(VALUE obj)
5940 {
5941  struct stat *st = get_stat(obj);
5942 
5943 #ifdef USE_GETEUID
5944  if (geteuid() == 0) return Qtrue;
5945 #endif
5946 #ifdef S_IWUSR
5947  if (rb_stat_owned(obj))
5948  return RBOOL(st->st_mode & S_IWUSR);
5949 #endif
5950 #ifdef S_IWGRP
5951  if (rb_stat_grpowned(obj))
5952  return RBOOL(st->st_mode & S_IWGRP);
5953 #endif
5954 #ifdef S_IWOTH
5955  if (!(st->st_mode & S_IWOTH)) return Qfalse;
5956 #endif
5957  return Qtrue;
5958 }
5959 
5960 /*
5961  * call-seq:
5962  * stat.writable_real? -> true or false
5963  *
5964  * Returns <code>true</code> if <i>stat</i> is writable by the real
5965  * user id of this process.
5966  *
5967  * File.stat("testfile").writable_real? #=> true
5968  *
5969  */
5970 
5971 static VALUE
5972 rb_stat_W(VALUE obj)
5973 {
5974  struct stat *st = get_stat(obj);
5975 
5976 #ifdef USE_GETEUID
5977  if (getuid() == 0) return Qtrue;
5978 #endif
5979 #ifdef S_IWUSR
5980  if (rb_stat_rowned(obj))
5981  return RBOOL(st->st_mode & S_IWUSR);
5982 #endif
5983 #ifdef S_IWGRP
5984  if (rb_group_member(get_stat(obj)->st_gid))
5985  return RBOOL(st->st_mode & S_IWGRP);
5986 #endif
5987 #ifdef S_IWOTH
5988  if (!(st->st_mode & S_IWOTH)) return Qfalse;
5989 #endif
5990  return Qtrue;
5991 }
5992 
5993 /*
5994  * call-seq:
5995  * stat.world_writable? -> integer or nil
5996  *
5997  * If <i>stat</i> is writable by others, returns an integer
5998  * representing the file permission bits of <i>stat</i>. Returns
5999  * <code>nil</code> otherwise. The meaning of the bits is platform
6000  * dependent; on Unix systems, see <code>stat(2)</code>.
6001  *
6002  * m = File.stat("/tmp").world_writable? #=> 511
6003  * sprintf("%o", m) #=> "777"
6004  */
6005 
6006 static VALUE
6007 rb_stat_ww(VALUE obj)
6008 {
6009 #ifdef S_IROTH
6010  struct stat *st = get_stat(obj);
6011  if ((st->st_mode & (S_IWOTH)) == S_IWOTH) {
6012  return UINT2NUM(st->st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
6013  }
6014  else {
6015  return Qnil;
6016  }
6017 #endif
6018 }
6019 
6020 /*
6021  * call-seq:
6022  * stat.executable? -> true or false
6023  *
6024  * Returns <code>true</code> if <i>stat</i> is executable or if the
6025  * operating system doesn't distinguish executable files from
6026  * nonexecutable files. The tests are made using the effective owner of
6027  * the process.
6028  *
6029  * File.stat("testfile").executable? #=> false
6030  *
6031  */
6032 
6033 static VALUE
6034 rb_stat_x(VALUE obj)
6035 {
6036  struct stat *st = get_stat(obj);
6037 
6038 #ifdef USE_GETEUID
6039  if (geteuid() == 0) {
6040  return RBOOL(st->st_mode & S_IXUGO);
6041  }
6042 #endif
6043 #ifdef S_IXUSR
6044  if (rb_stat_owned(obj))
6045  return RBOOL(st->st_mode & S_IXUSR);
6046 #endif
6047 #ifdef S_IXGRP
6048  if (rb_stat_grpowned(obj))
6049  return RBOOL(st->st_mode & S_IXGRP);
6050 #endif
6051 #ifdef S_IXOTH
6052  if (!(st->st_mode & S_IXOTH)) return Qfalse;
6053 #endif
6054  return Qtrue;
6055 }
6056 
6057 /*
6058  * call-seq:
6059  * stat.executable_real? -> true or false
6060  *
6061  * Same as <code>executable?</code>, but tests using the real owner of
6062  * the process.
6063  */
6064 
6065 static VALUE
6066 rb_stat_X(VALUE obj)
6067 {
6068  struct stat *st = get_stat(obj);
6069 
6070 #ifdef USE_GETEUID
6071  if (getuid() == 0) {
6072  return RBOOL(st->st_mode & S_IXUGO);
6073  }
6074 #endif
6075 #ifdef S_IXUSR
6076  if (rb_stat_rowned(obj))
6077  return RBOOL(st->st_mode & S_IXUSR);
6078 #endif
6079 #ifdef S_IXGRP
6080  if (rb_group_member(get_stat(obj)->st_gid))
6081  return RBOOL(st->st_mode & S_IXGRP);
6082 #endif
6083 #ifdef S_IXOTH
6084  if (!(st->st_mode & S_IXOTH)) return Qfalse;
6085 #endif
6086  return Qtrue;
6087 }
6088 
6089 /*
6090  * call-seq:
6091  * stat.file? -> true or false
6092  *
6093  * Returns <code>true</code> if <i>stat</i> is a regular file (not
6094  * a device file, pipe, socket, etc.).
6095  *
6096  * File.stat("testfile").file? #=> true
6097  *
6098  */
6099 
6100 static VALUE
6101 rb_stat_f(VALUE obj)
6102 {
6103  if (S_ISREG(get_stat(obj)->st_mode)) return Qtrue;
6104  return Qfalse;
6105 }
6106 
6107 /*
6108  * call-seq:
6109  * stat.zero? -> true or false
6110  *
6111  * Returns <code>true</code> if <i>stat</i> is a zero-length file;
6112  * <code>false</code> otherwise.
6113  *
6114  * File.stat("testfile").zero? #=> false
6115  *
6116  */
6117 
6118 static VALUE
6119 rb_stat_z(VALUE obj)
6120 {
6121  if (get_stat(obj)->st_size == 0) return Qtrue;
6122  return Qfalse;
6123 }
6124 
6125 /*
6126  * call-seq:
6127  * stat.size? -> Integer or nil
6128  *
6129  * Returns +nil+ if <i>stat</i> is a zero-length file, the size of
6130  * the file otherwise.
6131  *
6132  * File.stat("testfile").size? #=> 66
6133  * File.stat("/dev/null").size? #=> nil
6134  *
6135  */
6136 
6137 static VALUE
6138 rb_stat_s(VALUE obj)
6139 {
6140  off_t size = get_stat(obj)->st_size;
6141 
6142  if (size == 0) return Qnil;
6143  return OFFT2NUM(size);
6144 }
6145 
6146 /*
6147  * call-seq:
6148  * stat.setuid? -> true or false
6149  *
6150  * Returns <code>true</code> if <i>stat</i> has the set-user-id
6151  * permission bit set, <code>false</code> if it doesn't or if the
6152  * operating system doesn't support this feature.
6153  *
6154  * File.stat("/bin/su").setuid? #=> true
6155  */
6156 
6157 static VALUE
6158 rb_stat_suid(VALUE obj)
6159 {
6160 #ifdef S_ISUID
6161  if (get_stat(obj)->st_mode & S_ISUID) return Qtrue;
6162 #endif
6163  return Qfalse;
6164 }
6165 
6166 /*
6167  * call-seq:
6168  * stat.setgid? -> true or false
6169  *
6170  * Returns <code>true</code> if <i>stat</i> has the set-group-id
6171  * permission bit set, <code>false</code> if it doesn't or if the
6172  * operating system doesn't support this feature.
6173  *
6174  * File.stat("/usr/sbin/lpc").setgid? #=> true
6175  *
6176  */
6177 
6178 static VALUE
6179 rb_stat_sgid(VALUE obj)
6180 {
6181 #ifdef S_ISGID
6182  if (get_stat(obj)->st_mode & S_ISGID) return Qtrue;
6183 #endif
6184  return Qfalse;
6185 }
6186 
6187 /*
6188  * call-seq:
6189  * stat.sticky? -> true or false
6190  *
6191  * Returns <code>true</code> if <i>stat</i> has its sticky bit set,
6192  * <code>false</code> if it doesn't or if the operating system doesn't
6193  * support this feature.
6194  *
6195  * File.stat("testfile").sticky? #=> false
6196  *
6197  */
6198 
6199 static VALUE
6200 rb_stat_sticky(VALUE obj)
6201 {
6202 #ifdef S_ISVTX
6203  if (get_stat(obj)->st_mode & S_ISVTX) return Qtrue;
6204 #endif
6205  return Qfalse;
6206 }
6207 
6208 #if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
6209 #define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
6210 #define HAVE_MKFIFO
6211 #endif
6212 
6213 #ifdef HAVE_MKFIFO
6214 struct mkfifo_arg {
6215  const char *path;
6216  mode_t mode;
6217 };
6218 
6219 static void *
6220 nogvl_mkfifo(void *ptr)
6221 {
6222  struct mkfifo_arg *ma = ptr;
6223 
6224  return (void *)(VALUE)mkfifo(ma->path, ma->mode);
6225 }
6226 
6227 /*
6228  * call-seq:
6229  * File.mkfifo(file_name, mode=0666) => 0
6230  *
6231  * Creates a FIFO special file with name _file_name_. _mode_
6232  * specifies the FIFO's permissions. It is modified by the process's
6233  * umask in the usual way: the permissions of the created file are
6234  * (mode & ~umask).
6235  */
6236 
6237 static VALUE
6238 rb_file_s_mkfifo(int argc, VALUE *argv, VALUE _)
6239 {
6240  VALUE path;
6241  struct mkfifo_arg ma;
6242 
6243  ma.mode = 0666;
6244  rb_check_arity(argc, 1, 2);
6245  if (argc > 1) {
6246  ma.mode = NUM2MODET(argv[1]);
6247  }
6248  path = argv[0];
6249  FilePathValue(path);
6250  path = rb_str_encode_ospath(path);
6251  ma.path = RSTRING_PTR(path);
6252  if (rb_thread_call_without_gvl(nogvl_mkfifo, &ma, RUBY_UBF_IO, 0)) {
6253  rb_sys_fail_path(path);
6254  }
6255  return INT2FIX(0);
6256 }
6257 #else
6258 #define rb_file_s_mkfifo rb_f_notimplement
6259 #endif
6260 
6261 static VALUE rb_mFConst;
6262 
6263 void
6264 rb_file_const(const char *name, VALUE value)
6265 {
6266  rb_define_const(rb_mFConst, name, value);
6267 }
6268 
6269 int
6270 rb_is_absolute_path(const char *path)
6271 {
6272 #ifdef DOSISH_DRIVE_LETTER
6273  if (has_drive_letter(path) && isdirsep(path[2])) return 1;
6274 #endif
6275 #ifdef DOSISH_UNC
6276  if (isdirsep(path[0]) && isdirsep(path[1])) return 1;
6277 #endif
6278 #ifndef DOSISH
6279  if (path[0] == '/') return 1;
6280 #endif
6281  return 0;
6282 }
6283 
6284 #ifndef ENABLE_PATH_CHECK
6285 # if defined DOSISH || defined __CYGWIN__
6286 # define ENABLE_PATH_CHECK 0
6287 # else
6288 # define ENABLE_PATH_CHECK 1
6289 # endif
6290 #endif
6291 
6292 #if ENABLE_PATH_CHECK
6293 static int
6294 path_check_0(VALUE path)
6295 {
6296  struct stat st;
6297  const char *p0 = StringValueCStr(path);
6298  const char *e0;
6299  rb_encoding *enc;
6300  char *p = 0, *s;
6301 
6302  if (!rb_is_absolute_path(p0)) {
6303  char *buf = ruby_getcwd();
6304  VALUE newpath;
6305 
6306  newpath = rb_str_new2(buf);
6307  xfree(buf);
6308 
6309  rb_str_cat2(newpath, "/");
6310  rb_str_cat2(newpath, p0);
6311  path = newpath;
6312  p0 = RSTRING_PTR(path);
6313  }
6314  e0 = p0 + RSTRING_LEN(path);
6315  enc = rb_enc_get(path);
6316  for (;;) {
6317 #ifndef S_IWOTH
6318 # define S_IWOTH 002
6319 #endif
6320  if (STAT(p0, &st) == 0 && S_ISDIR(st.st_mode) && (st.st_mode & S_IWOTH)
6321 #ifdef S_ISVTX
6322  && !(p && (st.st_mode & S_ISVTX))
6323 #endif
6324  && !access(p0, W_OK)) {
6325  rb_enc_warn(enc, "Insecure world writable dir %s in PATH, mode 0%"
6326 #if SIZEOF_DEV_T > SIZEOF_INT
6327  PRI_MODET_PREFIX"o",
6328 #else
6329  "o",
6330 #endif
6331  p0, st.st_mode);
6332  if (p) *p = '/';
6333  RB_GC_GUARD(path);
6334  return 0;
6335  }
6336  s = strrdirsep(p0, e0, enc);
6337  if (p) *p = '/';
6338  if (!s || s == p0) return 1;
6339  p = s;
6340  e0 = p;
6341  *p = '\0';
6342  }
6343 }
6344 #endif
6345 
6346 int
6347 rb_path_check(const char *path)
6348 {
6349 #if ENABLE_PATH_CHECK
6350  const char *p0, *p, *pend;
6351  const char sep = PATH_SEP_CHAR;
6352 
6353  if (!path) return 1;
6354 
6355  pend = path + strlen(path);
6356  p0 = path;
6357  p = strchr(path, sep);
6358  if (!p) p = pend;
6359 
6360  for (;;) {
6361  if (!path_check_0(rb_str_new(p0, p - p0))) {
6362  return 0; /* not safe */
6363  }
6364  p0 = p + 1;
6365  if (p0 > pend) break;
6366  p = strchr(p0, sep);
6367  if (!p) p = pend;
6368  }
6369 #endif
6370  return 1;
6371 }
6372 
6373 int
6374 ruby_is_fd_loadable(int fd)
6375 {
6376 #ifdef _WIN32
6377  return 1;
6378 #else
6379  struct stat st;
6380 
6381  if (fstat(fd, &st) < 0)
6382  return 0;
6383 
6384  if (S_ISREG(st.st_mode))
6385  return 1;
6386 
6387  if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
6388  return -1;
6389 
6390  if (S_ISDIR(st.st_mode))
6391  errno = EISDIR;
6392  else
6393  errno = ENXIO;
6394 
6395  return 0;
6396 #endif
6397 }
6398 
6399 #ifndef _WIN32
6400 int
6401 rb_file_load_ok(const char *path)
6402 {
6403  int ret = 1;
6404  /*
6405  open(2) may block if path is FIFO and it's empty. Let's use O_NONBLOCK.
6406  FIXME: Why O_NDELAY is checked?
6407  */
6408  int mode = (O_RDONLY |
6409 #if defined O_NONBLOCK
6410  O_NONBLOCK |
6411 #elif defined O_NDELAY
6412  O_NDELAY |
6413 #endif
6414  0);
6415  int fd = rb_cloexec_open(path, mode, 0);
6416  if (fd == -1) return 0;
6417  rb_update_max_fd(fd);
6418  ret = ruby_is_fd_loadable(fd);
6419  (void)close(fd);
6420  return ret;
6421 }
6422 #endif
6423 
6424 static int
6425 is_explicit_relative(const char *path)
6426 {
6427  if (*path++ != '.') return 0;
6428  if (*path == '.') path++;
6429  return isdirsep(*path);
6430 }
6431 
6432 static VALUE
6433 copy_path_class(VALUE path, VALUE orig)
6434 {
6435  int encidx = rb_enc_get_index(orig);
6436  if (encidx == ENCINDEX_ASCII || encidx == ENCINDEX_US_ASCII)
6437  encidx = rb_filesystem_encindex();
6438  rb_enc_associate_index(path, encidx);
6439  str_shrink(path);
6440  RBASIC_SET_CLASS(path, rb_obj_class(orig));
6441  OBJ_FREEZE(path);
6442  return path;
6443 }
6444 
6445 int
6446 rb_find_file_ext(VALUE *filep, const char *const *ext)
6447 {
6448  const char *f = StringValueCStr(*filep);
6449  VALUE fname = *filep, load_path, tmp;
6450  long i, j, fnlen;
6451  int expanded = 0;
6452 
6453  if (!ext[0]) return 0;
6454 
6455  if (f[0] == '~') {
6456  fname = file_expand_path_1(fname);
6457  f = RSTRING_PTR(fname);
6458  *filep = fname;
6459  expanded = 1;
6460  }
6461 
6462  if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6463  if (!expanded) fname = file_expand_path_1(fname);
6464  fnlen = RSTRING_LEN(fname);
6465  for (i=0; ext[i]; i++) {
6466  rb_str_cat2(fname, ext[i]);
6467  if (rb_file_load_ok(RSTRING_PTR(fname))) {
6468  *filep = copy_path_class(fname, *filep);
6469  return (int)(i+1);
6470  }
6471  rb_str_set_len(fname, fnlen);
6472  }
6473  return 0;
6474  }
6475 
6476  RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6477  if (!load_path) return 0;
6478 
6479  fname = rb_str_dup(*filep);
6480  RBASIC_CLEAR_CLASS(fname);
6481  fnlen = RSTRING_LEN(fname);
6482  tmp = rb_str_tmp_new(MAXPATHLEN + 2);
6484  for (j=0; ext[j]; j++) {
6485  rb_str_cat2(fname, ext[j]);
6486  for (i = 0; i < RARRAY_LEN(load_path); i++) {
6487  VALUE str = RARRAY_AREF(load_path, i);
6488 
6489  RB_GC_GUARD(str) = rb_get_path(str);
6490  if (RSTRING_LEN(str) == 0) continue;
6491  rb_file_expand_path_internal(fname, str, 0, 0, tmp);
6492  if (rb_file_load_ok(RSTRING_PTR(tmp))) {
6493  *filep = copy_path_class(tmp, *filep);
6494  return (int)(j+1);
6495  }
6496  }
6497  rb_str_set_len(fname, fnlen);
6498  }
6499  rb_str_resize(tmp, 0);
6500  RB_GC_GUARD(load_path);
6501  return 0;
6502 }
6503 
6504 VALUE
6506 {
6507  VALUE tmp, load_path;
6508  const char *f = StringValueCStr(path);
6509  int expanded = 0;
6510 
6511  if (f[0] == '~') {
6512  tmp = file_expand_path_1(path);
6513  path = copy_path_class(tmp, path);
6514  f = RSTRING_PTR(path);
6515  expanded = 1;
6516  }
6517 
6518  if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6519  if (!rb_file_load_ok(f)) return 0;
6520  if (!expanded)
6521  path = copy_path_class(file_expand_path_1(path), path);
6522  return path;
6523  }
6524 
6525  RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6526  if (load_path) {
6527  long i;
6528 
6529  tmp = rb_str_tmp_new(MAXPATHLEN + 2);
6531  for (i = 0; i < RARRAY_LEN(load_path); i++) {
6532  VALUE str = RARRAY_AREF(load_path, i);
6533  RB_GC_GUARD(str) = rb_get_path(str);
6534  if (RSTRING_LEN(str) > 0) {
6535  rb_file_expand_path_internal(path, str, 0, 0, tmp);
6536  f = RSTRING_PTR(tmp);
6537  if (rb_file_load_ok(f)) goto found;
6538  }
6539  }
6540  rb_str_resize(tmp, 0);
6541  return 0;
6542  }
6543  else {
6544  return 0; /* no path, no load */
6545  }
6546 
6547  found:
6548  return copy_path_class(tmp, path);
6549 }
6550 
6551 static void
6552 define_filetest_function(const char *name, VALUE (*func)(ANYARGS), int argc)
6553 {
6554  rb_define_module_function(rb_mFileTest, name, func, argc);
6555  rb_define_singleton_method(rb_cFile, name, func, argc);
6556 }
6557 
6558 const char ruby_null_device[] =
6559 #if defined DOSISH
6560  "NUL"
6561 #elif defined AMIGA || defined __amigaos__
6562  "NIL"
6563 #elif defined __VMS
6564  "NL:"
6565 #else
6566  "/dev/null"
6567 #endif
6568  ;
6569 
6570 /*
6571  * A File is an abstraction of any file object accessible by the
6572  * program and is closely associated with class IO. File includes
6573  * the methods of module FileTest as class methods, allowing you to
6574  * write (for example) <code>File.exist?("foo")</code>.
6575  *
6576  * In the description of File methods,
6577  * <em>permission bits</em> are a platform-specific
6578  * set of bits that indicate permissions of a file. On Unix-based
6579  * systems, permissions are viewed as a set of three octets, for the
6580  * owner, the group, and the rest of the world. For each of these
6581  * entities, permissions may be set to read, write, or execute the
6582  * file:
6583  *
6584  * The permission bits <code>0644</code> (in octal) would thus be
6585  * interpreted as read/write for owner, and read-only for group and
6586  * other. Higher-order bits may also be used to indicate the type of
6587  * file (plain, directory, pipe, socket, and so on) and various other
6588  * special features. If the permissions are for a directory, the
6589  * meaning of the execute bit changes; when set the directory can be
6590  * searched.
6591  *
6592  * On non-Posix operating systems, there may be only the ability to
6593  * make a file read-only or read-write. In this case, the remaining
6594  * permission bits will be synthesized to resemble typical values. For
6595  * instance, on Windows NT the default permission bits are
6596  * <code>0644</code>, which means read/write for owner, read-only for
6597  * all others. The only change that can be made is to make the file
6598  * read-only, which is reported as <code>0444</code>.
6599  *
6600  * Various constants for the methods in File can be found in File::Constants.
6601  *
6602  * == What's Here
6603  *
6604  * First, what's elsewhere. \Class \File:
6605  *
6606  * - Inherits from {class IO}[IO.html#class-IO-label-What-27s+Here],
6607  * in particular, methods for creating, reading, and writing files
6608  * - Includes {module FileTest}[FileTest.html#module-FileTest-label-What-27s+Here].
6609  * which provides dozens of additional methods.
6610  *
6611  * Here, class \File provides methods that are useful for:
6612  *
6613  * - {Creating}[#class-File-label-Creating]
6614  * - {Querying}[#class-File-label-Querying]
6615  * - {Settings}[#class-File-label-Settings]
6616  * - {Other}[#class-File-label-Other]
6617  *
6618  * === Creating
6619  *
6620  * - ::new:: Opens the file at the given path; returns the file.
6621  * - ::open:: Same as ::new, but when given a block will yield the file to the block,
6622  * and close the file upon exiting the block.
6623  * - ::link:: Creates a new name for an existing file using a hard link.
6624  * - ::mkfifo:: Returns the FIFO file created at the given path.
6625  * - ::symlink:: Creates a symbolic link for the given file path.
6626  *
6627  * === Querying
6628  *
6629  * _Paths_
6630  *
6631  * - ::absolute_path:: Returns the absolute file path for the given path.
6632  * - ::absolute_path?:: Returns whether the given path is the absolute file path.
6633  * - ::basename:: Returns the last component of the given file path.
6634  * - ::dirname:: Returns all but the last component of the given file path.
6635  * - ::expand_path:: Returns the absolute file path for the given path,
6636  * expanding <tt>~</tt> for a home directory.
6637  * - ::extname:: Returns the file extension for the given file path.
6638  * - ::fnmatch? (aliased as ::fnmatch):: Returns whether the given file path
6639  * matches the given pattern.
6640  * - ::join:: Joins path components into a single path string.
6641  * - ::path:: Returns the string representation of the given path.
6642  * - ::readlink:: Returns the path to the file at the given symbolic link.
6643  * - ::realdirpath:: Returns the real path for the given file path,
6644  * where the last component need not exist.
6645  * - ::realpath:: Returns the real path for the given file path,
6646  * where all components must exist.
6647  * - ::split:: Returns an array of two strings: the directory name and basename
6648  * of the file at the given path.
6649  * - #path (aliased as #to_path):: Returns the string representation of the given path.
6650  *
6651  * _Times_
6652  *
6653  * - ::atime:: Returns a \Time for the most recent access to the given file.
6654  * - ::birthtime:: Returns a \Time for the creation of the given file.
6655  * - ::ctime:: Returns a \Time for the metadata change of the given file.
6656  * - ::mtime:: Returns a \Time for the most recent data modification to
6657  * the content of the given file.
6658  * - #atime:: Returns a \Time for the most recent access to +self+.
6659  * - #birthtime:: Returns a \Time the creation for +self+.
6660  * - #ctime:: Returns a \Time for the metadata change of +self+.
6661  * - #mtime:: Returns a \Time for the most recent data modification
6662  * to the content of +self+.
6663  *
6664  * _Types_
6665  *
6666  * - ::blockdev?:: Returns whether the file at the given path is a block device.
6667  * - ::chardev?:: Returns whether the file at the given path is a character device.
6668  * - ::directory?:: Returns whether the file at the given path is a diretory.
6669  * - ::executable?:: Returns whether the file at the given path is executable
6670  * by the effective user and group of the current process.
6671  * - ::executable_real?:: Returns whether the file at the given path is executable
6672  * by the real user and group of the current process.
6673  * - ::exist?:: Returns whether the file at the given path exists.
6674  * - ::file?:: Returns whether the file at the given path is a regular file.
6675  * - ::ftype:: Returns a string giving the type of the file at the given path.
6676  * - ::grpowned?:: Returns whether the effective group of the current process
6677  * owns the file at the given path.
6678  * - ::identical?:: Returns whether the files at two given paths are identical.
6679  * - ::lstat:: Returns the File::Stat object for the last symbolic link
6680  * in the given path.
6681  * - ::owned?:: Returns whether the effective user of the current process
6682  * owns the file at the given path.
6683  * - ::pipe?:: Returns whether the file at the given path is a pipe.
6684  * - ::readable?:: Returns whether the file at the given path is readable
6685  * by the effective user and group of the current process.
6686  * - ::readable_real?:: Returns whether the file at the given path is readable
6687  * by the real user and group of the current process.
6688  * - ::setgid?:: Returns whether the setgid bit is set for the file at the given path.
6689  * - ::setuid?:: Returns whether the setuid bit is set for the file at the given path.
6690  * - ::socket?:: Returns whether the file at the given path is a socket.
6691  * - ::stat:: Returns the File::Stat object for the file at the given path.
6692  * - ::sticky?:: Returns whether the file at the given path has its sticky bit set.
6693  * - ::symlink?:: Returns whether the file at the given path is a symbolic link.
6694  * - ::umask:: Returns the umask value for the current process.
6695  * - ::world_readable?:: Returns whether the file at the given path is readable
6696  * by others.
6697  * - ::world_writable?:: Returns whether the file at the given path is writable
6698  * by others.
6699  * - ::writable?:: Returns whether the file at the given path is writable
6700  * by the effective user and group of the current process.
6701  * - ::writable_real?:: Returns whether the file at the given path is writable
6702  * by the real user and group of the current process.
6703  * - #lstat:: Returns the File::Stat object for the last symbolic link
6704  * in the path for +self+.
6705  *
6706  * _Contents_
6707  *
6708  * - ::empty? (aliased as ::zero?):: Returns whether the file at the given path
6709  * exists and is empty.
6710  * - ::size:: Returns the size (bytes) of the file at the given path.
6711  * - ::size?:: Returns +nil+ if there is no file at the given path,
6712  * or if that file is empty; otherwise returns the file size (bytes).
6713  * - #size:: Returns the size (bytes) of +self+.
6714  *
6715  * === Settings
6716  *
6717  * - ::chmod:: Changes permissions of the file at the given path.
6718  * - ::chown:: Change ownership of the file at the given path.
6719  * - ::lchmod:: Changes permissions of the last symbolic link in the given path.
6720  * - ::lchown:: Change ownership of the last symbolic in the given path.
6721  * - ::lutime:: For each given file path, sets the access time and modification time
6722  * of the last symbolic link in the path.
6723  * - ::rename:: Moves the file at one given path to another given path.
6724  * - ::utime:: Sets the access time and modification time of each file
6725  * at the given paths.
6726  * - #flock:: Locks or unlocks +self+.
6727  *
6728  * === Other
6729  *
6730  * - ::truncate:: Truncates the file at the given file path to the given size.
6731  * - ::unlink (aliased as ::delete):: Deletes the file for each given file path.
6732  * - #truncate:: Truncates +self+ to the given size.
6733  *
6734  */
6735 
6736 void
6737 Init_File(void)
6738 {
6739 #if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
6740  rb_CFString_class_initialize_before_fork();
6741 #endif
6742 
6743  VALUE separator;
6744 
6745  rb_mFileTest = rb_define_module("FileTest");
6746  rb_cFile = rb_define_class("File", rb_cIO);
6747 
6748  define_filetest_function("directory?", rb_file_directory_p, 1);
6749  define_filetest_function("exist?", rb_file_exist_p, 1);
6750  define_filetest_function("exists?", rb_file_exists_p, 1);
6751  define_filetest_function("readable?", rb_file_readable_p, 1);
6752  define_filetest_function("readable_real?", rb_file_readable_real_p, 1);
6753  define_filetest_function("world_readable?", rb_file_world_readable_p, 1);
6754  define_filetest_function("writable?", rb_file_writable_p, 1);
6755  define_filetest_function("writable_real?", rb_file_writable_real_p, 1);
6756  define_filetest_function("world_writable?", rb_file_world_writable_p, 1);
6757  define_filetest_function("executable?", rb_file_executable_p, 1);
6758  define_filetest_function("executable_real?", rb_file_executable_real_p, 1);
6759  define_filetest_function("file?", rb_file_file_p, 1);
6760  define_filetest_function("zero?", rb_file_zero_p, 1);
6761  define_filetest_function("empty?", rb_file_zero_p, 1);
6762  define_filetest_function("size?", rb_file_size_p, 1);
6763  define_filetest_function("size", rb_file_s_size, 1);
6764  define_filetest_function("owned?", rb_file_owned_p, 1);
6765  define_filetest_function("grpowned?", rb_file_grpowned_p, 1);
6766 
6767  define_filetest_function("pipe?", rb_file_pipe_p, 1);
6768  define_filetest_function("symlink?", rb_file_symlink_p, 1);
6769  define_filetest_function("socket?", rb_file_socket_p, 1);
6770 
6771  define_filetest_function("blockdev?", rb_file_blockdev_p, 1);
6772  define_filetest_function("chardev?", rb_file_chardev_p, 1);
6773 
6774  define_filetest_function("setuid?", rb_file_suid_p, 1);
6775  define_filetest_function("setgid?", rb_file_sgid_p, 1);
6776  define_filetest_function("sticky?", rb_file_sticky_p, 1);
6777 
6778  define_filetest_function("identical?", rb_file_identical_p, 2);
6779 
6780  rb_define_singleton_method(rb_cFile, "stat", rb_file_s_stat, 1);
6781  rb_define_singleton_method(rb_cFile, "lstat", rb_file_s_lstat, 1);
6782  rb_define_singleton_method(rb_cFile, "ftype", rb_file_s_ftype, 1);
6783 
6784  rb_define_singleton_method(rb_cFile, "atime", rb_file_s_atime, 1);
6785  rb_define_singleton_method(rb_cFile, "mtime", rb_file_s_mtime, 1);
6786  rb_define_singleton_method(rb_cFile, "ctime", rb_file_s_ctime, 1);
6787  rb_define_singleton_method(rb_cFile, "birthtime", rb_file_s_birthtime, 1);
6788 
6789  rb_define_singleton_method(rb_cFile, "utime", rb_file_s_utime, -1);
6790  rb_define_singleton_method(rb_cFile, "chmod", rb_file_s_chmod, -1);
6791  rb_define_singleton_method(rb_cFile, "chown", rb_file_s_chown, -1);
6792  rb_define_singleton_method(rb_cFile, "lchmod", rb_file_s_lchmod, -1);
6793  rb_define_singleton_method(rb_cFile, "lchown", rb_file_s_lchown, -1);
6794  rb_define_singleton_method(rb_cFile, "lutime", rb_file_s_lutime, -1);
6795 
6796  rb_define_singleton_method(rb_cFile, "link", rb_file_s_link, 2);
6797  rb_define_singleton_method(rb_cFile, "symlink", rb_file_s_symlink, 2);
6798  rb_define_singleton_method(rb_cFile, "readlink", rb_file_s_readlink, 1);
6799 
6800  rb_define_singleton_method(rb_cFile, "unlink", rb_file_s_unlink, -1);
6801  rb_define_singleton_method(rb_cFile, "delete", rb_file_s_unlink, -1);
6802  rb_define_singleton_method(rb_cFile, "rename", rb_file_s_rename, 2);
6803  rb_define_singleton_method(rb_cFile, "umask", rb_file_s_umask, -1);
6804  rb_define_singleton_method(rb_cFile, "truncate", rb_file_s_truncate, 2);
6805  rb_define_singleton_method(rb_cFile, "mkfifo", rb_file_s_mkfifo, -1);
6806  rb_define_singleton_method(rb_cFile, "expand_path", s_expand_path, -1);
6807  rb_define_singleton_method(rb_cFile, "absolute_path", s_absolute_path, -1);
6808  rb_define_singleton_method(rb_cFile, "absolute_path?", s_absolute_path_p, 1);
6809  rb_define_singleton_method(rb_cFile, "realpath", rb_file_s_realpath, -1);
6810  rb_define_singleton_method(rb_cFile, "realdirpath", rb_file_s_realdirpath, -1);
6811  rb_define_singleton_method(rb_cFile, "basename", rb_file_s_basename, -1);
6812  rb_define_singleton_method(rb_cFile, "dirname", rb_file_s_dirname, -1);
6813  rb_define_singleton_method(rb_cFile, "extname", rb_file_s_extname, 1);
6814  rb_define_singleton_method(rb_cFile, "path", rb_file_s_path, 1);
6815 
6816  separator = rb_fstring_lit("/");
6817  /* separates directory parts in path */
6818  rb_define_const(rb_cFile, "Separator", separator);
6819  /* separates directory parts in path */
6820  rb_define_const(rb_cFile, "SEPARATOR", separator);
6821  rb_define_singleton_method(rb_cFile, "split", rb_file_s_split, 1);
6822  rb_define_singleton_method(rb_cFile, "join", rb_file_s_join, -2);
6823 
6824 #ifdef DOSISH
6825  /* platform specific alternative separator */
6826  rb_define_const(rb_cFile, "ALT_SEPARATOR", rb_obj_freeze(rb_usascii_str_new2(file_alt_separator)));
6827 #else
6828  rb_define_const(rb_cFile, "ALT_SEPARATOR", Qnil);
6829 #endif
6830  /* path list separator */
6831  rb_define_const(rb_cFile, "PATH_SEPARATOR", rb_fstring_cstr(PATH_SEP));
6832 
6833  rb_define_method(rb_cIO, "stat", rb_io_stat, 0); /* this is IO's method */
6834  rb_define_method(rb_cFile, "lstat", rb_file_lstat, 0);
6835 
6836  rb_define_method(rb_cFile, "atime", rb_file_atime, 0);
6837  rb_define_method(rb_cFile, "mtime", rb_file_mtime, 0);
6838  rb_define_method(rb_cFile, "ctime", rb_file_ctime, 0);
6839  rb_define_method(rb_cFile, "birthtime", rb_file_birthtime, 0);
6840  rb_define_method(rb_cFile, "size", file_size, 0);
6841 
6842  rb_define_method(rb_cFile, "chmod", rb_file_chmod, 1);
6843  rb_define_method(rb_cFile, "chown", rb_file_chown, 2);
6844  rb_define_method(rb_cFile, "truncate", rb_file_truncate, 1);
6845 
6846  rb_define_method(rb_cFile, "flock", rb_file_flock, 1);
6847 
6848  /*
6849  * Document-module: File::Constants
6850  *
6851  * File::Constants provides file-related constants. All possible
6852  * file constants are listed in the documentation but they may not all
6853  * be present on your platform.
6854  *
6855  * If the underlying platform doesn't define a constant the corresponding
6856  * Ruby constant is not defined.
6857  *
6858  * Your platform documentations (e.g. man open(2)) may describe more
6859  * detailed information.
6860  */
6861  rb_mFConst = rb_define_module_under(rb_cFile, "Constants");
6862  rb_include_module(rb_cIO, rb_mFConst);
6863 
6864  /* open for reading only */
6865  rb_define_const(rb_mFConst, "RDONLY", INT2FIX(O_RDONLY));
6866  /* open for writing only */
6867  rb_define_const(rb_mFConst, "WRONLY", INT2FIX(O_WRONLY));
6868  /* open for reading and writing */
6869  rb_define_const(rb_mFConst, "RDWR", INT2FIX(O_RDWR));
6870  /* append on each write */
6871  rb_define_const(rb_mFConst, "APPEND", INT2FIX(O_APPEND));
6872  /* create file if it does not exist */
6873  rb_define_const(rb_mFConst, "CREAT", INT2FIX(O_CREAT));
6874  /* error if CREAT and the file exists */
6875  rb_define_const(rb_mFConst, "EXCL", INT2FIX(O_EXCL));
6876 #if defined(O_NDELAY) || defined(O_NONBLOCK)
6877 # ifndef O_NONBLOCK
6878 # define O_NONBLOCK O_NDELAY
6879 # endif
6880  /* do not block on open or for data to become available */
6881  rb_define_const(rb_mFConst, "NONBLOCK", INT2FIX(O_NONBLOCK));
6882 #endif
6883  /* truncate size to 0 */
6884  rb_define_const(rb_mFConst, "TRUNC", INT2FIX(O_TRUNC));
6885 #ifdef O_NOCTTY
6886  /* not to make opened IO the controlling terminal device */
6887  rb_define_const(rb_mFConst, "NOCTTY", INT2FIX(O_NOCTTY));
6888 #endif
6889 #ifndef O_BINARY
6890 # define O_BINARY 0
6891 #endif
6892  /* disable line code conversion */
6893  rb_define_const(rb_mFConst, "BINARY", INT2FIX(O_BINARY));
6894 #ifndef O_SHARE_DELETE
6895 # define O_SHARE_DELETE 0
6896 #endif
6897  /* can delete opened file */
6898  rb_define_const(rb_mFConst, "SHARE_DELETE", INT2FIX(O_SHARE_DELETE));
6899 #ifdef O_SYNC
6900  /* any write operation perform synchronously */
6901  rb_define_const(rb_mFConst, "SYNC", INT2FIX(O_SYNC));
6902 #endif
6903 #ifdef O_DSYNC
6904  /* any write operation perform synchronously except some meta data */
6905  rb_define_const(rb_mFConst, "DSYNC", INT2FIX(O_DSYNC));
6906 #endif
6907 #ifdef O_RSYNC
6908  /* any read operation perform synchronously. used with SYNC or DSYNC. */
6909  rb_define_const(rb_mFConst, "RSYNC", INT2FIX(O_RSYNC));
6910 #endif
6911 #ifdef O_NOFOLLOW
6912  /* do not follow symlinks */
6913  rb_define_const(rb_mFConst, "NOFOLLOW", INT2FIX(O_NOFOLLOW)); /* FreeBSD, Linux */
6914 #endif
6915 #ifdef O_NOATIME
6916  /* do not change atime */
6917  rb_define_const(rb_mFConst, "NOATIME", INT2FIX(O_NOATIME)); /* Linux */
6918 #endif
6919 #ifdef O_DIRECT
6920  /* Try to minimize cache effects of the I/O to and from this file. */
6921  rb_define_const(rb_mFConst, "DIRECT", INT2FIX(O_DIRECT));
6922 #endif
6923 #ifdef O_TMPFILE
6924  /* Create an unnamed temporary file */
6925  rb_define_const(rb_mFConst, "TMPFILE", INT2FIX(O_TMPFILE));
6926 #endif
6927 
6928  /* shared lock. see File#flock */
6929  rb_define_const(rb_mFConst, "LOCK_SH", INT2FIX(LOCK_SH));
6930  /* exclusive lock. see File#flock */
6931  rb_define_const(rb_mFConst, "LOCK_EX", INT2FIX(LOCK_EX));
6932  /* unlock. see File#flock */
6933  rb_define_const(rb_mFConst, "LOCK_UN", INT2FIX(LOCK_UN));
6934  /* non-blocking lock. used with LOCK_SH or LOCK_EX. see File#flock */
6935  rb_define_const(rb_mFConst, "LOCK_NB", INT2FIX(LOCK_NB));
6936 
6937  /* Name of the null device */
6938  rb_define_const(rb_mFConst, "NULL", rb_fstring_cstr(ruby_null_device));
6939 
6940  rb_define_method(rb_cFile, "path", rb_file_path, 0);
6941  rb_define_method(rb_cFile, "to_path", rb_file_path, 0);
6942  rb_define_global_function("test", rb_f_test, -1);
6943 
6944  rb_cStat = rb_define_class_under(rb_cFile, "Stat", rb_cObject);
6945  rb_define_alloc_func(rb_cStat, rb_stat_s_alloc);
6946  rb_define_method(rb_cStat, "initialize", rb_stat_init, 1);
6947  rb_define_method(rb_cStat, "initialize_copy", rb_stat_init_copy, 1);
6948 
6950 
6951  rb_define_method(rb_cStat, "<=>", rb_stat_cmp, 1);
6952 
6953  rb_define_method(rb_cStat, "dev", rb_stat_dev, 0);
6954  rb_define_method(rb_cStat, "dev_major", rb_stat_dev_major, 0);
6955  rb_define_method(rb_cStat, "dev_minor", rb_stat_dev_minor, 0);
6956  rb_define_method(rb_cStat, "ino", rb_stat_ino, 0);
6957  rb_define_method(rb_cStat, "mode", rb_stat_mode, 0);
6958  rb_define_method(rb_cStat, "nlink", rb_stat_nlink, 0);
6959  rb_define_method(rb_cStat, "uid", rb_stat_uid, 0);
6960  rb_define_method(rb_cStat, "gid", rb_stat_gid, 0);
6961  rb_define_method(rb_cStat, "rdev", rb_stat_rdev, 0);
6962  rb_define_method(rb_cStat, "rdev_major", rb_stat_rdev_major, 0);
6963  rb_define_method(rb_cStat, "rdev_minor", rb_stat_rdev_minor, 0);
6964  rb_define_method(rb_cStat, "size", rb_stat_size, 0);
6965  rb_define_method(rb_cStat, "blksize", rb_stat_blksize, 0);
6966  rb_define_method(rb_cStat, "blocks", rb_stat_blocks, 0);
6967  rb_define_method(rb_cStat, "atime", rb_stat_atime, 0);
6968  rb_define_method(rb_cStat, "mtime", rb_stat_mtime, 0);
6969  rb_define_method(rb_cStat, "ctime", rb_stat_ctime, 0);
6970  rb_define_method(rb_cStat, "birthtime", rb_stat_birthtime, 0);
6971 
6972  rb_define_method(rb_cStat, "inspect", rb_stat_inspect, 0);
6973 
6974  rb_define_method(rb_cStat, "ftype", rb_stat_ftype, 0);
6975 
6976  rb_define_method(rb_cStat, "directory?", rb_stat_d, 0);
6977  rb_define_method(rb_cStat, "readable?", rb_stat_r, 0);
6978  rb_define_method(rb_cStat, "readable_real?", rb_stat_R, 0);
6979  rb_define_method(rb_cStat, "world_readable?", rb_stat_wr, 0);
6980  rb_define_method(rb_cStat, "writable?", rb_stat_w, 0);
6981  rb_define_method(rb_cStat, "writable_real?", rb_stat_W, 0);
6982  rb_define_method(rb_cStat, "world_writable?", rb_stat_ww, 0);
6983  rb_define_method(rb_cStat, "executable?", rb_stat_x, 0);
6984  rb_define_method(rb_cStat, "executable_real?", rb_stat_X, 0);
6985  rb_define_method(rb_cStat, "file?", rb_stat_f, 0);
6986  rb_define_method(rb_cStat, "zero?", rb_stat_z, 0);
6987  rb_define_method(rb_cStat, "size?", rb_stat_s, 0);
6988  rb_define_method(rb_cStat, "owned?", rb_stat_owned, 0);
6989  rb_define_method(rb_cStat, "grpowned?", rb_stat_grpowned, 0);
6990 
6991  rb_define_method(rb_cStat, "pipe?", rb_stat_p, 0);
6992  rb_define_method(rb_cStat, "symlink?", rb_stat_l, 0);
6993  rb_define_method(rb_cStat, "socket?", rb_stat_S, 0);
6994 
6995  rb_define_method(rb_cStat, "blockdev?", rb_stat_b, 0);
6996  rb_define_method(rb_cStat, "chardev?", rb_stat_c, 0);
6997 
6998  rb_define_method(rb_cStat, "setuid?", rb_stat_suid, 0);
6999  rb_define_method(rb_cStat, "setgid?", rb_stat_sgid, 0);
7000  rb_define_method(rb_cStat, "sticky?", rb_stat_sticky, 0);
7001 }
#define LONG_LONG
Definition: long_long.h:38
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:685
#define PATH_SEP
The delimiter of PATH environment variable.
Definition: dosish.h:45
#define PATH_SEP_CHAR
Identical to PATH_SEP, except it is of type char.
Definition: dosish.h:49
#define GIDT2NUM
Converts a C's gid_t into an instance of rb_cInteger.
Definition: gid_t.h:28
#define NUM2GIDT
Converts an instance of rb_cNumeric into C's gid_t.
Definition: gid_t.h:33
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
Definition: class.c:1043
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:837
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition: class.c:869
VALUE rb_define_module(const char *name)
Defines a top-level module.
Definition: class.c:948
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
Definition: class.c:972
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for a module.
Definition: class.c:2100
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a method.
Definition: class.c:1914
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:2110
#define rb_str_new2
Old name of rb_str_new_cstr.
Definition: string.h:1738
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition: coderange.h:180
#define T_FILE
Old name of RUBY_T_FILE.
Definition: value_type.h:62
#define NUM2ULONG
Old name of RB_NUM2ULONG.
Definition: long.h:52
#define ALLOCV
Old name of RB_ALLOCV.
Definition: memory.h:398
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition: object.h:41
#define ALLOC
Old name of RB_ALLOC.
Definition: memory.h:394
#define T_STRING
Old name of RUBY_T_STRING.
Definition: value_type.h:78
#define xfree
Old name of ruby_xfree.
Definition: xmalloc.h:58
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition: long.h:48
#define ID2SYM
Old name of RB_ID2SYM.
Definition: symbol.h:44
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
Definition: string.h:1743
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition: fl_type.h:143
#define ULONG2NUM
Old name of RB_ULONG2NUM.
Definition: long.h:60
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
Definition: assume.h:31
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
Definition: encoding.h:108
#define LONG2FIX
Old name of RB_INT2FIX.
Definition: long.h:49
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition: encoding.h:533
#define STRCASECMP
Old name of st_locale_insensitive_strcasecmp.
Definition: ctype.h:102
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
Definition: string.h:1744
#define ISALPHA
Old name of rb_isalpha.
Definition: ctype.h:92
#define ULL2NUM
Old name of RB_ULL2NUM.
Definition: long_long.h:31
#define TOLOWER
Old name of rb_tolower.
Definition: ctype.h:101
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
Definition: int.h:44
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition: coderange.h:182
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition: value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition: memory.h:399
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition: encoding.h:532
#define ISPRINT
Old name of rb_isprint.
Definition: ctype.h:86
#define NUM2CHR
Old name of RB_NUM2CHR.
Definition: char.h:33
#define T_CLASS
Old name of RUBY_T_CLASS.
Definition: value_type.h:58
#define ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
Definition: coderange.h:187
#define UINT2NUM
Old name of RB_UINT2NUM.
Definition: int.h:46
#define CONST_ID
Old name of RUBY_CONST_ID.
Definition: symbol.h:47
#define rb_str_new4
Old name of rb_str_new_frozen.
Definition: string.h:1740
#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
VALUE rb_eNotImpError
NotImplementedError exception.
Definition: error.c:1109
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:675
void rb_bug(const char *fmt,...)
Interpreter panic switch.
Definition: error.c:802
VALUE rb_eIOError
IOError exception.
Definition: io.c:187
VALUE rb_eTypeError
TypeError exception.
Definition: error.c:1099
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition: error.c:1106
VALUE rb_eArgError
ArgumentError exception.
Definition: error.c:1100
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
Definition: error.c:3006
VALUE rb_rescue(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2)
Identical to rb_rescue2(), except it does not take a list of exception classes.
Definition: eval.c:954
VALUE rb_eSystemCallError
SystemCallError exception.
Definition: error.c:1119
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
Definition: object.c:1950
VALUE rb_cIO
IO class.
Definition: io.c:185
VALUE rb_cStat
File::Stat class.
Definition: file.c:176
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition: object.c:188
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition: object.c:564
VALUE rb_class_inherited_p(VALUE scion, VALUE ascendant)
Determines if the given two modules are relatives.
Definition: object.c:1608
VALUE rb_mFileTest
FileTest module.
Definition: file.c:175
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition: object.c:120
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition: object.c:731
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
Definition: object.c:1161
VALUE rb_mComparable
Comparable module.
Definition: compar.c:19
VALUE rb_cFile
File class.
Definition: file.c:174
VALUE rb_cString
String class.
Definition: string.c:80
Encoding relates APIs.
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Queries the number of bytes of the character at the passed pointer.
Definition: encoding.c:1234
int rb_enc_get_index(VALUE obj)
Queries the index of the encoding of the passed object, if any.
Definition: encoding.c:979
int rb_filesystem_encindex(void)
Identical to rb_filesystem_encoding(), except it returns the encoding's index instead of the encoding...
Definition: encoding.c:1579
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
static char * rb_enc_left_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the left boundary of a character.
Definition: encoding.h:697
void rb_enc_copy(VALUE dst, VALUE src)
Destructively copies the encoding of the latter object to that of former one.
Definition: encoding.c:1192
rb_encoding * rb_enc_compatible(VALUE str1, VALUE str2)
Look for the "common" encoding between the two.
Definition: encoding.c:1176
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len, rb_encoding *enc)
Queries the code point of character pointed by the passed pointer.
Definition: encoding.c:1270
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
Definition: encoding.c:1724
int rb_enc_to_index(rb_encoding *enc)
Queries the index of the encoding.
Definition: encoding.c:197
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
Definition: encoding.c:1515
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
static bool rb_enc_asciicompat(rb_encoding *enc)
Queries if the passed encoding is in some sense compatible with ASCII.
Definition: encoding.h:782
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
Definition: encoding.c:1527
rb_encoding * rb_enc_from_index(int idx)
Identical to rb_find_encoding(), except it takes an encoding index instead of a Ruby object.
Definition: encoding.c:414
VALUE rb_enc_associate_index(VALUE obj, int encindex)
Identical to rb_enc_set_index(), except it additionally does contents fix-up depending on the passed ...
Definition: encoding.c:1038
rb_encoding * rb_enc_get(VALUE obj)
Identical to rb_enc_get_index(), except the return type.
Definition: encoding.c:1072
static OnigCodePoint rb_enc_mbc_to_codepoint(const char *p, const char *e, rb_encoding *enc)
Identical to rb_enc_codepoint(), except it assumes the passed character is not broken.
Definition: encoding.h:607
static const char * rb_enc_name(rb_encoding *enc)
Queries the (canonical) name of the passed encoding.
Definition: encoding.h:433
int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
Queries the code point of character pointed by the passed pointer.
Definition: encoding.c:1246
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_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
Definition: string.c:1182
int rb_enc_str_coderange(VALUE str)
Scans the passed string to collect its code range.
Definition: string.c:776
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
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition: vm_eval.c:1102
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
Definition: array.c:976
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
Definition: bignum.h:546
VALUE rb_integer_unpack(const void *words, size_t numwords, size_t wordsize, size_t nails, int flags)
Import an integer from a buffer.
Definition: bignum.c:3645
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
Definition: bignum.h:549
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
Definition: bignum.h:528
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition: error.h:294
VALUE rb_find_file(VALUE path)
Identical to rb_find_file_ext(), except it takes a feature name and is extension at once,...
Definition: file.c:6505
VALUE rb_file_s_absolute_path(int argc, const VALUE *argv)
Identical to rb_file_absolute_path(), except how arguments are passed.
Definition: file.c:4179
VALUE rb_str_encode_ospath(VALUE path)
Converts a string into an "OS Path" encoding, if any.
Definition: file.c:251
int rb_is_absolute_path(const char *path)
Queries if the given path is an absolute path.
Definition: file.c:6270
int rb_find_file_ext(VALUE *feature, const char *const *exts)
Resolves a feature's path.
Definition: file.c:6446
off_t rb_file_size(VALUE file)
Queries the file size of the given file.
Definition: file.c:2559
VALUE rb_file_s_expand_path(int argc, const VALUE *argv)
Identical to rb_file_expand_path(), except how arguments are passed.
Definition: file.c:4131
VALUE rb_file_directory_p(VALUE _, VALUE path)
Queries if the given path is either a directory, or a symlink that (potentially recursively) points t...
Definition: file.c:1645
VALUE rb_file_expand_path(VALUE fname, VALUE dname)
Identical to rb_file_absolute_path(), except it additionally understands ~.
Definition: file.c:4118
VALUE rb_file_dirname(VALUE fname)
Strips a file path's last component (and trailing separators if any).
Definition: file.c:4785
VALUE rb_file_absolute_path(VALUE fname, VALUE dname)
Maps a relative path to its absolute representation.
Definition: file.c:4172
int rb_path_check(const char *path)
This function is mysterious.
Definition: file.c:6347
VALUE rb_hash_aref(VALUE hash, VALUE key)
Queries the given key in the given hash table.
Definition: hash.c:2082
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Inserts or replaces ("upsert"s) the objects into the given hash table.
Definition: hash.c:2903
VALUE rb_hash_new(void)
Creates a new, empty hash object.
Definition: hash.c:1529
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
Definition: io.c:234
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
Definition: io.c:314
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
Definition: string.c:1350
VALUE rb_str_plus(VALUE lhs, VALUE rhs)
Generates a new string, concatenating the former to the latter.
Definition: string.c:2232
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition: string.c:3317
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
Definition: string.c:1540
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition: string.c:2821
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
Definition: string.c:10802
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
Definition: string.c:828
VALUE rb_str_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition: string.c:1808
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
Definition: string.c:3161
VALUE rb_str_replace(VALUE dst, VALUE src)
Replaces the contents of the former object with the stringised contents of the latter.
Definition: string.c:5841
VALUE rb_str_buf_cat2(VALUE, const char *)
Just another name of rb_str_cat_cstr.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition: string.c:3302
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
Definition: string.c:3039
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition: string.c:6456
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
Definition: string.c:3582
VALUE rb_str_new(const char *ptr, long len)
Allocates an instance of rb_cString.
Definition: string.c:918
VALUE rb_str_dup_frozen(VALUE)
Just another name of rb_str_new_frozen.
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
VALUE rb_str_resize(VALUE str, long len)
Overwrites the length of the string.
Definition: string.c:3056
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
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition: string.c:1506
#define RUBY_UBF_IO
A special UBF for blocking IO operations.
Definition: thread.h:382
VALUE rb_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
Definition: thread.c:1558
VALUE rb_time_nano_new(time_t sec, long nsec)
Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
Definition: time.c:2510
struct timespec rb_time_timespec(VALUE time)
Identical to rb_time_timeval(), except for return type.
Definition: time.c:2679
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
void rb_define_const(VALUE klass, const char *name, VALUE val)
Defines a Ruby level constant under a namespace.
Definition: variable.c:3253
VALUE rb_stat_new(const struct stat *st)
Constructs an instance of rb_cStat from the passed information.
Definition: file.c:547
VALUE rb_io_taint_check(VALUE obj)
Definition: io.c:771
#define GetOpenFile
This is an old name of RB_IO_POINTER.
Definition: io.h:343
#define FMODE_WRITABLE
The IO is opened for writing.
Definition: io.h:235
#define RB_IO_POINTER(obj, fp)
Queries the underlying IO pointer.
Definition: io.h:337
void rb_io_check_initialized(rb_io_t *fptr)
Asserts that the passed IO is initialised.
Definition: io.c:778
void rb_io_check_closed(rb_io_t *fptr)
This badly named function asserts that the passed IO is open.
Definition: io.c:786
void * rb_thread_call_without_gvl(void *(*func)(void *), void *data1, rb_unblock_function_t *ubf, void *data2)
Allows the passed function to run in parallel with other Ruby threads.
char * ruby_getcwd(void)
This is our own version of getcwd(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
Definition: util.c:548
#define strdup(s)
Just another name of ruby_strdup.
Definition: util.h:176
VALUE rb_sprintf(const char *fmt,...)
Ruby's extended sprintf(3).
Definition: sprintf.c:1201
VALUE rb_str_catf(VALUE dst, const char *fmt,...)
Identical to rb_sprintf(), except it renders the output to the specified object rather than creating ...
Definition: sprintf.c:1241
#define ALLOCA_N(type, n)
Definition: memory.h:286
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition: memory.h:161
#define PRI_MODET_PREFIX
A rb_sprintf() format prefix to be used for a mode_t parameter.
Definition: mode_t.h:38
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
Definition: mode_t.h:28
#define MODET2NUM
Converts an instance of rb_cNumeric into C's mode_t.
Definition: mode_t.h:33
#define OFFT2NUM
Converts a C's off_t into an instance of rb_cInteger.
Definition: off_t.h:33
#define NUM2OFFT
Converts an instance of rb_cNumeric into C's off_t.
Definition: off_t.h:44
char * rb_enc_path_next(const char *path, const char *end, rb_encoding *enc)
Returns a path component directly adjacent to the passed pointer.
Definition: file.c:3423
const char * ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
Our own encoding-aware version of extname.
Definition: file.c:4877
char * rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc)
Returns the last path component.
Definition: file.c:3471
char * rb_enc_path_end(const char *path, const char *end, rb_encoding *enc)
This just returns the passed end basically.
Definition: file.c:3505
char * rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc)
Seeks for non-prefix part of a pathname.
Definition: file.c:3437
const char * ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc)
Our own encoding-aware version of basename(3).
Definition: file.c:4630
#define RARRAY_LEN
Just another name of rb_array_len.
Definition: rarray.h:68
#define RARRAY_AREF(a, i)
Definition: rarray.h:588
#define DATA_PTR(obj)
Convenient getter macro.
Definition: rdata.h:71
#define RFILE(obj)
Convenient casting macro.
Definition: rfile.h:50
#define StringValue(v)
Ensures that the parameter object is a String.
Definition: rstring.h:72
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
Definition: rstring.h:82
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 StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition: rstring.h:95
#define RTYPEDDATA_DATA(v)
Convenient getter macro.
Definition: rtypeddata.h:102
#define RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
Definition: rtypeddata.h:79
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
Definition: rtypeddata.h:507
#define TypedData_Wrap_Struct(klass, data_type, sval)
Converts sval, a pointer to your struct, into a Ruby object.
Definition: rtypeddata.h:441
VALUE rb_get_path(VALUE obj)
Converts an object to a path.
Definition: file.c:245
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
Definition: variable.c:309
#define FilePathValue(v)
Ensures that the parameter object is a path.
Definition: ruby.h:90
#define FilePathStringValue(v)
Definition: ruby.h:103
VALUE rb_get_path_no_checksafe(VALUE)
Definition: file.c:239
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition: stdarg.h:35
#define ANYARGS
Functions declared using this macro take arbitrary arguments, including void.
Definition: stdarg.h:64
This is the struct that holds necessary info for a struct.
Definition: rtypeddata.h:190
Ruby's IO, metadata and buffers.
Definition: io.h:95
int fd
file descriptor.
Definition: io.h:104
VALUE pathv
pathname for file
Definition: io.h:116
int mode
mode flags: FMODE_XXXs
Definition: io.h:107
Definition: file.c:2929
#define UIDT2NUM
Converts a C's uid_t into an instance of rb_cInteger.
Definition: uid_t.h:28
#define NUM2UIDT
Converts an instance of rb_cNumeric into C's uid_t.
Definition: uid_t.h:33
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition: value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition: value_type.h:375