ext/fiddle/handle.c in fiddle-1.0.9 vs ext/fiddle/handle.c in fiddle-1.1.0
- old
+ new
@@ -257,13 +257,27 @@
rb_fiddle_handle_to_i(VALUE self)
{
struct dl_handle *fiddle_handle;
TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
- return PTR2NUM(fiddle_handle);
+ return PTR2NUM(fiddle_handle->ptr);
}
+/*
+ * call-seq: to_ptr
+ *
+ * Returns the Fiddle::Pointer of this handle.
+ */
+static VALUE
+rb_fiddle_handle_to_ptr(VALUE self)
+{
+ struct dl_handle *fiddle_handle;
+
+ TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
+ return rb_fiddle_ptr_new_wrap(fiddle_handle->ptr, 0, 0, self, 0);
+}
+
static VALUE fiddle_handle_sym(void *handle, VALUE symbol);
/*
* Document-method: sym
*
@@ -370,10 +384,52 @@
}
return PTR2NUM(func);
}
+/*
+ * call-seq: file_name
+ *
+ * Returns the file name of this handle.
+ */
+static VALUE
+rb_fiddle_handle_file_name(VALUE self)
+{
+ struct dl_handle *fiddle_handle;
+
+ TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
+
+#if defined(HAVE_DLINFO) && defined(HAVE_CONST_RTLD_DI_LINKMAP)
+ {
+ struct link_map *lm = NULL;
+ int res = dlinfo(fiddle_handle->ptr, RTLD_DI_LINKMAP, &lm);
+ if (res == 0 && lm != NULL) {
+ return rb_str_new_cstr(lm->l_name);
+ }
+ else {
+#if defined(HAVE_DLERROR)
+ rb_raise(rb_eFiddleDLError, "could not get handle file name: %s", dlerror());
+#else
+ rb_raise(rb_eFiddleDLError, "could not get handle file name");
+#endif
+ }
+ }
+#elif defined(HAVE_GETMODULEFILENAME)
+ {
+ char filename[MAX_PATH];
+ DWORD res = GetModuleFileName(fiddle_handle->ptr, filename, MAX_PATH);
+ if (res == 0) {
+ rb_raise(rb_eFiddleDLError, "could not get handle file name: %s", dlerror());
+ }
+ return rb_str_new_cstr(filename);
+ }
+#else
+ (void)fiddle_handle;
+ return Qnil;
+#endif
+}
+
void
Init_fiddle_handle(void)
{
/*
* Document-class: Fiddle::Handle
@@ -464,12 +520,14 @@
*/
rb_define_const(rb_cHandle, "RTLD_NOW", INT2NUM(RTLD_NOW));
rb_define_method(rb_cHandle, "initialize", rb_fiddle_handle_initialize, -1);
rb_define_method(rb_cHandle, "to_i", rb_fiddle_handle_to_i, 0);
+ rb_define_method(rb_cHandle, "to_ptr", rb_fiddle_handle_to_ptr, 0);
rb_define_method(rb_cHandle, "close", rb_fiddle_handle_close, 0);
rb_define_method(rb_cHandle, "sym", rb_fiddle_handle_sym, 1);
rb_define_method(rb_cHandle, "[]", rb_fiddle_handle_sym, 1);
+ rb_define_method(rb_cHandle, "file_name", rb_fiddle_handle_file_name, 0);
rb_define_method(rb_cHandle, "disable_close", rb_fiddle_handle_disable_close, 0);
rb_define_method(rb_cHandle, "enable_close", rb_fiddle_handle_enable_close, 0);
rb_define_method(rb_cHandle, "close_enabled?", rb_fiddle_handle_close_enabled_p, 0);
}