* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / ext / dl / handle.c
blob24269dfd78ffef19df99c4bb9d627b79c6fda7c8
1 /* -*- C -*-
2 * $Id$
3 */
5 #include <ruby.h>
6 #include "dl.h"
8 VALUE rb_cDLHandle;
10 void
11 dlhandle_free(struct dl_handle *dlhandle)
13 if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){
14 dlclose(dlhandle->ptr);
18 VALUE
19 rb_dlhandle_close(VALUE self)
21 struct dl_handle *dlhandle;
23 Data_Get_Struct(self, struct dl_handle, dlhandle);
24 dlhandle->open = 0;
25 return INT2NUM(dlclose(dlhandle->ptr));
28 VALUE
29 rb_dlhandle_s_allocate(VALUE klass)
31 VALUE obj;
32 struct dl_handle *dlhandle;
34 obj = Data_Make_Struct(rb_cDLHandle, struct dl_handle, 0,
35 dlhandle_free, dlhandle);
36 dlhandle->ptr = 0;
37 dlhandle->open = 0;
38 dlhandle->enable_close = 0;
40 return obj;
43 VALUE
44 rb_dlhandle_initialize(int argc, VALUE argv[], VALUE self)
46 void *ptr;
47 struct dl_handle *dlhandle;
48 VALUE lib, flag;
49 char *clib;
50 int cflag;
51 const char *err;
53 switch( rb_scan_args(argc, argv, "02", &lib, &flag) ){
54 case 0:
55 clib = NULL;
56 cflag = RTLD_LAZY | RTLD_GLOBAL;
57 break;
58 case 1:
59 clib = NIL_P(lib) ? NULL : StringValuePtr(lib);
60 cflag = RTLD_LAZY | RTLD_GLOBAL;
61 break;
62 case 2:
63 clib = NIL_P(lib) ? NULL : StringValuePtr(lib);
64 cflag = NUM2INT(flag);
65 break;
66 default:
67 rb_bug("rb_dlhandle_new");
70 ptr = dlopen(clib, cflag);
71 #if defined(HAVE_DLERROR)
72 if( !ptr && (err = dlerror()) ){
73 rb_raise(rb_eDLError, "%s", err);
75 #else
76 if( !ptr ){
77 err = dlerror();
78 rb_raise(rb_eDLError, "%s", err);
80 #endif
81 Data_Get_Struct(self, struct dl_handle, dlhandle);
82 if( dlhandle->ptr && dlhandle->open && dlhandle->enable_close ){
83 dlclose(dlhandle->ptr);
85 dlhandle->ptr = ptr;
86 dlhandle->open = 1;
87 dlhandle->enable_close = 0;
89 if( rb_block_given_p() ){
90 rb_ensure(rb_yield, self, rb_dlhandle_close, self);
93 return Qnil;
96 VALUE
97 rb_dlhandle_enable_close(VALUE self)
99 struct dl_handle *dlhandle;
101 Data_Get_Struct(self, struct dl_handle, dlhandle);
102 dlhandle->enable_close = 1;
103 return Qnil;
106 VALUE
107 rb_dlhandle_disable_close(VALUE self)
109 struct dl_handle *dlhandle;
111 Data_Get_Struct(self, struct dl_handle, dlhandle);
112 dlhandle->enable_close = 0;
113 return Qnil;
116 VALUE
117 rb_dlhandle_to_i(VALUE self)
119 struct dl_handle *dlhandle;
121 Data_Get_Struct(self, struct dl_handle, dlhandle);
122 return PTR2NUM(dlhandle);
125 VALUE
126 rb_dlhandle_sym(VALUE self, VALUE sym)
128 void (*func)();
129 struct dl_handle *dlhandle;
130 void *handle;
131 const char *name;
132 const char *err;
133 int i;
135 #if defined(HAVE_DLERROR)
136 # define CHECK_DLERROR if( err = dlerror() ){ func = 0; }
137 #else
138 # define CHECK_DLERROR
139 #endif
141 rb_secure(2);
143 if( sym == Qnil ){
144 #if defined(RTLD_NEXT)
145 name = RTLD_NEXT;
146 #else
147 name = NULL;
148 #endif
150 else{
151 name = StringValuePtr(sym);
155 Data_Get_Struct(self, struct dl_handle, dlhandle);
156 if( ! dlhandle->open ){
157 rb_raise(rb_eDLError, "closed handle");
159 handle = dlhandle->ptr;
161 func = dlsym(handle, name);
162 CHECK_DLERROR;
163 if( !func ){
164 #if defined(__CYGWIN__) || defined(WIN32) || defined(__MINGW32__)
166 int len = strlen(name);
167 char *name_a = (char*)xmalloc(len+2);
168 strcpy(name_a, name);
169 name_a[len] = 'A';
170 name_a[len+1] = '\0';
171 func = dlsym(handle, name_a);
172 xfree(name_a);
173 CHECK_DLERROR;
174 if( !func ){
175 for( i = 0; i < 256; i += 4 ){
176 int len = strlen(name);
177 char *name_n = (char*)xmalloc(len+5);
178 sprintf(name_n, "%s@%d%c", name, i, 0);
179 func = dlsym(handle, name_n);
180 xfree(name_n);
181 CHECK_DLERROR;
182 if( func )
184 break;
187 CHECK_DLERROR;
188 if( !func ){
189 rb_raise(rb_eDLError, "unknown symbol \"%s\"", name);
193 #else
194 for( i = 0; i < 256; i += 4 ){
195 int len = strlen(name);
196 char *name_n = (char*)xmalloc(len+4);
197 sprintf(name_n, "%s@%d", name, i);
198 func = dlsym(handle, name_n);
199 xfree(name_n);
200 CHECK_DLERROR;
201 if( func ){
202 break;
205 CHECK_DLERROR;
206 if( !func ){
207 rb_raise(rb_eDLError, "unknown symbol \"%s\"", name);
209 #endif
212 return PTR2NUM(func);
215 void
216 Init_dlhandle()
218 rb_cDLHandle = rb_define_class_under(rb_mDL, "Handle", rb_cObject);
219 rb_define_alloc_func(rb_cDLHandle, rb_dlhandle_s_allocate);
220 rb_define_method(rb_cDLHandle, "initialize", rb_dlhandle_initialize, -1);
221 rb_define_method(rb_cDLHandle, "to_i", rb_dlhandle_to_i, 0);
222 rb_define_method(rb_cDLHandle, "close", rb_dlhandle_close, 0);
223 rb_define_method(rb_cDLHandle, "sym", rb_dlhandle_sym, 1);
224 rb_define_method(rb_cDLHandle, "[]", rb_dlhandle_sym, 1);
225 rb_define_method(rb_cDLHandle, "disable_close", rb_dlhandle_disable_close, 0);
226 rb_define_method(rb_cDLHandle, "enable_close", rb_dlhandle_enable_close, 0);