From 8428347e99598e7808575d97becfaf0c881af3cc Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 24 Feb 2009 15:45:16 +0000 Subject: [PATCH] * file.c (rb_file_size): new method added. [ruby-core:22088] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22604 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 ++++ file.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/ChangeLog b/ChangeLog index df31271b1c..3cd60e0f0e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -6,6 +6,10 @@ Wed Feb 25 00:41:21 2009 Akinori MUSHA * ext/socket/tcpserver.c (tcp_svr_init): Make use of it. +Wed Feb 25 00:28:25 2009 Yukihiro Matsumoto + + * file.c (rb_file_size): new method added. [ruby-core:22088] + Wed Feb 25 00:15:15 2009 Yukihiro Matsumoto * ext/curses/curses.c (curses_colors): new method added. a patch diff --git a/file.c b/file.c index d73fdf86fd..98f7ecf7db 100644 --- a/file.c +++ b/file.c @@ -1807,6 +1807,22 @@ rb_file_ctime(VALUE obj) return stat_ctime(&st); } +static VALUE +rb_file_size(VALUE obj) +{ + rb_io_t *fptr; + struct stat st; + + GetOpenFile(obj, fptr); + if (fptr->mode & FMODE_WRITABLE) { + rb_io_flush(obj); + } + if (fstat(fptr->fd, &st) == -1) { + rb_sys_fail_path(fptr->pathv); + } + return OFFT2NUM(st.st_size); +} + static void chmod_internal(const char *path, void *mode) { @@ -4766,6 +4782,7 @@ Init_File(void) rb_define_method(rb_cFile, "atime", rb_file_atime, 0); rb_define_method(rb_cFile, "mtime", rb_file_mtime, 0); rb_define_method(rb_cFile, "ctime", rb_file_ctime, 0); + rb_define_method(rb_cFile, "size", rb_file_size, 0); rb_define_method(rb_cFile, "chmod", rb_file_chmod, 1); rb_define_method(rb_cFile, "chown", rb_file_chown, 2);