From: nobu.nokada@... Date: 2003-12-02T14:15:30+09:00 Subject: Re: File::Stat and file flags Hi, At Tue, 2 Dec 2003 00:31:38 +0900, YANAGAWA Kazuhisa wrote: > I think supporting file flags is too *BSD specific to incorporate into > standard interpreter though. So it should be "BSD" extension library? Although not tested at all, and lacks constants and particular methods, nodump?, opaque? and so on. ===File bsd/stat.c========================================== #include #include #include "ruby.h" #include "rubyio.h" static VALUE stat_inspect; static struct stat* get_stat(self) VALUE self; { struct stat* st; Data_Get_Struct(self, struct stat, st); if (!st) rb_raise(rb_eTypeError, "uninitialized File::Stat"); return st; } static VALUE bsd_stat_flags(VALUE self) { return ULONG2NUM(get_stat(self)->st_flags); } static VALUE bsd_stat_inspect(VALUE self) { char tmp[sizeof(((struct stat*)0)->st_flags)*2+5]; VALUE bm = rb_funcall2(stat_inspect, rb_intern("bind"), 1, &self); VALUE str = rb_funcall2(bm, rb_intern("call"), 0, 0); snprintf(sizeof(tmp), ", 0x%lx", (unsigned long)get_stat(self)->st_flags); rb_str_update(str, -2, 0, rb_str_new2(tmp)); return str; } static VALUE bsd_chflags(VALUE self, VALUE flags) { OpenFile *fptr; rb_secure(2); GetOpenFile(self, fptr); if (fchflags(fileno(fptr->f), NUM2ULONG(flags))) { rb_sys_fail(0); } return INT2FIX(0); } static VALUE bsd_s_chflags(VALUE self, VALUE file, VALUE flags) { VALUE tmp = rb_check_convert_type(file, T_FILE, "IO", "to_io"); if (!NIL_P(tmp)) { return bsd_chflags(tmp, flags); } SafeStringValue(file); if (chflags(StringValueCStr(file), NUM2ULONG(flags))) { rb_sys_fail(0); } return INT2FIX(0); } void Init_stat() { VALUE stat = rb_const_get_at(rb_cFile, rb_intern("Stat")); stat_inspect = rb_funcall(stat, rb_intern("instance_method"), 1, ID2SYM(rb_intern("inspect"))); rb_define_method(stat, "inspect", bsd_stat_inspect, 0); rb_define_method(stat, "flags", bsd_stat_flags, 0); rb_define_method(rb_cFile, "chflags", bsd_chflags, 1); rb_define_singleton_method(rb_cFile, "chflags", bsd_s_chflags, 2); } ============================================================ -- Nobu Nakada