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