From: djberg96@... (Daniel Berger) Date: 2002-04-17T05:04:06+09:00 Subject: Help wanted for statvfs extension Hi all, A few people (including myself) have asked about a ruby interface to the unix 'df' command. Since there doesn't seem to be one (at least, not one that isn't calling 'system' or 'syscall' at some point), I thought I'd give it a shot. First, I should mention that I wrote this with Ruby 1.6.6 on Solaris 8. This is my first attempt at a C extension (no laughing please!). I have no idea how portable it is. Mostly what I'm looking for are opinions on how I've set things up, if there are any potential memory leaks, and whether or not I should make this a subclass of File::Stat. I realize that there are plenty of missing methods. Once everything else is established, the methods will come easy (since they'll just be struct attribute values). Anyway, here's the code (below the build instructions). The module is called 'statvfs'. The constructor is 'Statvfs.new'. The one and only method at the moment is 'favail'. Any and all help/opinions are appreciated. Thanks! Regards, Daniel Berger # Instructions To build, create a file called 'extconf.rb' and add these two lines: require 'mkmf' create_makefile("statvfs") Then do: ruby extconf.rb make make install Then, in a separate script do: require 'statvfs' df = Statvfs.new favail = df.favail('/tmp') # or whatever puts favail # SOURCE CODE - statvfs.c /***************************** Statvfs.c $Author: djberg96 $ $Date: 2002/04/15 12:00:00 $ ******************************/ #include "ruby.h" #include VALUE info; VALUE statvfs_new(VALUE class) { statvfs_t *p; p = ALLOC(statvfs_t); info = Data_Wrap_Struct(class,0,free,p); return info; } VALUE statvfs_favail(VALUE self, VALUE dir) { statvfs_t *p; int favail; Data_Get_Struct(self,statvfs_t,p); statvfs(STR2CSTR(dir),p); favail = p->f_favail; return rb_int_new(favail); } static VALUE mStatvfs; VALUE cStatvfs; void Init_statvfs() { mStatvfs = rb_define_module("statvfs"); cStatvfs = rb_define_class("Statvfs",rb_cObject); rb_define_singleton_method(cStatvfs,"new",statvfs_new,0); rb_define_method(cStatvfs,"favail",statvfs_favail,1); }