diff options
author | ttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-04-02 10:56:13 +0000 |
---|---|---|
committer | ttate <ttate@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2002-04-02 10:56:13 +0000 |
commit | 7d711b817e62eb6c8dee01ea2283fcb1ad90f8ac (patch) | |
tree | b1b54ab76217775071de4e3669674aa7887aa6b9 /ext/dl/sample/libc.rb | |
parent | 64b6406445e53f187d2982f87becff8065edd0cc (diff) | |
download | ruby-7d711b817e62eb6c8dee01ea2283fcb1ad90f8ac.tar.gz |
Add ruby-dl
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2324 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/dl/sample/libc.rb')
-rw-r--r-- | ext/dl/sample/libc.rb | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/ext/dl/sample/libc.rb b/ext/dl/sample/libc.rb new file mode 100644 index 0000000000..c88fc687f2 --- /dev/null +++ b/ext/dl/sample/libc.rb @@ -0,0 +1,84 @@ +require 'dl' + +module LIBC + begin + LIB = DL.dlopen('libc.so.6') + rescue RuntimeError + LIB = DL.dlopen('libc.so.5') + end + + SYM = { + :atoi => LIB['atoi', 'IS'], + :isdigit => LIB['isdigit', 'II'], + } + + def atoi(str) + r,rs = SYM[:atoi].call(str) + return r + end + + def isdigit(c) + r,rs = SYM[:isdigit].call(c) + return (r != 0) + end +end + +module LIBC + SYM[:strcat] = LIB['strcat', 'SsS'] + def strcat(str1,str2) + r,rs = SYM[:strcat].call(str1 + "\0#{str2}",str2) + return rs[0] + end +end + +module LIBC + SYM[:fopen] = LIB['fopen', 'PSS'] + SYM[:fclose] = LIB['fclose', '0P'] + SYM[:fgetc] = LIB['fgetc', 'IP'] + + def fopen(filename, mode) + r,rs = SYM[:fopen].call(filename, mode) + return r + end + + def fclose(ptr) + SYM[:fclose].call(ptr) + return nil + end + + def fgetc(ptr) + r,rs = SYM[:fgetc].call(ptr) + return r + end +end + +module LIBC + SYM[:strlen] = LIB['strlen', 'IP'] + def strlen(str) + r,rs = SYM[:strlen].call(str) + return r + end +end + +$cb1 = DL.set_callback('IPP', 0){|ptr1, ptr2| + str1 = ptr1.ptr.to_s + str2 = ptr2.ptr.to_s + str1 <=> str2 +} + +module LIBC + SYM[:qsort] = LIB['qsort', '0aIIP'] + def qsort(ary, comp) + len = ary.length + r,rs = SYM[:qsort].call(ary, len, DL.sizeof('P'), comp) + return rs[0].to_a('S', len) + end +end + +include LIBC + +p atoi("10") +p isdigit(?1) +p isdigit(?a) +p strcat("a", "b") +p qsort(["a","c","b"],$cb1) |