Ruby  2.7.2p137(2020-10-01revision5445e0435260b449decf2ac16f9d09bae3cafe72)
pathname.c
Go to the documentation of this file.
1 #include "ruby.h"
2 #include "ruby/encoding.h"
3 
4 static VALUE rb_cPathname;
5 static ID id_ENOTDIR;
6 static ID id_at_path;
7 static ID id_atime;
8 static ID id_base;
9 static ID id_basename;
10 static ID id_binread;
11 static ID id_binwrite;
12 static ID id_birthtime;
13 static ID id_blockdev_p;
14 static ID id_chardev_p;
15 static ID id_chmod;
16 static ID id_chown;
17 static ID id_ctime;
18 static ID id_directory_p;
19 static ID id_dirname;
20 static ID id_empty_p;
21 static ID id_entries;
22 static ID id_executable_p;
23 static ID id_executable_real_p;
24 static ID id_exist_p;
25 static ID id_expand_path;
26 static ID id_extname;
27 static ID id_file_p;
28 static ID id_fnmatch;
29 static ID id_foreach;
30 static ID id_ftype;
31 static ID id_getwd;
32 static ID id_glob;
33 static ID id_grpowned_p;
34 static ID id_lchmod;
35 static ID id_lchown;
36 static ID id_link;
37 static ID id_lstat;
38 static ID id_mkdir;
39 static ID id_mtime;
40 static ID id_open;
41 static ID id_owned_p;
42 static ID id_pipe_p;
43 static ID id_read;
44 static ID id_readable_p;
45 static ID id_readable_real_p;
46 static ID id_readlines;
47 static ID id_readlink;
48 static ID id_realdirpath;
49 static ID id_realpath;
50 static ID id_rename;
51 static ID id_rmdir;
52 static ID id_setgid_p;
53 static ID id_setuid_p;
54 static ID id_size;
55 static ID id_size_p;
56 static ID id_socket_p;
57 static ID id_split;
58 static ID id_stat;
59 static ID id_sticky_p;
60 static ID id_sub;
61 static ID id_symlink;
62 static ID id_symlink_p;
63 static ID id_sysopen;
64 static ID id_to_path;
65 static ID id_truncate;
66 static ID id_unlink;
67 static ID id_utime;
68 static ID id_world_readable_p;
69 static ID id_world_writable_p;
70 static ID id_writable_p;
71 static ID id_writable_real_p;
72 static ID id_write;
73 static ID id_zero_p;
74 
75 static VALUE
76 get_strpath(VALUE obj)
77 {
78  VALUE strpath;
79  strpath = rb_ivar_get(obj, id_at_path);
80  if (!RB_TYPE_P(strpath, T_STRING))
81  rb_raise(rb_eTypeError, "unexpected @path");
82  return strpath;
83 }
84 
85 static void
86 set_strpath(VALUE obj, VALUE val)
87 {
88  rb_ivar_set(obj, id_at_path, val);
89 }
90 
91 /*
92  * Create a Pathname object from the given String (or String-like object).
93  * If +path+ contains a NULL character (<tt>\0</tt>), an ArgumentError is raised.
94  */
95 static VALUE
96 path_initialize(VALUE self, VALUE arg)
97 {
98  VALUE str;
99  if (RB_TYPE_P(arg, T_STRING)) {
100  str = arg;
101  }
102  else {
103  str = rb_check_funcall(arg, id_to_path, 0, NULL);
104  if (str == Qundef)
105  str = arg;
106  StringValue(str);
107  }
108  if (memchr(RSTRING_PTR(str), '\0', RSTRING_LEN(str)))
109  rb_raise(rb_eArgError, "pathname contains null byte");
110  str = rb_obj_dup(str);
111 
112  set_strpath(self, str);
113  return self;
114 }
115 
116 /*
117  * call-seq:
118  * pathname.freeze -> obj
119  *
120  * Freezes this Pathname.
121  *
122  * See Object.freeze.
123  */
124 static VALUE
125 path_freeze(VALUE self)
126 {
127  rb_call_super(0, 0);
128  rb_str_freeze(get_strpath(self));
129  return self;
130 }
131 
132 /*
133  * call-seq:
134  * pathname.taint -> obj
135  *
136  * Returns pathname. This method is deprecated and will be removed in Ruby 3.2.
137  */
138 static VALUE
139 path_taint(VALUE self)
140 {
141  rb_warning("Pathname#taint is deprecated and will be removed in Ruby 3.2.");
142  return self;
143 }
144 
145 /*
146  * call-seq:
147  * pathname.untaint -> obj
148  *
149  * Returns pathname. This method is deprecated and will be removed in Ruby 3.2.
150  */
151 static VALUE
152 path_untaint(VALUE self)
153 {
154  rb_warning("Pathname#untaint is deprecated and will be removed in Ruby 3.2.");
155  return self;
156 }
157 
158 /*
159  * Compare this pathname with +other+. The comparison is string-based.
160  * Be aware that two different paths (<tt>foo.txt</tt> and <tt>./foo.txt</tt>)
161  * can refer to the same file.
162  */
163 static VALUE
164 path_eq(VALUE self, VALUE other)
165 {
166  if (!rb_obj_is_kind_of(other, rb_cPathname))
167  return Qfalse;
168  return rb_str_equal(get_strpath(self), get_strpath(other));
169 }
170 
171 /*
172  * Provides a case-sensitive comparison operator for pathnames.
173  *
174  * Pathname.new('/usr') <=> Pathname.new('/usr/bin')
175  * #=> -1
176  * Pathname.new('/usr/bin') <=> Pathname.new('/usr/bin')
177  * #=> 0
178  * Pathname.new('/usr/bin') <=> Pathname.new('/USR/BIN')
179  * #=> 1
180  *
181  * It will return +-1+, +0+ or +1+ depending on the value of the left argument
182  * relative to the right argument. Or it will return +nil+ if the arguments
183  * are not comparable.
184  */
185 static VALUE
186 path_cmp(VALUE self, VALUE other)
187 {
188  VALUE s1, s2;
189  char *p1, *p2;
190  char *e1, *e2;
191  if (!rb_obj_is_kind_of(other, rb_cPathname))
192  return Qnil;
193  s1 = get_strpath(self);
194  s2 = get_strpath(other);
195  p1 = RSTRING_PTR(s1);
196  p2 = RSTRING_PTR(s2);
197  e1 = p1 + RSTRING_LEN(s1);
198  e2 = p2 + RSTRING_LEN(s2);
199  while (p1 < e1 && p2 < e2) {
200  int c1, c2;
201  c1 = (unsigned char)*p1++;
202  c2 = (unsigned char)*p2++;
203  if (c1 == '/') c1 = '\0';
204  if (c2 == '/') c2 = '\0';
205  if (c1 != c2) {
206  if (c1 < c2)
207  return INT2FIX(-1);
208  else
209  return INT2FIX(1);
210  }
211  }
212  if (p1 < e1)
213  return INT2FIX(1);
214  if (p2 < e2)
215  return INT2FIX(-1);
216  return INT2FIX(0);
217 }
218 
219 #ifndef ST2FIX
220 #define ST2FIX(h) LONG2FIX((long)(h))
221 #endif
222 
223 /* :nodoc: */
224 static VALUE
225 path_hash(VALUE self)
226 {
227  return ST2FIX(rb_str_hash(get_strpath(self)));
228 }
229 
230 /*
231  * call-seq:
232  * pathname.to_s -> string
233  * pathname.to_path -> string
234  *
235  * Return the path as a String.
236  *
237  * to_path is implemented so Pathname objects are usable with File.open, etc.
238  */
239 static VALUE
240 path_to_s(VALUE self)
241 {
242  return rb_obj_dup(get_strpath(self));
243 }
244 
245 /* :nodoc: */
246 static VALUE
247 path_inspect(VALUE self)
248 {
249  const char *c = rb_obj_classname(self);
250  VALUE str = get_strpath(self);
251  return rb_sprintf("#<%s:%"PRIsVALUE">", c, str);
252 }
253 
254 /*
255  * Return a pathname which is substituted by String#sub.
256  *
257  * path1 = Pathname.new('/usr/bin/perl')
258  * path1.sub('perl', 'ruby')
259  * #=> #<Pathname:/usr/bin/ruby>
260  */
261 static VALUE
262 path_sub(int argc, VALUE *argv, VALUE self)
263 {
264  VALUE str = get_strpath(self);
265 
266  if (rb_block_given_p()) {
267  str = rb_block_call(str, id_sub, argc, argv, 0, 0);
268  }
269  else {
270  str = rb_funcallv(str, id_sub, argc, argv);
271  }
272  return rb_class_new_instance(1, &str, rb_obj_class(self));
273 }
274 
275 /*
276  * Return a pathname with +repl+ added as a suffix to the basename.
277  *
278  * If self has no extension part, +repl+ is appended.
279  *
280  * Pathname.new('/usr/bin/shutdown').sub_ext('.rb')
281  * #=> #<Pathname:/usr/bin/shutdown.rb>
282  */
283 static VALUE
284 path_sub_ext(VALUE self, VALUE repl)
285 {
286  VALUE str = get_strpath(self);
287  VALUE str2;
288  long extlen;
289  const char *ext;
290  const char *p;
291 
292  StringValue(repl);
293  p = RSTRING_PTR(str);
294  extlen = RSTRING_LEN(str);
295  ext = ruby_enc_find_extname(p, &extlen, rb_enc_get(str));
296  if (ext == NULL) {
297  ext = p + RSTRING_LEN(str);
298  }
299  else if (extlen <= 1) {
300  ext += extlen;
301  }
302  str2 = rb_str_subseq(str, 0, ext-p);
303  rb_str_append(str2, repl);
304  return rb_class_new_instance(1, &str2, rb_obj_class(self));
305 }
306 
307 /* Facade for File */
308 
309 /*
310  * Returns the real (absolute) pathname for +self+ in the actual
311  * filesystem.
312  *
313  * Does not contain symlinks or useless dots, +..+ and +.+.
314  *
315  * All components of the pathname must exist when this method is
316  * called.
317  *
318  */
319 static VALUE
320 path_realpath(int argc, VALUE *argv, VALUE self)
321 {
322  VALUE basedir, str;
323  rb_scan_args(argc, argv, "01", &basedir);
324  str = rb_funcall(rb_cFile, id_realpath, 2, get_strpath(self), basedir);
325  return rb_class_new_instance(1, &str, rb_obj_class(self));
326 }
327 
328 /*
329  * Returns the real (absolute) pathname of +self+ in the actual filesystem.
330  *
331  * Does not contain symlinks or useless dots, +..+ and +.+.
332  *
333  * The last component of the real pathname can be nonexistent.
334  */
335 static VALUE
336 path_realdirpath(int argc, VALUE *argv, VALUE self)
337 {
338  VALUE basedir, str;
339  rb_scan_args(argc, argv, "01", &basedir);
340  str = rb_funcall(rb_cFile, id_realdirpath, 2, get_strpath(self), basedir);
341  return rb_class_new_instance(1, &str, rb_obj_class(self));
342 }
343 
344 /*
345  * call-seq:
346  * pathname.each_line {|line| ... }
347  * pathname.each_line(sep=$/ [, open_args]) {|line| block } -> nil
348  * pathname.each_line(limit [, open_args]) {|line| block } -> nil
349  * pathname.each_line(sep, limit [, open_args]) {|line| block } -> nil
350  * pathname.each_line(...) -> an_enumerator
351  *
352  * Iterates over each line in the file and yields a String object for each.
353  */
354 static VALUE
355 path_each_line(int argc, VALUE *argv, VALUE self)
356 {
357  VALUE args[4];
358  int n;
359 
360  args[0] = get_strpath(self);
361  n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
362  if (rb_block_given_p()) {
363  return rb_block_call(rb_cFile, id_foreach, 1+n, args, 0, 0);
364  }
365  else {
366  return rb_funcallv(rb_cFile, id_foreach, 1+n, args);
367  }
368 }
369 
370 /*
371  * call-seq:
372  * pathname.read([length [, offset]]) -> string
373  * pathname.read([length [, offset]], open_args) -> string
374  *
375  * Returns all data from the file, or the first +N+ bytes if specified.
376  *
377  * See File.read.
378  *
379  */
380 static VALUE
381 path_read(int argc, VALUE *argv, VALUE self)
382 {
383  VALUE args[4];
384  int n;
385 
386  args[0] = get_strpath(self);
387  n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
388  return rb_funcallv_kw(rb_cFile, id_read, 1+n, args, RB_PASS_CALLED_KEYWORDS);
389 }
390 
391 /*
392  * call-seq:
393  * pathname.binread([length [, offset]]) -> string
394  *
395  * Returns all the bytes from the file, or the first +N+ if specified.
396  *
397  * See File.binread.
398  *
399  */
400 static VALUE
401 path_binread(int argc, VALUE *argv, VALUE self)
402 {
403  VALUE args[3];
404  int n;
405 
406  args[0] = get_strpath(self);
407  n = rb_scan_args(argc, argv, "02", &args[1], &args[2]);
408  return rb_funcallv(rb_cFile, id_binread, 1+n, args);
409 }
410 
411 /*
412  * call-seq:
413  * pathname.write(string, [offset] ) => fixnum
414  * pathname.write(string, [offset], open_args ) => fixnum
415  *
416  * Writes +contents+ to the file.
417  *
418  * See File.write.
419  *
420  */
421 static VALUE
422 path_write(int argc, VALUE *argv, VALUE self)
423 {
424  VALUE args[4];
425  int n;
426 
427  args[0] = get_strpath(self);
428  n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
429  return rb_funcallv_kw(rb_cFile, id_write, 1+n, args, RB_PASS_CALLED_KEYWORDS);
430 }
431 
432 /*
433  * call-seq:
434  * pathname.binwrite(string, [offset] ) => fixnum
435  * pathname.binwrite(string, [offset], open_args ) => fixnum
436  *
437  * Writes +contents+ to the file, opening it in binary mode.
438  *
439  * See File.binwrite.
440  *
441  */
442 static VALUE
443 path_binwrite(int argc, VALUE *argv, VALUE self)
444 {
445  VALUE args[4];
446  int n;
447 
448  args[0] = get_strpath(self);
449  n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
450  return rb_funcallv_kw(rb_cFile, id_binwrite, 1+n, args, RB_PASS_CALLED_KEYWORDS);
451 }
452 
453 /*
454  * call-seq:
455  * pathname.readlines(sep=$/ [, open_args]) -> array
456  * pathname.readlines(limit [, open_args]) -> array
457  * pathname.readlines(sep, limit [, open_args]) -> array
458  *
459  * Returns all the lines from the file.
460  *
461  * See File.readlines.
462  *
463  */
464 static VALUE
465 path_readlines(int argc, VALUE *argv, VALUE self)
466 {
467  VALUE args[4];
468  int n;
469 
470  args[0] = get_strpath(self);
471  n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
472  return rb_funcallv_kw(rb_cFile, id_readlines, 1+n, args, RB_PASS_CALLED_KEYWORDS);
473 }
474 
475 /*
476  * call-seq:
477  * pathname.sysopen([mode, [perm]]) -> fixnum
478  *
479  * See IO.sysopen.
480  *
481  */
482 static VALUE
483 path_sysopen(int argc, VALUE *argv, VALUE self)
484 {
485  VALUE args[3];
486  int n;
487 
488  args[0] = get_strpath(self);
489  n = rb_scan_args(argc, argv, "02", &args[1], &args[2]);
490  return rb_funcallv(rb_cIO, id_sysopen, 1+n, args);
491 }
492 
493 /*
494  * call-seq:
495  * pathname.atime -> time
496  *
497  * Returns the last access time for the file.
498  *
499  * See File.atime.
500  */
501 static VALUE
502 path_atime(VALUE self)
503 {
504  return rb_funcall(rb_cFile, id_atime, 1, get_strpath(self));
505 }
506 
507 #if defined(HAVE_RB_FILE_S_BIRTHTIME)
508 /*
509  * call-seq:
510  * pathname.birthtime -> time
511  *
512  * Returns the birth time for the file.
513  * If the platform doesn't have birthtime, raises NotImplementedError.
514  *
515  * See File.birthtime.
516  */
517 static VALUE
518 path_birthtime(VALUE self)
519 {
520  return rb_funcall(rb_cFile, id_birthtime, 1, get_strpath(self));
521 }
522 #else
523 /* check at compilation time for `respond_to?` */
524 # define path_birthtime rb_f_notimplement
525 #endif
526 
527 /*
528  * call-seq:
529  * pathname.ctime -> time
530  *
531  * Returns the last change time, using directory information, not the file itself.
532  *
533  * See File.ctime.
534  */
535 static VALUE
536 path_ctime(VALUE self)
537 {
538  return rb_funcall(rb_cFile, id_ctime, 1, get_strpath(self));
539 }
540 
541 /*
542  * call-seq:
543  * pathname.mtime -> time
544  *
545  * Returns the last modified time of the file.
546  *
547  * See File.mtime.
548  */
549 static VALUE
550 path_mtime(VALUE self)
551 {
552  return rb_funcall(rb_cFile, id_mtime, 1, get_strpath(self));
553 }
554 
555 /*
556  * call-seq:
557  * pathname.chmod -> integer
558  *
559  * Changes file permissions.
560  *
561  * See File.chmod.
562  */
563 static VALUE
564 path_chmod(VALUE self, VALUE mode)
565 {
566  return rb_funcall(rb_cFile, id_chmod, 2, mode, get_strpath(self));
567 }
568 
569 /*
570  * call-seq:
571  * pathname.lchmod -> integer
572  *
573  * Same as Pathname.chmod, but does not follow symbolic links.
574  *
575  * See File.lchmod.
576  */
577 static VALUE
578 path_lchmod(VALUE self, VALUE mode)
579 {
580  return rb_funcall(rb_cFile, id_lchmod, 2, mode, get_strpath(self));
581 }
582 
583 /*
584  * call-seq:
585  * pathname.chown -> integer
586  *
587  * Change owner and group of the file.
588  *
589  * See File.chown.
590  */
591 static VALUE
592 path_chown(VALUE self, VALUE owner, VALUE group)
593 {
594  return rb_funcall(rb_cFile, id_chown, 3, owner, group, get_strpath(self));
595 }
596 
597 /*
598  * call-seq:
599  * pathname.lchown -> integer
600  *
601  * Same as Pathname.chown, but does not follow symbolic links.
602  *
603  * See File.lchown.
604  */
605 static VALUE
606 path_lchown(VALUE self, VALUE owner, VALUE group)
607 {
608  return rb_funcall(rb_cFile, id_lchown, 3, owner, group, get_strpath(self));
609 }
610 
611 /*
612  * call-seq:
613  * pathname.fnmatch(pattern, [flags]) -> string
614  * pathname.fnmatch?(pattern, [flags]) -> string
615  *
616  * Return +true+ if the receiver matches the given pattern.
617  *
618  * See File.fnmatch.
619  */
620 static VALUE
621 path_fnmatch(int argc, VALUE *argv, VALUE self)
622 {
623  VALUE str = get_strpath(self);
624  VALUE pattern, flags;
625  if (rb_scan_args(argc, argv, "11", &pattern, &flags) == 1)
626  return rb_funcall(rb_cFile, id_fnmatch, 2, pattern, str);
627  else
628  return rb_funcall(rb_cFile, id_fnmatch, 3, pattern, str, flags);
629 }
630 
631 /*
632  * call-seq:
633  * pathname.ftype -> string
634  *
635  * Returns "type" of file ("file", "directory", etc).
636  *
637  * See File.ftype.
638  */
639 static VALUE
640 path_ftype(VALUE self)
641 {
642  return rb_funcall(rb_cFile, id_ftype, 1, get_strpath(self));
643 }
644 
645 /*
646  * call-seq:
647  * pathname.make_link(old)
648  *
649  * Creates a hard link at _pathname_.
650  *
651  * See File.link.
652  */
653 static VALUE
654 path_make_link(VALUE self, VALUE old)
655 {
656  return rb_funcall(rb_cFile, id_link, 2, old, get_strpath(self));
657 }
658 
659 /*
660  * Opens the file for reading or writing.
661  *
662  * See File.open.
663  */
664 static VALUE
665 path_open(int argc, VALUE *argv, VALUE self)
666 {
667  VALUE args[4];
668  int n;
669 
670  args[0] = get_strpath(self);
671  n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
672  if (rb_block_given_p()) {
673  return rb_block_call_kw(rb_cFile, id_open, 1+n, args, 0, 0, RB_PASS_CALLED_KEYWORDS);
674  }
675  else {
676  return rb_funcallv_kw(rb_cFile, id_open, 1+n, args, RB_PASS_CALLED_KEYWORDS);
677  }
678 }
679 
680 /*
681  * Read symbolic link.
682  *
683  * See File.readlink.
684  */
685 static VALUE
686 path_readlink(VALUE self)
687 {
688  VALUE str;
689  str = rb_funcall(rb_cFile, id_readlink, 1, get_strpath(self));
690  return rb_class_new_instance(1, &str, rb_obj_class(self));
691 }
692 
693 /*
694  * Rename the file.
695  *
696  * See File.rename.
697  */
698 static VALUE
699 path_rename(VALUE self, VALUE to)
700 {
701  return rb_funcall(rb_cFile, id_rename, 2, get_strpath(self), to);
702 }
703 
704 /*
705  * Returns a File::Stat object.
706  *
707  * See File.stat.
708  */
709 static VALUE
710 path_stat(VALUE self)
711 {
712  return rb_funcall(rb_cFile, id_stat, 1, get_strpath(self));
713 }
714 
715 /*
716  * See File.lstat.
717  */
718 static VALUE
719 path_lstat(VALUE self)
720 {
721  return rb_funcall(rb_cFile, id_lstat, 1, get_strpath(self));
722 }
723 
724 /*
725  * call-seq:
726  * pathname.make_symlink(old)
727  *
728  * Creates a symbolic link.
729  *
730  * See File.symlink.
731  */
732 static VALUE
733 path_make_symlink(VALUE self, VALUE old)
734 {
735  return rb_funcall(rb_cFile, id_symlink, 2, old, get_strpath(self));
736 }
737 
738 /*
739  * Truncates the file to +length+ bytes.
740  *
741  * See File.truncate.
742  */
743 static VALUE
744 path_truncate(VALUE self, VALUE length)
745 {
746  return rb_funcall(rb_cFile, id_truncate, 2, get_strpath(self), length);
747 }
748 
749 /*
750  * Update the access and modification times of the file.
751  *
752  * See File.utime.
753  */
754 static VALUE
755 path_utime(VALUE self, VALUE atime, VALUE mtime)
756 {
757  return rb_funcall(rb_cFile, id_utime, 3, atime, mtime, get_strpath(self));
758 }
759 
760 /*
761  * Returns the last component of the path.
762  *
763  * See File.basename.
764  */
765 static VALUE
766 path_basename(int argc, VALUE *argv, VALUE self)
767 {
768  VALUE str = get_strpath(self);
769  VALUE fext;
770  if (rb_scan_args(argc, argv, "01", &fext) == 0)
771  str = rb_funcall(rb_cFile, id_basename, 1, str);
772  else
773  str = rb_funcall(rb_cFile, id_basename, 2, str, fext);
774  return rb_class_new_instance(1, &str, rb_obj_class(self));
775 }
776 
777 /*
778  * Returns all but the last component of the path.
779  *
780  * See File.dirname.
781  */
782 static VALUE
783 path_dirname(VALUE self)
784 {
785  VALUE str = get_strpath(self);
786  str = rb_funcall(rb_cFile, id_dirname, 1, str);
787  return rb_class_new_instance(1, &str, rb_obj_class(self));
788 }
789 
790 /*
791  * Returns the file's extension.
792  *
793  * See File.extname.
794  */
795 static VALUE
796 path_extname(VALUE self)
797 {
798  VALUE str = get_strpath(self);
799  return rb_funcall(rb_cFile, id_extname, 1, str);
800 }
801 
802 /*
803  * Returns the absolute path for the file.
804  *
805  * See File.expand_path.
806  */
807 static VALUE
808 path_expand_path(int argc, VALUE *argv, VALUE self)
809 {
810  VALUE str = get_strpath(self);
811  VALUE dname;
812  if (rb_scan_args(argc, argv, "01", &dname) == 0)
813  str = rb_funcall(rb_cFile, id_expand_path, 1, str);
814  else
815  str = rb_funcall(rb_cFile, id_expand_path, 2, str, dname);
816  return rb_class_new_instance(1, &str, rb_obj_class(self));
817 }
818 
819 /*
820  * Returns the #dirname and the #basename in an Array.
821  *
822  * See File.split.
823  */
824 static VALUE
825 path_split(VALUE self)
826 {
827  VALUE str = get_strpath(self);
828  VALUE ary, dirname, basename;
829  ary = rb_funcall(rb_cFile, id_split, 1, str);
830  ary = rb_check_array_type(ary);
831  dirname = rb_ary_entry(ary, 0);
832  basename = rb_ary_entry(ary, 1);
833  dirname = rb_class_new_instance(1, &dirname, rb_obj_class(self));
835  return rb_ary_new3(2, dirname, basename);
836 }
837 
838 /*
839  * See FileTest.blockdev?.
840  */
841 static VALUE
842 path_blockdev_p(VALUE self)
843 {
844  return rb_funcall(rb_mFileTest, id_blockdev_p, 1, get_strpath(self));
845 }
846 
847 /*
848  * See FileTest.chardev?.
849  */
850 static VALUE
851 path_chardev_p(VALUE self)
852 {
853  return rb_funcall(rb_mFileTest, id_chardev_p, 1, get_strpath(self));
854 }
855 
856 /*
857  * See FileTest.executable?.
858  */
859 static VALUE
860 path_executable_p(VALUE self)
861 {
862  return rb_funcall(rb_mFileTest, id_executable_p, 1, get_strpath(self));
863 }
864 
865 /*
866  * See FileTest.executable_real?.
867  */
868 static VALUE
869 path_executable_real_p(VALUE self)
870 {
871  return rb_funcall(rb_mFileTest, id_executable_real_p, 1, get_strpath(self));
872 }
873 
874 /*
875  * See FileTest.exist?.
876  */
877 static VALUE
878 path_exist_p(VALUE self)
879 {
880  return rb_funcall(rb_mFileTest, id_exist_p, 1, get_strpath(self));
881 }
882 
883 /*
884  * See FileTest.grpowned?.
885  */
886 static VALUE
887 path_grpowned_p(VALUE self)
888 {
889  return rb_funcall(rb_mFileTest, id_grpowned_p, 1, get_strpath(self));
890 }
891 
892 /*
893  * See FileTest.directory?.
894  */
895 static VALUE
896 path_directory_p(VALUE self)
897 {
898  return rb_funcall(rb_mFileTest, id_directory_p, 1, get_strpath(self));
899 }
900 
901 /*
902  * See FileTest.file?.
903  */
904 static VALUE
905 path_file_p(VALUE self)
906 {
907  return rb_funcall(rb_mFileTest, id_file_p, 1, get_strpath(self));
908 }
909 
910 /*
911  * See FileTest.pipe?.
912  */
913 static VALUE
914 path_pipe_p(VALUE self)
915 {
916  return rb_funcall(rb_mFileTest, id_pipe_p, 1, get_strpath(self));
917 }
918 
919 /*
920  * See FileTest.socket?.
921  */
922 static VALUE
923 path_socket_p(VALUE self)
924 {
925  return rb_funcall(rb_mFileTest, id_socket_p, 1, get_strpath(self));
926 }
927 
928 /*
929  * See FileTest.owned?.
930  */
931 static VALUE
932 path_owned_p(VALUE self)
933 {
934  return rb_funcall(rb_mFileTest, id_owned_p, 1, get_strpath(self));
935 }
936 
937 /*
938  * See FileTest.readable?.
939  */
940 static VALUE
941 path_readable_p(VALUE self)
942 {
943  return rb_funcall(rb_mFileTest, id_readable_p, 1, get_strpath(self));
944 }
945 
946 /*
947  * See FileTest.world_readable?.
948  */
949 static VALUE
950 path_world_readable_p(VALUE self)
951 {
952  return rb_funcall(rb_mFileTest, id_world_readable_p, 1, get_strpath(self));
953 }
954 
955 /*
956  * See FileTest.readable_real?.
957  */
958 static VALUE
959 path_readable_real_p(VALUE self)
960 {
961  return rb_funcall(rb_mFileTest, id_readable_real_p, 1, get_strpath(self));
962 }
963 
964 /*
965  * See FileTest.setuid?.
966  */
967 static VALUE
968 path_setuid_p(VALUE self)
969 {
970  return rb_funcall(rb_mFileTest, id_setuid_p, 1, get_strpath(self));
971 }
972 
973 /*
974  * See FileTest.setgid?.
975  */
976 static VALUE
977 path_setgid_p(VALUE self)
978 {
979  return rb_funcall(rb_mFileTest, id_setgid_p, 1, get_strpath(self));
980 }
981 
982 /*
983  * See FileTest.size.
984  */
985 static VALUE
986 path_size(VALUE self)
987 {
988  return rb_funcall(rb_mFileTest, id_size, 1, get_strpath(self));
989 }
990 
991 /*
992  * See FileTest.size?.
993  */
994 static VALUE
995 path_size_p(VALUE self)
996 {
997  return rb_funcall(rb_mFileTest, id_size_p, 1, get_strpath(self));
998 }
999 
1000 /*
1001  * See FileTest.sticky?.
1002  */
1003 static VALUE
1004 path_sticky_p(VALUE self)
1005 {
1006  return rb_funcall(rb_mFileTest, id_sticky_p, 1, get_strpath(self));
1007 }
1008 
1009 /*
1010  * See FileTest.symlink?.
1011  */
1012 static VALUE
1013 path_symlink_p(VALUE self)
1014 {
1015  return rb_funcall(rb_mFileTest, id_symlink_p, 1, get_strpath(self));
1016 }
1017 
1018 /*
1019  * See FileTest.writable?.
1020  */
1021 static VALUE
1022 path_writable_p(VALUE self)
1023 {
1024  return rb_funcall(rb_mFileTest, id_writable_p, 1, get_strpath(self));
1025 }
1026 
1027 /*
1028  * See FileTest.world_writable?.
1029  */
1030 static VALUE
1031 path_world_writable_p(VALUE self)
1032 {
1033  return rb_funcall(rb_mFileTest, id_world_writable_p, 1, get_strpath(self));
1034 }
1035 
1036 /*
1037  * See FileTest.writable_real?.
1038  */
1039 static VALUE
1040 path_writable_real_p(VALUE self)
1041 {
1042  return rb_funcall(rb_mFileTest, id_writable_real_p, 1, get_strpath(self));
1043 }
1044 
1045 /*
1046  * See FileTest.zero?.
1047  */
1048 static VALUE
1049 path_zero_p(VALUE self)
1050 {
1051  return rb_funcall(rb_mFileTest, id_zero_p, 1, get_strpath(self));
1052 }
1053 
1054 /*
1055  * Tests the file is empty.
1056  *
1057  * See Dir#empty? and FileTest.empty?.
1058  */
1059 static VALUE
1060 path_empty_p(VALUE self)
1061 {
1062 
1063  VALUE path = get_strpath(self);
1064  if (RTEST(rb_funcall(rb_mFileTest, id_directory_p, 1, path)))
1065  return rb_funcall(rb_cDir, id_empty_p, 1, path);
1066  else
1067  return rb_funcall(rb_mFileTest, id_empty_p, 1, path);
1068 }
1069 
1070 static VALUE
1071 s_glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, klass))
1072 {
1073  return rb_yield(rb_class_new_instance(1, &elt, klass));
1074 }
1075 
1076 /*
1077  * Returns or yields Pathname objects.
1078  *
1079  * Pathname.glob("lib/i*.rb")
1080  * #=> [#<Pathname:lib/ipaddr.rb>, #<Pathname:lib/irb.rb>]
1081  *
1082  * See Dir.glob.
1083  */
1084 static VALUE
1085 path_s_glob(int argc, VALUE *argv, VALUE klass)
1086 {
1087  VALUE args[3];
1088  int n;
1089 
1090  n = rb_scan_args(argc, argv, "12", &args[0], &args[1], &args[2]);
1091  if (rb_block_given_p()) {
1092  return rb_block_call_kw(rb_cDir, id_glob, n, args, s_glob_i, klass, RB_PASS_CALLED_KEYWORDS);
1093  }
1094  else {
1095  VALUE ary;
1096  long i;
1097  ary = rb_funcallv_kw(rb_cDir, id_glob, n, args, RB_PASS_CALLED_KEYWORDS);
1098  ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
1099  for (i = 0; i < RARRAY_LEN(ary); i++) {
1100  VALUE elt = RARRAY_AREF(ary, i);
1101  elt = rb_class_new_instance(1, &elt, klass);
1102  rb_ary_store(ary, i, elt);
1103  }
1104  return ary;
1105  }
1106 }
1107 
1108 static VALUE
1109 glob_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, self))
1110 {
1111  elt = rb_funcall(self, '+', 1, elt);
1112  return rb_yield(elt);
1113 }
1114 
1115 /*
1116  * Returns or yields Pathname objects.
1117  *
1118  * Pathname("ruby-2.4.2").glob("R*.md")
1119  * #=> [#<Pathname:ruby-2.4.2/README.md>, #<Pathname:ruby-2.4.2/README.ja.md>]
1120  *
1121  * See Dir.glob.
1122  * This method uses the +base+ keyword argument of Dir.glob.
1123  */
1124 static VALUE
1125 path_glob(int argc, VALUE *argv, VALUE self)
1126 {
1127  VALUE args[3];
1128  int n;
1129 
1130  n = rb_scan_args(argc, argv, "11", &args[0], &args[1]);
1131  if (n == 1)
1132  args[1] = INT2FIX(0);
1133 
1134  args[2] = rb_hash_new();
1135  rb_hash_aset(args[2], ID2SYM(id_base), get_strpath(self));
1136 
1137  n = 3;
1138 
1139  if (rb_block_given_p()) {
1140  return rb_block_call_kw(rb_cDir, id_glob, n, args, glob_i, self, RB_PASS_KEYWORDS);
1141  }
1142  else {
1143  VALUE ary;
1144  long i;
1145  ary = rb_funcallv_kw(rb_cDir, id_glob, n, args, RB_PASS_KEYWORDS);
1146  ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
1147  for (i = 0; i < RARRAY_LEN(ary); i++) {
1148  VALUE elt = RARRAY_AREF(ary, i);
1149  elt = rb_funcall(self, '+', 1, elt);
1150  rb_ary_store(ary, i, elt);
1151  }
1152  return ary;
1153  }
1154 }
1155 
1156 /*
1157  * Returns the current working directory as a Pathname.
1158  *
1159  * Pathname.getwd
1160  * #=> #<Pathname:/home/zzak/projects/ruby>
1161  *
1162  * See Dir.getwd.
1163  */
1164 static VALUE
1165 path_s_getwd(VALUE klass)
1166 {
1167  VALUE str;
1168  str = rb_funcall(rb_cDir, id_getwd, 0);
1169  return rb_class_new_instance(1, &str, klass);
1170 }
1171 
1172 /*
1173  * Return the entries (files and subdirectories) in the directory, each as a
1174  * Pathname object.
1175  *
1176  * The results contains just the names in the directory, without any trailing
1177  * slashes or recursive look-up.
1178  *
1179  * pp Pathname.new('/usr/local').entries
1180  * #=> [#<Pathname:share>,
1181  * # #<Pathname:lib>,
1182  * # #<Pathname:..>,
1183  * # #<Pathname:include>,
1184  * # #<Pathname:etc>,
1185  * # #<Pathname:bin>,
1186  * # #<Pathname:man>,
1187  * # #<Pathname:games>,
1188  * # #<Pathname:.>,
1189  * # #<Pathname:sbin>,
1190  * # #<Pathname:src>]
1191  *
1192  * The result may contain the current directory <code>#<Pathname:.></code> and
1193  * the parent directory <code>#<Pathname:..></code>.
1194  *
1195  * If you don't want +.+ and +..+ and
1196  * want directories, consider Pathname#children.
1197  */
1198 static VALUE
1199 path_entries(VALUE self)
1200 {
1201  VALUE klass, str, ary;
1202  long i;
1203  klass = rb_obj_class(self);
1204  str = get_strpath(self);
1205  ary = rb_funcall(rb_cDir, id_entries, 1, str);
1206  ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
1207  for (i = 0; i < RARRAY_LEN(ary); i++) {
1208  VALUE elt = RARRAY_AREF(ary, i);
1209  elt = rb_class_new_instance(1, &elt, klass);
1210  rb_ary_store(ary, i, elt);
1211  }
1212  return ary;
1213 }
1214 
1215 /*
1216  * Create the referenced directory.
1217  *
1218  * See Dir.mkdir.
1219  */
1220 static VALUE
1221 path_mkdir(int argc, VALUE *argv, VALUE self)
1222 {
1223  VALUE str = get_strpath(self);
1224  VALUE vmode;
1225  if (rb_scan_args(argc, argv, "01", &vmode) == 0)
1226  return rb_funcall(rb_cDir, id_mkdir, 1, str);
1227  else
1228  return rb_funcall(rb_cDir, id_mkdir, 2, str, vmode);
1229 }
1230 
1231 /*
1232  * Remove the referenced directory.
1233  *
1234  * See Dir.rmdir.
1235  */
1236 static VALUE
1237 path_rmdir(VALUE self)
1238 {
1239  return rb_funcall(rb_cDir, id_rmdir, 1, get_strpath(self));
1240 }
1241 
1242 /*
1243  * Opens the referenced directory.
1244  *
1245  * See Dir.open.
1246  */
1247 static VALUE
1248 path_opendir(VALUE self)
1249 {
1250  VALUE args[1];
1251 
1252  args[0] = get_strpath(self);
1253  return rb_block_call(rb_cDir, id_open, 1, args, 0, 0);
1254 }
1255 
1256 static VALUE
1257 each_entry_i(RB_BLOCK_CALL_FUNC_ARGLIST(elt, klass))
1258 {
1259  return rb_yield(rb_class_new_instance(1, &elt, klass));
1260 }
1261 
1262 /*
1263  * Iterates over the entries (files and subdirectories) in the directory,
1264  * yielding a Pathname object for each entry.
1265  */
1266 static VALUE
1267 path_each_entry(VALUE self)
1268 {
1269  VALUE args[1];
1270 
1271  args[0] = get_strpath(self);
1272  return rb_block_call(rb_cDir, id_foreach, 1, args, each_entry_i, rb_obj_class(self));
1273 }
1274 
1275 static VALUE
1276 unlink_body(VALUE str)
1277 {
1278  return rb_funcall(rb_cDir, id_unlink, 1, str);
1279 }
1280 
1281 static VALUE
1282 unlink_rescue(VALUE str, VALUE errinfo)
1283 {
1284  return rb_funcall(rb_cFile, id_unlink, 1, str);
1285 }
1286 
1287 /*
1288  * Removes a file or directory, using File.unlink if +self+ is a file, or
1289  * Dir.unlink as necessary.
1290  */
1291 static VALUE
1292 path_unlink(VALUE self)
1293 {
1294  VALUE eENOTDIR = rb_const_get_at(rb_mErrno, id_ENOTDIR);
1295  VALUE str = get_strpath(self);
1296  return rb_rescue2(unlink_body, str, unlink_rescue, str, eENOTDIR, (VALUE)0);
1297 }
1298 
1299 /*
1300  * :call-seq:
1301  * Pathname(path) -> pathname
1302  *
1303  * Creates a new Pathname object from the given string, +path+, and returns
1304  * pathname object.
1305  *
1306  * In order to use this constructor, you must first require the Pathname
1307  * standard library extension.
1308  *
1309  * require 'pathname'
1310  * Pathname("/home/zzak")
1311  * #=> #<Pathname:/home/zzak>
1312  *
1313  * See also Pathname::new for more information.
1314  */
1315 static VALUE
1316 path_f_pathname(VALUE self, VALUE str)
1317 {
1318  if (CLASS_OF(str) == rb_cPathname)
1319  return str;
1320  return rb_class_new_instance(1, &str, rb_cPathname);
1321 }
1322 
1323 /*
1324  *
1325  * Pathname represents the name of a file or directory on the filesystem,
1326  * but not the file itself.
1327  *
1328  * The pathname depends on the Operating System: Unix, Windows, etc.
1329  * This library works with pathnames of local OS, however non-Unix pathnames
1330  * are supported experimentally.
1331  *
1332  * A Pathname can be relative or absolute. It's not until you try to
1333  * reference the file that it even matters whether the file exists or not.
1334  *
1335  * Pathname is immutable. It has no method for destructive update.
1336  *
1337  * The goal of this class is to manipulate file path information in a neater
1338  * way than standard Ruby provides. The examples below demonstrate the
1339  * difference.
1340  *
1341  * *All* functionality from File, FileTest, and some from Dir and FileUtils is
1342  * included, in an unsurprising way. It is essentially a facade for all of
1343  * these, and more.
1344  *
1345  * == Examples
1346  *
1347  * === Example 1: Using Pathname
1348  *
1349  * require 'pathname'
1350  * pn = Pathname.new("/usr/bin/ruby")
1351  * size = pn.size # 27662
1352  * isdir = pn.directory? # false
1353  * dir = pn.dirname # Pathname:/usr/bin
1354  * base = pn.basename # Pathname:ruby
1355  * dir, base = pn.split # [Pathname:/usr/bin, Pathname:ruby]
1356  * data = pn.read
1357  * pn.open { |f| _ }
1358  * pn.each_line { |line| _ }
1359  *
1360  * === Example 2: Using standard Ruby
1361  *
1362  * pn = "/usr/bin/ruby"
1363  * size = File.size(pn) # 27662
1364  * isdir = File.directory?(pn) # false
1365  * dir = File.dirname(pn) # "/usr/bin"
1366  * base = File.basename(pn) # "ruby"
1367  * dir, base = File.split(pn) # ["/usr/bin", "ruby"]
1368  * data = File.read(pn)
1369  * File.open(pn) { |f| _ }
1370  * File.foreach(pn) { |line| _ }
1371  *
1372  * === Example 3: Special features
1373  *
1374  * p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib
1375  * p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8
1376  * p3 = p1.parent # Pathname:/usr
1377  * p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8
1378  * pwd = Pathname.pwd # Pathname:/home/gavin
1379  * pwd.absolute? # true
1380  * p5 = Pathname.new "." # Pathname:.
1381  * p5 = p5 + "music/../articles" # Pathname:music/../articles
1382  * p5.cleanpath # Pathname:articles
1383  * p5.realpath # Pathname:/home/gavin/articles
1384  * p5.children # [Pathname:/home/gavin/articles/linux, ...]
1385  *
1386  * == Breakdown of functionality
1387  *
1388  * === Core methods
1389  *
1390  * These methods are effectively manipulating a String, because that's
1391  * all a path is. None of these access the file system except for
1392  * #mountpoint?, #children, #each_child, #realdirpath and #realpath.
1393  *
1394  * - +
1395  * - #join
1396  * - #parent
1397  * - #root?
1398  * - #absolute?
1399  * - #relative?
1400  * - #relative_path_from
1401  * - #each_filename
1402  * - #cleanpath
1403  * - #realpath
1404  * - #realdirpath
1405  * - #children
1406  * - #each_child
1407  * - #mountpoint?
1408  *
1409  * === File status predicate methods
1410  *
1411  * These methods are a facade for FileTest:
1412  * - #blockdev?
1413  * - #chardev?
1414  * - #directory?
1415  * - #executable?
1416  * - #executable_real?
1417  * - #exist?
1418  * - #file?
1419  * - #grpowned?
1420  * - #owned?
1421  * - #pipe?
1422  * - #readable?
1423  * - #world_readable?
1424  * - #readable_real?
1425  * - #setgid?
1426  * - #setuid?
1427  * - #size
1428  * - #size?
1429  * - #socket?
1430  * - #sticky?
1431  * - #symlink?
1432  * - #writable?
1433  * - #world_writable?
1434  * - #writable_real?
1435  * - #zero?
1436  *
1437  * === File property and manipulation methods
1438  *
1439  * These methods are a facade for File:
1440  * - #atime
1441  * - #birthtime
1442  * - #ctime
1443  * - #mtime
1444  * - #chmod(mode)
1445  * - #lchmod(mode)
1446  * - #chown(owner, group)
1447  * - #lchown(owner, group)
1448  * - #fnmatch(pattern, *args)
1449  * - #fnmatch?(pattern, *args)
1450  * - #ftype
1451  * - #make_link(old)
1452  * - #open(*args, &block)
1453  * - #readlink
1454  * - #rename(to)
1455  * - #stat
1456  * - #lstat
1457  * - #make_symlink(old)
1458  * - #truncate(length)
1459  * - #utime(atime, mtime)
1460  * - #basename(*args)
1461  * - #dirname
1462  * - #extname
1463  * - #expand_path(*args)
1464  * - #split
1465  *
1466  * === Directory methods
1467  *
1468  * These methods are a facade for Dir:
1469  * - Pathname.glob(*args)
1470  * - Pathname.getwd / Pathname.pwd
1471  * - #rmdir
1472  * - #entries
1473  * - #each_entry(&block)
1474  * - #mkdir(*args)
1475  * - #opendir(*args)
1476  *
1477  * === IO
1478  *
1479  * These methods are a facade for IO:
1480  * - #each_line(*args, &block)
1481  * - #read(*args)
1482  * - #binread(*args)
1483  * - #readlines(*args)
1484  * - #sysopen(*args)
1485  *
1486  * === Utilities
1487  *
1488  * These methods are a mixture of Find, FileUtils, and others:
1489  * - #find(&block)
1490  * - #mkpath
1491  * - #rmtree
1492  * - #unlink / #delete
1493  *
1494  *
1495  * == Method documentation
1496  *
1497  * As the above section shows, most of the methods in Pathname are facades. The
1498  * documentation for these methods generally just says, for instance, "See
1499  * FileTest.writable?", as you should be familiar with the original method
1500  * anyway, and its documentation (e.g. through +ri+) will contain more
1501  * information. In some cases, a brief description will follow.
1502  */
1503 void
1505 {
1506  InitVM(pathname);
1507 
1508  rb_cPathname = rb_define_class("Pathname", rb_cObject);
1509  rb_define_method(rb_cPathname, "initialize", path_initialize, 1);
1510  rb_define_method(rb_cPathname, "freeze", path_freeze, 0);
1511  rb_define_method(rb_cPathname, "taint", path_taint, 0);
1512  rb_define_method(rb_cPathname, "untaint", path_untaint, 0);
1513  rb_define_method(rb_cPathname, "==", path_eq, 1);
1514  rb_define_method(rb_cPathname, "===", path_eq, 1);
1515  rb_define_method(rb_cPathname, "eql?", path_eq, 1);
1516  rb_define_method(rb_cPathname, "<=>", path_cmp, 1);
1517  rb_define_method(rb_cPathname, "hash", path_hash, 0);
1518  rb_define_method(rb_cPathname, "to_s", path_to_s, 0);
1519  rb_define_method(rb_cPathname, "to_path", path_to_s, 0);
1520  rb_define_method(rb_cPathname, "inspect", path_inspect, 0);
1521  rb_define_method(rb_cPathname, "sub", path_sub, -1);
1522  rb_define_method(rb_cPathname, "sub_ext", path_sub_ext, 1);
1523  rb_define_method(rb_cPathname, "realpath", path_realpath, -1);
1524  rb_define_method(rb_cPathname, "realdirpath", path_realdirpath, -1);
1525  rb_define_method(rb_cPathname, "each_line", path_each_line, -1);
1526  rb_define_method(rb_cPathname, "read", path_read, -1);
1527  rb_define_method(rb_cPathname, "binread", path_binread, -1);
1528  rb_define_method(rb_cPathname, "readlines", path_readlines, -1);
1529  rb_define_method(rb_cPathname, "write", path_write, -1);
1530  rb_define_method(rb_cPathname, "binwrite", path_binwrite, -1);
1531  rb_define_method(rb_cPathname, "sysopen", path_sysopen, -1);
1532  rb_define_method(rb_cPathname, "atime", path_atime, 0);
1533  rb_define_method(rb_cPathname, "birthtime", path_birthtime, 0);
1534  rb_define_method(rb_cPathname, "ctime", path_ctime, 0);
1535  rb_define_method(rb_cPathname, "mtime", path_mtime, 0);
1536  rb_define_method(rb_cPathname, "chmod", path_chmod, 1);
1537  rb_define_method(rb_cPathname, "lchmod", path_lchmod, 1);
1538  rb_define_method(rb_cPathname, "chown", path_chown, 2);
1539  rb_define_method(rb_cPathname, "lchown", path_lchown, 2);
1540  rb_define_method(rb_cPathname, "fnmatch", path_fnmatch, -1);
1541  rb_define_method(rb_cPathname, "fnmatch?", path_fnmatch, -1);
1542  rb_define_method(rb_cPathname, "ftype", path_ftype, 0);
1543  rb_define_method(rb_cPathname, "make_link", path_make_link, 1);
1544  rb_define_method(rb_cPathname, "open", path_open, -1);
1545  rb_define_method(rb_cPathname, "readlink", path_readlink, 0);
1546  rb_define_method(rb_cPathname, "rename", path_rename, 1);
1547  rb_define_method(rb_cPathname, "stat", path_stat, 0);
1548  rb_define_method(rb_cPathname, "lstat", path_lstat, 0);
1549  rb_define_method(rb_cPathname, "make_symlink", path_make_symlink, 1);
1550  rb_define_method(rb_cPathname, "truncate", path_truncate, 1);
1551  rb_define_method(rb_cPathname, "utime", path_utime, 2);
1552  rb_define_method(rb_cPathname, "basename", path_basename, -1);
1553  rb_define_method(rb_cPathname, "dirname", path_dirname, 0);
1554  rb_define_method(rb_cPathname, "extname", path_extname, 0);
1555  rb_define_method(rb_cPathname, "expand_path", path_expand_path, -1);
1556  rb_define_method(rb_cPathname, "split", path_split, 0);
1557  rb_define_method(rb_cPathname, "blockdev?", path_blockdev_p, 0);
1558  rb_define_method(rb_cPathname, "chardev?", path_chardev_p, 0);
1559  rb_define_method(rb_cPathname, "executable?", path_executable_p, 0);
1560  rb_define_method(rb_cPathname, "executable_real?", path_executable_real_p, 0);
1561  rb_define_method(rb_cPathname, "exist?", path_exist_p, 0);
1562  rb_define_method(rb_cPathname, "grpowned?", path_grpowned_p, 0);
1563  rb_define_method(rb_cPathname, "directory?", path_directory_p, 0);
1564  rb_define_method(rb_cPathname, "file?", path_file_p, 0);
1565  rb_define_method(rb_cPathname, "pipe?", path_pipe_p, 0);
1566  rb_define_method(rb_cPathname, "socket?", path_socket_p, 0);
1567  rb_define_method(rb_cPathname, "owned?", path_owned_p, 0);
1568  rb_define_method(rb_cPathname, "readable?", path_readable_p, 0);
1569  rb_define_method(rb_cPathname, "world_readable?", path_world_readable_p, 0);
1570  rb_define_method(rb_cPathname, "readable_real?", path_readable_real_p, 0);
1571  rb_define_method(rb_cPathname, "setuid?", path_setuid_p, 0);
1572  rb_define_method(rb_cPathname, "setgid?", path_setgid_p, 0);
1573  rb_define_method(rb_cPathname, "size", path_size, 0);
1574  rb_define_method(rb_cPathname, "size?", path_size_p, 0);
1575  rb_define_method(rb_cPathname, "sticky?", path_sticky_p, 0);
1576  rb_define_method(rb_cPathname, "symlink?", path_symlink_p, 0);
1577  rb_define_method(rb_cPathname, "writable?", path_writable_p, 0);
1578  rb_define_method(rb_cPathname, "world_writable?", path_world_writable_p, 0);
1579  rb_define_method(rb_cPathname, "writable_real?", path_writable_real_p, 0);
1580  rb_define_method(rb_cPathname, "zero?", path_zero_p, 0);
1581  rb_define_method(rb_cPathname, "empty?", path_empty_p, 0);
1582  rb_define_singleton_method(rb_cPathname, "glob", path_s_glob, -1);
1583  rb_define_singleton_method(rb_cPathname, "getwd", path_s_getwd, 0);
1584  rb_define_singleton_method(rb_cPathname, "pwd", path_s_getwd, 0);
1585  rb_define_method(rb_cPathname, "glob", path_glob, -1);
1586  rb_define_method(rb_cPathname, "entries", path_entries, 0);
1587  rb_define_method(rb_cPathname, "mkdir", path_mkdir, -1);
1588  rb_define_method(rb_cPathname, "rmdir", path_rmdir, 0);
1589  rb_define_method(rb_cPathname, "opendir", path_opendir, 0);
1590  rb_define_method(rb_cPathname, "each_entry", path_each_entry, 0);
1591  rb_define_method(rb_cPathname, "unlink", path_unlink, 0);
1592  rb_define_method(rb_cPathname, "delete", path_unlink, 0);
1593  rb_undef_method(rb_cPathname, "=~");
1594  rb_define_global_function("Pathname", path_f_pathname, 1);
1595 }
1596 
1597 void
1599 {
1600 #undef rb_intern
1601  id_at_path = rb_intern("@path");
1602  id_to_path = rb_intern("to_path");
1603  id_ENOTDIR = rb_intern("ENOTDIR");
1604  id_atime = rb_intern("atime");
1605  id_basename = rb_intern("basename");
1606  id_base = rb_intern("base");
1607  id_binread = rb_intern("binread");
1608  id_binwrite = rb_intern("binwrite");
1609  id_birthtime = rb_intern("birthtime");
1610  id_blockdev_p = rb_intern("blockdev?");
1611  id_chardev_p = rb_intern("chardev?");
1612  id_chmod = rb_intern("chmod");
1613  id_chown = rb_intern("chown");
1614  id_ctime = rb_intern("ctime");
1615  id_directory_p = rb_intern("directory?");
1616  id_dirname = rb_intern("dirname");
1617  id_empty_p = rb_intern("empty?");
1618  id_entries = rb_intern("entries");
1619  id_executable_p = rb_intern("executable?");
1620  id_executable_real_p = rb_intern("executable_real?");
1621  id_exist_p = rb_intern("exist?");
1622  id_expand_path = rb_intern("expand_path");
1623  id_extname = rb_intern("extname");
1624  id_file_p = rb_intern("file?");
1625  id_fnmatch = rb_intern("fnmatch");
1626  id_foreach = rb_intern("foreach");
1627  id_ftype = rb_intern("ftype");
1628  id_getwd = rb_intern("getwd");
1629  id_glob = rb_intern("glob");
1630  id_grpowned_p = rb_intern("grpowned?");
1631  id_lchmod = rb_intern("lchmod");
1632  id_lchown = rb_intern("lchown");
1633  id_link = rb_intern("link");
1634  id_lstat = rb_intern("lstat");
1635  id_mkdir = rb_intern("mkdir");
1636  id_mtime = rb_intern("mtime");
1637  id_open = rb_intern("open");
1638  id_owned_p = rb_intern("owned?");
1639  id_pipe_p = rb_intern("pipe?");
1640  id_read = rb_intern("read");
1641  id_readable_p = rb_intern("readable?");
1642  id_readable_real_p = rb_intern("readable_real?");
1643  id_readlines = rb_intern("readlines");
1644  id_readlink = rb_intern("readlink");
1645  id_realdirpath = rb_intern("realdirpath");
1646  id_realpath = rb_intern("realpath");
1647  id_rename = rb_intern("rename");
1648  id_rmdir = rb_intern("rmdir");
1649  id_setgid_p = rb_intern("setgid?");
1650  id_setuid_p = rb_intern("setuid?");
1651  id_size = rb_intern("size");
1652  id_size_p = rb_intern("size?");
1653  id_socket_p = rb_intern("socket?");
1654  id_split = rb_intern("split");
1655  id_stat = rb_intern("stat");
1656  id_sticky_p = rb_intern("sticky?");
1657  id_sub = rb_intern("sub");
1658  id_symlink = rb_intern("symlink");
1659  id_symlink_p = rb_intern("symlink?");
1660  id_sysopen = rb_intern("sysopen");
1661  id_truncate = rb_intern("truncate");
1662  id_unlink = rb_intern("unlink");
1663  id_utime = rb_intern("utime");
1664  id_world_readable_p = rb_intern("world_readable?");
1665  id_world_writable_p = rb_intern("world_writable?");
1666  id_writable_p = rb_intern("writable?");
1667  id_writable_real_p = rb_intern("writable_real?");
1668  id_write = rb_intern("write");
1669  id_zero_p = rb_intern("zero?");
1670 }
ID
unsigned long ID
Definition: ruby.h:103
rb_check_funcall
VALUE rb_check_funcall(VALUE, ID, int, const VALUE *)
Definition: vm_eval.c:505
rb_define_class
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:649
rb_mFileTest
VALUE rb_mFileTest
Definition: file.c:160
rb_str_freeze
VALUE rb_str_freeze(VALUE)
Definition: string.c:2616
rb_str_hash
st_index_t rb_str_hash(VALUE)
Definition: string.c:3163
id_size
#define id_size
Definition: enum.c:30
RB_PASS_CALLED_KEYWORDS
#define RB_PASS_CALLED_KEYWORDS
Definition: ruby.h:1980
rb_hash_new
VALUE rb_hash_new(void)
Definition: hash.c:1523
basename
#define basename
Definition: rb_mjit_min_header-2.7.2.h:2853
s2
const char * s2
Definition: rb_mjit_min_header-2.7.2.h:5489
rb_block_given_p
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:898
rb_warning
void rb_warning(const char *fmt,...)
Definition: error.c:336
rb_cDir
VALUE rb_cDir
Definition: dir.c:439
rb_funcallv_kw
VALUE rb_funcallv_kw(VALUE, ID, int, const VALUE *, int)
Definition: vm_eval.c:962
INT2FIX
#define INT2FIX(i)
Definition: ruby.h:263
RSTRING_PTR
#define RSTRING_PTR(str)
Definition: ruby.h:1009
i
uint32_t i
Definition: rb_mjit_min_header-2.7.2.h:5499
VALUE
unsigned long VALUE
Definition: ruby.h:102
rb_eArgError
VALUE rb_eArgError
Definition: error.c:925
encoding.h
rb_intern
#define rb_intern(str)
rb_ary_store
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:1079
RB_TYPE_P
#define RB_TYPE_P(obj, type)
Definition: ruby.h:560
rb_enc_get
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:872
rb_const_get_at
VALUE rb_const_get_at(VALUE, ID)
Definition: variable.c:2397
rb_define_global_function
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
Definition: class.c:1787
ruby_enc_find_extname
const char * ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
Definition: file.c:4699
rb_call_super
VALUE rb_call_super(int, const VALUE *)
Definition: vm_eval.c:306
Init_pathname
void Init_pathname(void)
Definition: pathname.c:1504
Qundef
#define Qundef
Definition: ruby.h:470
rb_define_singleton_method
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1755
rb_define_method
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1551
Qfalse
#define Qfalse
Definition: ruby.h:467
rb_ary_new3
#define rb_ary_new3
Definition: intern.h:104
NULL
#define NULL
Definition: _sdbm.c:101
PRIsVALUE
#define PRIsVALUE
Definition: ruby.h:166
ID2SYM
#define ID2SYM(x)
Definition: ruby.h:414
rb_cFile
VALUE rb_cFile
Definition: file.c:159
RB_BLOCK_CALL_FUNC_ARGLIST
#define RB_BLOCK_CALL_FUNC_ARGLIST(yielded_arg, callback_arg)
Definition: ruby.h:1964
rb_undef_method
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1575
InitVM
#define InitVM(ext)
Definition: ruby.h:2329
rb_raise
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2671
rb_ary_entry
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1512
rb_ivar_get
VALUE rb_ivar_get(VALUE, ID)
Definition: variable.c:1070
rb_obj_class
VALUE rb_obj_class(VALUE)
Equivalent to Object#class in Ruby.
Definition: object.c:217
klass
VALUE klass
Definition: rb_mjit_min_header-2.7.2.h:13302
RB_PASS_KEYWORDS
#define RB_PASS_KEYWORDS
Definition: ruby.h:1978
InitVM_pathname
void InitVM_pathname(void)
Definition: pathname.c:1598
rb_str_equal
VALUE rb_str_equal(VALUE str1, VALUE str2)
Definition: string.c:3267
memchr
void * memchr(const void *, int, size_t)
ruby.h
RARRAY_AREF
#define RARRAY_AREF(a, i)
Definition: psych_emitter.c:7
rb_eTypeError
VALUE rb_eTypeError
Definition: error.c:924
arg
VALUE arg
Definition: rb_mjit_min_header-2.7.2.h:5636
rb_obj_dup
VALUE rb_obj_dup(VALUE)
Equivalent to Object#dup in Ruby.
Definition: object.c:420
rb_check_array_type
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:909
rb_scan_args
#define rb_scan_args(argc, argvp, fmt,...)
Definition: rb_mjit_min_header-2.7.2.h:6407
path_birthtime
#define path_birthtime
Definition: pathname.c:524
CLASS_OF
#define CLASS_OF(v)
Definition: ruby.h:484
RARRAY_LEN
#define RARRAY_LEN(a)
Definition: ruby.h:1070
rb_cObject
RUBY_EXTERN VALUE rb_cObject
Definition: ruby.h:2010
obj
const VALUE VALUE obj
Definition: rb_mjit_min_header-2.7.2.h:5777
rb_str_append
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:2965
T_ARRAY
#define T_ARRAY
Definition: ruby.h:530
argv
char ** argv
Definition: ruby.c:223
rb_sprintf
VALUE rb_sprintf(const char *format,...)
Definition: sprintf.c:1197
StringValue
use StringValue() instead")))
rb_str_subseq
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:2474
str
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
rb_convert_type
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
Definition: object.c:2900
path
VALUE path
Definition: rb_mjit_min_header-2.7.2.h:7389
rb_hash_aset
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:2852
ruby::backward::cxxanyargs::rb_block_call
VALUE rb_block_call(VALUE q, ID w, int e, const VALUE *r, type *t, VALUE y)
Call a method with a block.
Definition: cxxanyargs.hpp:178
rb_funcall
#define rb_funcall(recv, mid, argc,...)
Definition: rb_mjit_min_header-2.7.2.h:6620
argc
int argc
Definition: ruby.c:222
rb_obj_classname
const char * rb_obj_classname(VALUE)
Definition: variable.c:289
rb_rescue2
VALUE rb_rescue2(VALUE(*b_proc)(VALUE), VALUE data1, VALUE(*r_proc)(VALUE, VALUE), VALUE data2,...)
An equivalent of rescue clause.
Definition: eval.c:962
rb_mErrno
VALUE rb_mErrno
Definition: error.c:944
rb_class_new_instance
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:1955
rb_ivar_set
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1300
T_STRING
#define T_STRING
Definition: ruby.h:528
rb_funcallv
#define rb_funcallv(recv, mid, argc, argv)
Definition: rb_mjit_min_header-2.7.2.h:7940
old
VALUE ID VALUE old
Definition: rb_mjit_min_header-2.7.2.h:16196
rb_yield
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1237
rb_block_call_kw
VALUE rb_block_call_kw(VALUE, ID, int, const VALUE *, rb_block_call_func_t, VALUE, int)
Definition: vm_eval.c:1484
ST2FIX
#define ST2FIX(h)
Definition: ruby_missing.h:21
Qnil
#define Qnil
Definition: ruby.h:469
RSTRING_LEN
#define RSTRING_LEN(str)
Definition: ruby.h:1005
char
#define char
Definition: rb_mjit_min_header-2.7.2.h:2916
rb_obj_is_kind_of
VALUE rb_obj_is_kind_of(VALUE, VALUE)
Determines if obj is a kind of c.
Definition: object.c:692
RTEST
#define RTEST(v)
Definition: ruby.h:481
rb_cIO
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:2030
n
const char size_t n
Definition: rb_mjit_min_header-2.7.2.h:5491