From: Victor Manuel Reyes Viloria Date: 2002-04-24T03:02:23+09:00 Subject: Re: Hostname command Perhaps I don't know what you guys are talking about, but I get my windows hostname with either one of these expressions: hn = system("hostname") puts hn or the following": puts system("hostname") Victor |---------+----------------------------> | | djberg96@hotmail.| | | com (Daniel | | | Berger) | | | | | | 04/23/2002 01:15 | | | PM | | | Please respond to| | | ruby-talk | | | | |---------+----------------------------> >---------------------------------------------------------------------------------------------------------------------------------------------| | | | To: ruby-talk@ruby-lang.org (ruby-talk ML) | | cc: | | Subject: Re: Hostname command | | | | | >---------------------------------------------------------------------------------------------------------------------------------------------| ts wrote in message news:<200204231413.g3NEDbr01417@moulon.inra.fr>... > >>>>> "D" == Daniel Berger writes: > > D> Is there a 'hostname' command in Ruby (short of backticks, I mean)? > > Well, you can do > > pigeon% ruby -rsocket -e 'p Socket.gethostname' > "pigeon" > pigeon% > > > Guy Decoux Hmmm...forgot about that (oops). Seems like overkill just to get the hostname, though. Well, I went and wrote an extension anyway, on the notion that I might add other commands (such as 'mailhost') to it someday. Here it is. It's not Windows friendly, and I don't check the rv on the gethostname call (yet). In fact, I'm not even sure *how* to get the hostname on Windows (via a C extension, I mean. I realize that it's probably possible with the Win32API). Any help on that would be appreciated. Enjoy. Dan #include "ruby.h" #if defined(I_UNISTD) && defined(HAS_GETHOSTNAME) #include #endif /* a reasonable default */ #ifndef MAXHOSTNAMELEN #define MAXHOSTNAMELEN 256 #endif #ifdef I_SYSUTSNAME #include #endif #ifdef __cplusplus extern "C" { #endif /* set up a struct with the notion of adding other things (such as mailhost, etc) in the future */ struct mystruct{ char* hostname; }; typedef struct mystruct sys_struct; VALUE mSys; VALUE cSys; static void sys_free(void* p) { free(p); } static VALUE sys_hostname(VALUE self) { return rb_iv_get(self,"@hostname"); } static VALUE sys_init(VALUE self, VALUE hostname) { rb_iv_set(self,"@hostname",hostname); return self; } VALUE sys_new(VALUE class) { VALUE argv[1]; VALUE sdata; sys_struct *p; char* h; int rv; sdata = Data_Make_Struct(class,sys_struct,0,sys_free,p); rv = gethostname(h,MAXHOSTNAMELEN); argv[0] = rb_str_new2(h); rb_obj_call_init(sdata,1,argv); return sdata; } void Init_Sys() { mSys = rb_define_module("sys"); cSys = rb_define_class("Sys",rb_cObject); rb_define_singleton_method(cSys,"new",sys_new,0); rb_define_method(cSys,"hostname",sys_hostname,0); rb_define_method(cSys,"initialize",sys_init,1); } #ifdef __cplusplus } #endif #