summaryrefslogtreecommitdiff
path: root/ruby_1_8_5/ext/dl/sample/libc.rb
diff options
context:
space:
mode:
authorshyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-15 23:23:39 +0000
committershyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-15 23:23:39 +0000
commit6175ca03be6d0d51359f9017123708987d0f5eb7 (patch)
treeecfcf6e79a21b1d25c3f6f42dd68ea0a14add89c /ruby_1_8_5/ext/dl/sample/libc.rb
parent80a56b248b2e9cfc95622aed98750df05a19f667 (diff)
add tag v1_8_5_91v1_8_5_91
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/tags/v1_8_5_91@13046 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ruby_1_8_5/ext/dl/sample/libc.rb')
-rw-r--r--ruby_1_8_5/ext/dl/sample/libc.rb69
1 files changed, 69 insertions, 0 deletions
diff --git a/ruby_1_8_5/ext/dl/sample/libc.rb b/ruby_1_8_5/ext/dl/sample/libc.rb
new file mode 100644
index 0000000000..a1f6fbe543
--- /dev/null
+++ b/ruby_1_8_5/ext/dl/sample/libc.rb
@@ -0,0 +1,69 @@
+require "dl/import"
+require "dl/struct"
+
+module LIBC
+ extend DL::Importable
+
+ begin
+ dlload "libc.so.6"
+ rescue
+ dlload "libc.so.5"
+ end
+
+ extern "int atoi(char*)"
+ extern "ibool isdigit(int)"
+ extern "int gettimeofday(struct timeval *, struct timezone *)"
+ extern "char* strcat(char*, char*)"
+ extern "FILE* fopen(char*, char*)"
+ extern "int fclose(FILE*)"
+ extern "int fgetc(FILE*)"
+ extern "int strlen(char*)"
+ extern "void qsort(void*, int, int, void*)"
+
+ def str_qsort(ary, comp)
+ len = ary.length
+ r,rs = qsort(ary, len, DL.sizeof('P'), comp)
+ return rs[0].to_a('S', len)
+ end
+
+ Timeval = struct [
+ "long tv_sec",
+ "long tv_usec",
+ ]
+
+ Timezone = struct [
+ "int tz_minuteswest",
+ "int tz_dsttime",
+ ]
+
+ def my_compare(ptr1, ptr2)
+ ptr1.ptr.to_s <=> ptr2.ptr.to_s
+ end
+ COMPARE = callback("int my_compare(char**, char**)")
+end
+
+
+$cb1 = DL.callback('IPP'){|ptr1, ptr2|
+ str1 = ptr1.ptr.to_s
+ str2 = ptr2.ptr.to_s
+ str1 <=> str2
+}
+
+p LIBC.atoi("10")
+
+p LIBC.isdigit(?1)
+
+p LIBC.isdigit(?a)
+
+p LIBC.strcat("a", "b")
+
+ary = ["a","c","b"]
+ptr = ary.to_ptr
+LIBC.qsort(ptr, ary.length, DL.sizeof('P'), LIBC::COMPARE)
+p ptr.to_a('S', ary.length)
+
+tv = LIBC::Timeval.malloc
+tz = LIBC::Timezone.malloc
+LIBC.gettimeofday(tv, tz)
+
+p Time.at(tv.tv_sec)