diff --git a/dir.c b/dir.c index 1367ed0..b60432d 100644 --- a/dir.c +++ b/dir.c @@ -2017,6 +2017,62 @@ dir_s_home(int argc, VALUE *argv, VALUE obj) } /* + * call-seq: + * dir.path_max -> 1024 + * + * Returns the maximum number of bytes in a path name relative to the + * directory object on which it is called. + */ +static VALUE +dir_path_max(VALUE dir) +{ + long max_path; + struct dir_data *dirp; + + TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp); + + errno = 0; + max_path = fpathconf(dirfd(dirp->dir), _PC_PATH_MAX); + if (max_path == -1) { + if (errno == 0) { + return Qnil; /* no error, but pathconf has no value */ + } else { + rb_bug_errno("max path", errno); + } + } + + return LONG2NUM(max_path); +} + +/* + * call-seq: + * dir.name_max -> 1024 + * + * Returns the maximum number of bytes in a file name relative to the + * directory object on which it is called. + */ +static VALUE +dir_name_max(VALUE dir) +{ + long max_name; + struct dir_data *dirp; + + TypedData_Get_Struct(dir, struct dir_data, &dir_data_type, dirp); + + errno = 0; + max_name = fpathconf(dirfd(dirp->dir), _PC_NAME_MAX); + if (max_name == -1) { + if (errno == 0) { + return Qnil; /* no error, but pathconf has no value */ + } else { + rb_bug_errno("max name", errno); + } + } + + return LONG2NUM(max_name); +} + +/* * Objects of class Dir are directory streams representing * directories in the underlying file system. They provide a variety of * ways to list directories and their contents. See also @@ -2041,6 +2097,8 @@ Init_Dir(void) rb_define_method(rb_cDir,"initialize", dir_initialize, -1); rb_define_method(rb_cDir,"path", dir_path, 0); + rb_define_method(rb_cDir,"path_max", dir_path_max, 0); + rb_define_method(rb_cDir,"name_max", dir_name_max, 0); rb_define_method(rb_cDir,"to_path", dir_path, 0); rb_define_method(rb_cDir,"inspect", dir_inspect, 0); rb_define_method(rb_cDir,"read", dir_read, 0); diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 236fd99..5063756 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -25,6 +25,14 @@ class TestDir < Test::Unit::TestCase FileUtils.remove_entry_secure @root if File.directory?(@root) end + def test_path_max + assert_operator Dir.new(@root).path_max, :>, 0 + end + + def test_name_max + assert_operator Dir.new(@root).name_max, :>, 0 + end + def test_seek dir = Dir.open(@root) begin