00001 #include "ruby.h"
00002 #include "ruby/encoding.h"
00003
00004 static VALUE rb_cPathname;
00005 static ID id_at_path, id_to_path;
00006
00007 static VALUE
00008 get_strpath(VALUE obj)
00009 {
00010 VALUE strpath;
00011 strpath = rb_ivar_get(obj, id_at_path);
00012 if (!RB_TYPE_P(strpath, T_STRING))
00013 rb_raise(rb_eTypeError, "unexpected @path");
00014 return strpath;
00015 }
00016
00017 static void
00018 set_strpath(VALUE obj, VALUE val)
00019 {
00020 rb_ivar_set(obj, id_at_path, val);
00021 }
00022
00023
00024
00025
00026
00027 static VALUE
00028 path_initialize(VALUE self, VALUE arg)
00029 {
00030 VALUE str;
00031 if (RB_TYPE_P(arg, T_STRING)) {
00032 str = arg;
00033 }
00034 else {
00035 str = rb_check_funcall(arg, id_to_path, 0, NULL);
00036 if (str == Qundef)
00037 str = arg;
00038 StringValue(str);
00039 }
00040 if (memchr(RSTRING_PTR(str), '\0', RSTRING_LEN(str)))
00041 rb_raise(rb_eArgError, "pathname contains null byte");
00042 str = rb_obj_dup(str);
00043
00044 set_strpath(self, str);
00045 OBJ_INFECT(self, str);
00046 return self;
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 static VALUE
00058 path_freeze(VALUE self)
00059 {
00060 rb_call_super(0, 0);
00061 rb_str_freeze(get_strpath(self));
00062 return self;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 static VALUE
00074 path_taint(VALUE self)
00075 {
00076 rb_call_super(0, 0);
00077 rb_obj_taint(get_strpath(self));
00078 return self;
00079 }
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 static VALUE
00090 path_untaint(VALUE self)
00091 {
00092 rb_call_super(0, 0);
00093 rb_obj_untaint(get_strpath(self));
00094 return self;
00095 }
00096
00097
00098
00099
00100
00101
00102 static VALUE
00103 path_eq(VALUE self, VALUE other)
00104 {
00105 if (!rb_obj_is_kind_of(other, rb_cPathname))
00106 return Qfalse;
00107 return rb_str_equal(get_strpath(self), get_strpath(other));
00108 }
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 static VALUE
00125 path_cmp(VALUE self, VALUE other)
00126 {
00127 VALUE s1, s2;
00128 char *p1, *p2;
00129 char *e1, *e2;
00130 if (!rb_obj_is_kind_of(other, rb_cPathname))
00131 return Qnil;
00132 s1 = get_strpath(self);
00133 s2 = get_strpath(other);
00134 p1 = RSTRING_PTR(s1);
00135 p2 = RSTRING_PTR(s2);
00136 e1 = p1 + RSTRING_LEN(s1);
00137 e2 = p2 + RSTRING_LEN(s2);
00138 while (p1 < e1 && p2 < e2) {
00139 int c1, c2;
00140 c1 = (unsigned char)*p1++;
00141 c2 = (unsigned char)*p2++;
00142 if (c1 == '/') c1 = '\0';
00143 if (c2 == '/') c2 = '\0';
00144 if (c1 != c2) {
00145 if (c1 < c2)
00146 return INT2FIX(-1);
00147 else
00148 return INT2FIX(1);
00149 }
00150 }
00151 if (p1 < e1)
00152 return INT2FIX(1);
00153 if (p2 < e2)
00154 return INT2FIX(-1);
00155 return INT2FIX(0);
00156 }
00157
00158
00159 static VALUE
00160 path_hash(VALUE self)
00161 {
00162 return INT2FIX(rb_str_hash(get_strpath(self)));
00163 }
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174 static VALUE
00175 path_to_s(VALUE self)
00176 {
00177 return rb_obj_dup(get_strpath(self));
00178 }
00179
00180
00181 static VALUE
00182 path_inspect(VALUE self)
00183 {
00184 const char *c = rb_obj_classname(self);
00185 VALUE str = get_strpath(self);
00186 return rb_sprintf("#<%s:%"PRIsVALUE">", c, str);
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196 static VALUE
00197 path_sub(int argc, VALUE *argv, VALUE self)
00198 {
00199 VALUE str = get_strpath(self);
00200
00201 if (rb_block_given_p()) {
00202 str = rb_block_call(str, rb_intern("sub"), argc, argv, 0, 0);
00203 }
00204 else {
00205 str = rb_funcall2(str, rb_intern("sub"), argc, argv);
00206 }
00207 return rb_class_new_instance(1, &str, rb_obj_class(self));
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 static VALUE
00219 path_sub_ext(VALUE self, VALUE repl)
00220 {
00221 VALUE str = get_strpath(self);
00222 VALUE str2;
00223 long extlen;
00224 const char *ext;
00225 const char *p;
00226
00227 StringValue(repl);
00228 p = RSTRING_PTR(str);
00229 extlen = RSTRING_LEN(str);
00230 ext = ruby_enc_find_extname(p, &extlen, rb_enc_get(str));
00231 if (ext == NULL) {
00232 ext = p + RSTRING_LEN(str);
00233 }
00234 else if (extlen <= 1) {
00235 ext += extlen;
00236 }
00237 str2 = rb_str_subseq(str, 0, ext-p);
00238 rb_str_append(str2, repl);
00239 OBJ_INFECT(str2, str);
00240 return rb_class_new_instance(1, &str2, rb_obj_class(self));
00241 }
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 static VALUE
00256 path_realpath(int argc, VALUE *argv, VALUE self)
00257 {
00258 VALUE basedir, str;
00259 rb_scan_args(argc, argv, "01", &basedir);
00260 str = rb_funcall(rb_cFile, rb_intern("realpath"), 2, get_strpath(self), basedir);
00261 return rb_class_new_instance(1, &str, rb_obj_class(self));
00262 }
00263
00264
00265
00266
00267
00268
00269
00270
00271 static VALUE
00272 path_realdirpath(int argc, VALUE *argv, VALUE self)
00273 {
00274 VALUE basedir, str;
00275 rb_scan_args(argc, argv, "01", &basedir);
00276 str = rb_funcall(rb_cFile, rb_intern("realdirpath"), 2, get_strpath(self), basedir);
00277 return rb_class_new_instance(1, &str, rb_obj_class(self));
00278 }
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 static VALUE
00291 path_each_line(int argc, VALUE *argv, VALUE self)
00292 {
00293 VALUE args[4];
00294 int n;
00295
00296 args[0] = get_strpath(self);
00297 n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
00298 if (rb_block_given_p()) {
00299 return rb_block_call(rb_cIO, rb_intern("foreach"), 1+n, args, 0, 0);
00300 }
00301 else {
00302 return rb_funcall2(rb_cIO, rb_intern("foreach"), 1+n, args);
00303 }
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 static VALUE
00317 path_read(int argc, VALUE *argv, VALUE self)
00318 {
00319 VALUE args[4];
00320 int n;
00321
00322 args[0] = get_strpath(self);
00323 n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
00324 return rb_funcall2(rb_cIO, rb_intern("read"), 1+n, args);
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336 static VALUE
00337 path_binread(int argc, VALUE *argv, VALUE self)
00338 {
00339 VALUE args[3];
00340 int n;
00341
00342 args[0] = get_strpath(self);
00343 n = rb_scan_args(argc, argv, "02", &args[1], &args[2]);
00344 return rb_funcall2(rb_cIO, rb_intern("binread"), 1+n, args);
00345 }
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358 static VALUE
00359 path_readlines(int argc, VALUE *argv, VALUE self)
00360 {
00361 VALUE args[4];
00362 int n;
00363
00364 args[0] = get_strpath(self);
00365 n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
00366 return rb_funcall2(rb_cIO, rb_intern("readlines"), 1+n, args);
00367 }
00368
00369
00370
00371
00372
00373
00374
00375
00376 static VALUE
00377 path_sysopen(int argc, VALUE *argv, VALUE self)
00378 {
00379 VALUE args[3];
00380 int n;
00381
00382 args[0] = get_strpath(self);
00383 n = rb_scan_args(argc, argv, "02", &args[1], &args[2]);
00384 return rb_funcall2(rb_cIO, rb_intern("sysopen"), 1+n, args);
00385 }
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 static VALUE
00396 path_atime(VALUE self)
00397 {
00398 return rb_funcall(rb_cFile, rb_intern("atime"), 1, get_strpath(self));
00399 }
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409 static VALUE
00410 path_ctime(VALUE self)
00411 {
00412 return rb_funcall(rb_cFile, rb_intern("ctime"), 1, get_strpath(self));
00413 }
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 static VALUE
00424 path_mtime(VALUE self)
00425 {
00426 return rb_funcall(rb_cFile, rb_intern("mtime"), 1, get_strpath(self));
00427 }
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437 static VALUE
00438 path_chmod(VALUE self, VALUE mode)
00439 {
00440 return rb_funcall(rb_cFile, rb_intern("chmod"), 2, mode, get_strpath(self));
00441 }
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451 static VALUE
00452 path_lchmod(VALUE self, VALUE mode)
00453 {
00454 return rb_funcall(rb_cFile, rb_intern("lchmod"), 2, mode, get_strpath(self));
00455 }
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465 static VALUE
00466 path_chown(VALUE self, VALUE owner, VALUE group)
00467 {
00468 return rb_funcall(rb_cFile, rb_intern("chown"), 3, owner, group, get_strpath(self));
00469 }
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479 static VALUE
00480 path_lchown(VALUE self, VALUE owner, VALUE group)
00481 {
00482 return rb_funcall(rb_cFile, rb_intern("lchown"), 3, owner, group, get_strpath(self));
00483 }
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494 static VALUE
00495 path_fnmatch(int argc, VALUE *argv, VALUE self)
00496 {
00497 VALUE str = get_strpath(self);
00498 VALUE pattern, flags;
00499 if (rb_scan_args(argc, argv, "11", &pattern, &flags) == 1)
00500 return rb_funcall(rb_cFile, rb_intern("fnmatch"), 2, pattern, str);
00501 else
00502 return rb_funcall(rb_cFile, rb_intern("fnmatch"), 3, pattern, str, flags);
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513 static VALUE
00514 path_ftype(VALUE self)
00515 {
00516 return rb_funcall(rb_cFile, rb_intern("ftype"), 1, get_strpath(self));
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527 static VALUE
00528 path_make_link(VALUE self, VALUE old)
00529 {
00530 return rb_funcall(rb_cFile, rb_intern("link"), 2, old, get_strpath(self));
00531 }
00532
00533
00534
00535
00536
00537
00538 static VALUE
00539 path_open(int argc, VALUE *argv, VALUE self)
00540 {
00541 VALUE args[4];
00542 int n;
00543
00544 args[0] = get_strpath(self);
00545 n = rb_scan_args(argc, argv, "03", &args[1], &args[2], &args[3]);
00546 if (rb_block_given_p()) {
00547 return rb_block_call(rb_cFile, rb_intern("open"), 1+n, args, 0, 0);
00548 }
00549 else {
00550 return rb_funcall2(rb_cFile, rb_intern("open"), 1+n, args);
00551 }
00552 }
00553
00554
00555
00556
00557
00558
00559 static VALUE
00560 path_readlink(VALUE self)
00561 {
00562 VALUE str;
00563 str = rb_funcall(rb_cFile, rb_intern("readlink"), 1, get_strpath(self));
00564 return rb_class_new_instance(1, &str, rb_obj_class(self));
00565 }
00566
00567
00568
00569
00570
00571
00572 static VALUE
00573 path_rename(VALUE self, VALUE to)
00574 {
00575 return rb_funcall(rb_cFile, rb_intern("rename"), 2, get_strpath(self), to);
00576 }
00577
00578
00579
00580
00581
00582
00583 static VALUE
00584 path_stat(VALUE self)
00585 {
00586 return rb_funcall(rb_cFile, rb_intern("stat"), 1, get_strpath(self));
00587 }
00588
00589
00590
00591
00592 static VALUE
00593 path_lstat(VALUE self)
00594 {
00595 return rb_funcall(rb_cFile, rb_intern("lstat"), 1, get_strpath(self));
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606 static VALUE
00607 path_make_symlink(VALUE self, VALUE old)
00608 {
00609 return rb_funcall(rb_cFile, rb_intern("symlink"), 2, old, get_strpath(self));
00610 }
00611
00612
00613
00614
00615
00616
00617 static VALUE
00618 path_truncate(VALUE self, VALUE length)
00619 {
00620 return rb_funcall(rb_cFile, rb_intern("truncate"), 2, get_strpath(self), length);
00621 }
00622
00623
00624
00625
00626
00627
00628 static VALUE
00629 path_utime(VALUE self, VALUE atime, VALUE mtime)
00630 {
00631 return rb_funcall(rb_cFile, rb_intern("utime"), 3, atime, mtime, get_strpath(self));
00632 }
00633
00634
00635
00636
00637
00638
00639 static VALUE
00640 path_basename(int argc, VALUE *argv, VALUE self)
00641 {
00642 VALUE str = get_strpath(self);
00643 VALUE fext;
00644 if (rb_scan_args(argc, argv, "01", &fext) == 0)
00645 str = rb_funcall(rb_cFile, rb_intern("basename"), 1, str);
00646 else
00647 str = rb_funcall(rb_cFile, rb_intern("basename"), 2, str, fext);
00648 return rb_class_new_instance(1, &str, rb_obj_class(self));
00649 }
00650
00651
00652
00653
00654
00655
00656 static VALUE
00657 path_dirname(VALUE self)
00658 {
00659 VALUE str = get_strpath(self);
00660 str = rb_funcall(rb_cFile, rb_intern("dirname"), 1, str);
00661 return rb_class_new_instance(1, &str, rb_obj_class(self));
00662 }
00663
00664
00665
00666
00667
00668
00669 static VALUE
00670 path_extname(VALUE self)
00671 {
00672 VALUE str = get_strpath(self);
00673 return rb_funcall(rb_cFile, rb_intern("extname"), 1, str);
00674 }
00675
00676
00677
00678
00679
00680
00681 static VALUE
00682 path_expand_path(int argc, VALUE *argv, VALUE self)
00683 {
00684 VALUE str = get_strpath(self);
00685 VALUE dname;
00686 if (rb_scan_args(argc, argv, "01", &dname) == 0)
00687 str = rb_funcall(rb_cFile, rb_intern("expand_path"), 1, str);
00688 else
00689 str = rb_funcall(rb_cFile, rb_intern("expand_path"), 2, str, dname);
00690 return rb_class_new_instance(1, &str, rb_obj_class(self));
00691 }
00692
00693
00694
00695
00696
00697
00698 static VALUE
00699 path_split(VALUE self)
00700 {
00701 VALUE str = get_strpath(self);
00702 VALUE ary, dirname, basename;
00703 ary = rb_funcall(rb_cFile, rb_intern("split"), 1, str);
00704 ary = rb_check_array_type(ary);
00705 dirname = rb_ary_entry(ary, 0);
00706 basename = rb_ary_entry(ary, 1);
00707 dirname = rb_class_new_instance(1, &dirname, rb_obj_class(self));
00708 basename = rb_class_new_instance(1, &basename, rb_obj_class(self));
00709 return rb_ary_new3(2, dirname, basename);
00710 }
00711
00712
00713
00714
00715 static VALUE
00716 path_blockdev_p(VALUE self)
00717 {
00718 return rb_funcall(rb_mFileTest, rb_intern("blockdev?"), 1, get_strpath(self));
00719 }
00720
00721
00722
00723
00724 static VALUE
00725 path_chardev_p(VALUE self)
00726 {
00727 return rb_funcall(rb_mFileTest, rb_intern("chardev?"), 1, get_strpath(self));
00728 }
00729
00730
00731
00732
00733 static VALUE
00734 path_executable_p(VALUE self)
00735 {
00736 return rb_funcall(rb_mFileTest, rb_intern("executable?"), 1, get_strpath(self));
00737 }
00738
00739
00740
00741
00742 static VALUE
00743 path_executable_real_p(VALUE self)
00744 {
00745 return rb_funcall(rb_mFileTest, rb_intern("executable_real?"), 1, get_strpath(self));
00746 }
00747
00748
00749
00750
00751 static VALUE
00752 path_exist_p(VALUE self)
00753 {
00754 return rb_funcall(rb_mFileTest, rb_intern("exist?"), 1, get_strpath(self));
00755 }
00756
00757
00758
00759
00760 static VALUE
00761 path_grpowned_p(VALUE self)
00762 {
00763 return rb_funcall(rb_mFileTest, rb_intern("grpowned?"), 1, get_strpath(self));
00764 }
00765
00766
00767
00768
00769 static VALUE
00770 path_directory_p(VALUE self)
00771 {
00772 return rb_funcall(rb_mFileTest, rb_intern("directory?"), 1, get_strpath(self));
00773 }
00774
00775
00776
00777
00778 static VALUE
00779 path_file_p(VALUE self)
00780 {
00781 return rb_funcall(rb_mFileTest, rb_intern("file?"), 1, get_strpath(self));
00782 }
00783
00784
00785
00786
00787 static VALUE
00788 path_pipe_p(VALUE self)
00789 {
00790 return rb_funcall(rb_mFileTest, rb_intern("pipe?"), 1, get_strpath(self));
00791 }
00792
00793
00794
00795
00796 static VALUE
00797 path_socket_p(VALUE self)
00798 {
00799 return rb_funcall(rb_mFileTest, rb_intern("socket?"), 1, get_strpath(self));
00800 }
00801
00802
00803
00804
00805 static VALUE
00806 path_owned_p(VALUE self)
00807 {
00808 return rb_funcall(rb_mFileTest, rb_intern("owned?"), 1, get_strpath(self));
00809 }
00810
00811
00812
00813
00814 static VALUE
00815 path_readable_p(VALUE self)
00816 {
00817 return rb_funcall(rb_mFileTest, rb_intern("readable?"), 1, get_strpath(self));
00818 }
00819
00820
00821
00822
00823 static VALUE
00824 path_world_readable_p(VALUE self)
00825 {
00826 return rb_funcall(rb_mFileTest, rb_intern("world_readable?"), 1, get_strpath(self));
00827 }
00828
00829
00830
00831
00832 static VALUE
00833 path_readable_real_p(VALUE self)
00834 {
00835 return rb_funcall(rb_mFileTest, rb_intern("readable_real?"), 1, get_strpath(self));
00836 }
00837
00838
00839
00840
00841 static VALUE
00842 path_setuid_p(VALUE self)
00843 {
00844 return rb_funcall(rb_mFileTest, rb_intern("setuid?"), 1, get_strpath(self));
00845 }
00846
00847
00848
00849
00850 static VALUE
00851 path_setgid_p(VALUE self)
00852 {
00853 return rb_funcall(rb_mFileTest, rb_intern("setgid?"), 1, get_strpath(self));
00854 }
00855
00856
00857
00858
00859 static VALUE
00860 path_size(VALUE self)
00861 {
00862 return rb_funcall(rb_mFileTest, rb_intern("size"), 1, get_strpath(self));
00863 }
00864
00865
00866
00867
00868 static VALUE
00869 path_size_p(VALUE self)
00870 {
00871 return rb_funcall(rb_mFileTest, rb_intern("size?"), 1, get_strpath(self));
00872 }
00873
00874
00875
00876
00877 static VALUE
00878 path_sticky_p(VALUE self)
00879 {
00880 return rb_funcall(rb_mFileTest, rb_intern("sticky?"), 1, get_strpath(self));
00881 }
00882
00883
00884
00885
00886 static VALUE
00887 path_symlink_p(VALUE self)
00888 {
00889 return rb_funcall(rb_mFileTest, rb_intern("symlink?"), 1, get_strpath(self));
00890 }
00891
00892
00893
00894
00895 static VALUE
00896 path_writable_p(VALUE self)
00897 {
00898 return rb_funcall(rb_mFileTest, rb_intern("writable?"), 1, get_strpath(self));
00899 }
00900
00901
00902
00903
00904 static VALUE
00905 path_world_writable_p(VALUE self)
00906 {
00907 return rb_funcall(rb_mFileTest, rb_intern("world_writable?"), 1, get_strpath(self));
00908 }
00909
00910
00911
00912
00913 static VALUE
00914 path_writable_real_p(VALUE self)
00915 {
00916 return rb_funcall(rb_mFileTest, rb_intern("writable_real?"), 1, get_strpath(self));
00917 }
00918
00919
00920
00921
00922 static VALUE
00923 path_zero_p(VALUE self)
00924 {
00925 return rb_funcall(rb_mFileTest, rb_intern("zero?"), 1, get_strpath(self));
00926 }
00927
00928 static VALUE
00929 glob_i(VALUE elt, VALUE klass, int argc, VALUE *argv)
00930 {
00931 return rb_yield(rb_class_new_instance(1, &elt, klass));
00932 }
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942 static VALUE
00943 path_s_glob(int argc, VALUE *argv, VALUE klass)
00944 {
00945 VALUE args[2];
00946 int n;
00947
00948 n = rb_scan_args(argc, argv, "11", &args[0], &args[1]);
00949 if (rb_block_given_p()) {
00950 return rb_block_call(rb_cDir, rb_intern("glob"), n, args, glob_i, klass);
00951 }
00952 else {
00953 VALUE ary;
00954 long i;
00955 ary = rb_funcall2(rb_cDir, rb_intern("glob"), n, args);
00956 ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
00957 for (i = 0; i < RARRAY_LEN(ary); i++) {
00958 VALUE elt = RARRAY_PTR(ary)[i];
00959 elt = rb_class_new_instance(1, &elt, klass);
00960 rb_ary_store(ary, i, elt);
00961 }
00962 return ary;
00963 }
00964 }
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974 static VALUE
00975 path_s_getwd(VALUE klass)
00976 {
00977 VALUE str;
00978 str = rb_funcall(rb_cDir, rb_intern("getwd"), 0);
00979 return rb_class_new_instance(1, &str, klass);
00980 }
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008 static VALUE
01009 path_entries(VALUE self)
01010 {
01011 VALUE klass, str, ary;
01012 long i;
01013 klass = rb_obj_class(self);
01014 str = get_strpath(self);
01015 ary = rb_funcall(rb_cDir, rb_intern("entries"), 1, str);
01016 ary = rb_convert_type(ary, T_ARRAY, "Array", "to_ary");
01017 for (i = 0; i < RARRAY_LEN(ary); i++) {
01018 VALUE elt = RARRAY_PTR(ary)[i];
01019 elt = rb_class_new_instance(1, &elt, klass);
01020 rb_ary_store(ary, i, elt);
01021 }
01022 return ary;
01023 }
01024
01025
01026
01027
01028
01029
01030 static VALUE
01031 path_mkdir(int argc, VALUE *argv, VALUE self)
01032 {
01033 VALUE str = get_strpath(self);
01034 VALUE vmode;
01035 if (rb_scan_args(argc, argv, "01", &vmode) == 0)
01036 return rb_funcall(rb_cDir, rb_intern("mkdir"), 1, str);
01037 else
01038 return rb_funcall(rb_cDir, rb_intern("mkdir"), 2, str, vmode);
01039 }
01040
01041
01042
01043
01044
01045
01046 static VALUE
01047 path_rmdir(VALUE self)
01048 {
01049 return rb_funcall(rb_cDir, rb_intern("rmdir"), 1, get_strpath(self));
01050 }
01051
01052
01053
01054
01055
01056
01057 static VALUE
01058 path_opendir(VALUE self)
01059 {
01060 VALUE args[1];
01061
01062 args[0] = get_strpath(self);
01063 return rb_block_call(rb_cDir, rb_intern("open"), 1, args, 0, 0);
01064 }
01065
01066 static VALUE
01067 each_entry_i(VALUE elt, VALUE klass, int argc, VALUE *argv)
01068 {
01069 return rb_yield(rb_class_new_instance(1, &elt, klass));
01070 }
01071
01072
01073
01074
01075
01076 static VALUE
01077 path_each_entry(VALUE self)
01078 {
01079 VALUE args[1];
01080
01081 args[0] = get_strpath(self);
01082 return rb_block_call(rb_cDir, rb_intern("foreach"), 1, args, each_entry_i, rb_obj_class(self));
01083 }
01084
01085 static VALUE
01086 unlink_body(VALUE str)
01087 {
01088 return rb_funcall(rb_cDir, rb_intern("unlink"), 1, str);
01089 }
01090
01091 static VALUE
01092 unlink_rescue(VALUE str, VALUE errinfo)
01093 {
01094 return rb_funcall(rb_cFile, rb_intern("unlink"), 1, str);
01095 }
01096
01097
01098
01099
01100
01101 static VALUE
01102 path_unlink(VALUE self)
01103 {
01104 VALUE eENOTDIR = rb_const_get_at(rb_mErrno, rb_intern("ENOTDIR"));
01105 VALUE str = get_strpath(self);
01106 return rb_rescue2(unlink_body, str, unlink_rescue, str, eENOTDIR, (VALUE)0);
01107 }
01108
01109
01110
01111
01112 static VALUE
01113 path_f_pathname(VALUE self, VALUE str)
01114 {
01115 return rb_class_new_instance(1, &str, rb_cPathname);
01116 }
01117
01118
01119
01120
01121
01122
01123
01124
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137
01138
01139
01140
01141
01142
01143
01144
01145
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206
01207
01208
01209
01210
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234
01235
01236
01237
01238
01239
01240
01241
01242
01243
01244
01245
01246
01247
01248
01249
01250
01251
01252
01253
01254
01255
01256
01257
01258
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296
01297 void
01298 Init_pathname()
01299 {
01300 id_at_path = rb_intern("@path");
01301 id_to_path = rb_intern("to_path");
01302
01303 rb_cPathname = rb_define_class("Pathname", rb_cObject);
01304 rb_define_method(rb_cPathname, "initialize", path_initialize, 1);
01305 rb_define_method(rb_cPathname, "freeze", path_freeze, 0);
01306 rb_define_method(rb_cPathname, "taint", path_taint, 0);
01307 rb_define_method(rb_cPathname, "untaint", path_untaint, 0);
01308 rb_define_method(rb_cPathname, "==", path_eq, 1);
01309 rb_define_method(rb_cPathname, "===", path_eq, 1);
01310 rb_define_method(rb_cPathname, "eql?", path_eq, 1);
01311 rb_define_method(rb_cPathname, "<=>", path_cmp, 1);
01312 rb_define_method(rb_cPathname, "hash", path_hash, 0);
01313 rb_define_method(rb_cPathname, "to_s", path_to_s, 0);
01314 rb_define_method(rb_cPathname, "to_path", path_to_s, 0);
01315 rb_define_method(rb_cPathname, "inspect", path_inspect, 0);
01316 rb_define_method(rb_cPathname, "sub", path_sub, -1);
01317 rb_define_method(rb_cPathname, "sub_ext", path_sub_ext, 1);
01318 rb_define_method(rb_cPathname, "realpath", path_realpath, -1);
01319 rb_define_method(rb_cPathname, "realdirpath", path_realdirpath, -1);
01320 rb_define_method(rb_cPathname, "each_line", path_each_line, -1);
01321 rb_define_method(rb_cPathname, "read", path_read, -1);
01322 rb_define_method(rb_cPathname, "binread", path_binread, -1);
01323 rb_define_method(rb_cPathname, "readlines", path_readlines, -1);
01324 rb_define_method(rb_cPathname, "sysopen", path_sysopen, -1);
01325 rb_define_method(rb_cPathname, "atime", path_atime, 0);
01326 rb_define_method(rb_cPathname, "ctime", path_ctime, 0);
01327 rb_define_method(rb_cPathname, "mtime", path_mtime, 0);
01328 rb_define_method(rb_cPathname, "chmod", path_chmod, 1);
01329 rb_define_method(rb_cPathname, "lchmod", path_lchmod, 1);
01330 rb_define_method(rb_cPathname, "chown", path_chown, 2);
01331 rb_define_method(rb_cPathname, "lchown", path_lchown, 2);
01332 rb_define_method(rb_cPathname, "fnmatch", path_fnmatch, -1);
01333 rb_define_method(rb_cPathname, "fnmatch?", path_fnmatch, -1);
01334 rb_define_method(rb_cPathname, "ftype", path_ftype, 0);
01335 rb_define_method(rb_cPathname, "make_link", path_make_link, 1);
01336 rb_define_method(rb_cPathname, "open", path_open, -1);
01337 rb_define_method(rb_cPathname, "readlink", path_readlink, 0);
01338 rb_define_method(rb_cPathname, "rename", path_rename, 1);
01339 rb_define_method(rb_cPathname, "stat", path_stat, 0);
01340 rb_define_method(rb_cPathname, "lstat", path_lstat, 0);
01341 rb_define_method(rb_cPathname, "make_symlink", path_make_symlink, 1);
01342 rb_define_method(rb_cPathname, "truncate", path_truncate, 1);
01343 rb_define_method(rb_cPathname, "utime", path_utime, 2);
01344 rb_define_method(rb_cPathname, "basename", path_basename, -1);
01345 rb_define_method(rb_cPathname, "dirname", path_dirname, 0);
01346 rb_define_method(rb_cPathname, "extname", path_extname, 0);
01347 rb_define_method(rb_cPathname, "expand_path", path_expand_path, -1);
01348 rb_define_method(rb_cPathname, "split", path_split, 0);
01349 rb_define_method(rb_cPathname, "blockdev?", path_blockdev_p, 0);
01350 rb_define_method(rb_cPathname, "chardev?", path_chardev_p, 0);
01351 rb_define_method(rb_cPathname, "executable?", path_executable_p, 0);
01352 rb_define_method(rb_cPathname, "executable_real?", path_executable_real_p, 0);
01353 rb_define_method(rb_cPathname, "exist?", path_exist_p, 0);
01354 rb_define_method(rb_cPathname, "grpowned?", path_grpowned_p, 0);
01355 rb_define_method(rb_cPathname, "directory?", path_directory_p, 0);
01356 rb_define_method(rb_cPathname, "file?", path_file_p, 0);
01357 rb_define_method(rb_cPathname, "pipe?", path_pipe_p, 0);
01358 rb_define_method(rb_cPathname, "socket?", path_socket_p, 0);
01359 rb_define_method(rb_cPathname, "owned?", path_owned_p, 0);
01360 rb_define_method(rb_cPathname, "readable?", path_readable_p, 0);
01361 rb_define_method(rb_cPathname, "world_readable?", path_world_readable_p, 0);
01362 rb_define_method(rb_cPathname, "readable_real?", path_readable_real_p, 0);
01363 rb_define_method(rb_cPathname, "setuid?", path_setuid_p, 0);
01364 rb_define_method(rb_cPathname, "setgid?", path_setgid_p, 0);
01365 rb_define_method(rb_cPathname, "size", path_size, 0);
01366 rb_define_method(rb_cPathname, "size?", path_size_p, 0);
01367 rb_define_method(rb_cPathname, "sticky?", path_sticky_p, 0);
01368 rb_define_method(rb_cPathname, "symlink?", path_symlink_p, 0);
01369 rb_define_method(rb_cPathname, "writable?", path_writable_p, 0);
01370 rb_define_method(rb_cPathname, "world_writable?", path_world_writable_p, 0);
01371 rb_define_method(rb_cPathname, "writable_real?", path_writable_real_p, 0);
01372 rb_define_method(rb_cPathname, "zero?", path_zero_p, 0);
01373 rb_define_singleton_method(rb_cPathname, "glob", path_s_glob, -1);
01374 rb_define_singleton_method(rb_cPathname, "getwd", path_s_getwd, 0);
01375 rb_define_singleton_method(rb_cPathname, "pwd", path_s_getwd, 0);
01376 rb_define_method(rb_cPathname, "entries", path_entries, 0);
01377 rb_define_method(rb_cPathname, "mkdir", path_mkdir, -1);
01378 rb_define_method(rb_cPathname, "rmdir", path_rmdir, 0);
01379 rb_define_method(rb_cPathname, "opendir", path_opendir, 0);
01380 rb_define_method(rb_cPathname, "each_entry", path_each_entry, 0);
01381 rb_define_method(rb_cPathname, "unlink", path_unlink, 0);
01382 rb_define_method(rb_cPathname, "delete", path_unlink, 0);
01383 rb_undef_method(rb_cPathname, "=~");
01384 rb_define_global_function("Pathname", path_f_pathname, 1);
01385 }
01386