2002-04-11 04:07:01 -04:00
|
|
|
require "dl/import"
|
|
|
|
require "dl/struct"
|
2002-04-02 05:56:13 -05:00
|
|
|
|
|
|
|
module LIBC
|
2002-04-11 04:07:01 -04:00
|
|
|
extend DL::Importable
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
begin
|
|
|
|
dlload "libc.so.6"
|
|
|
|
rescue
|
|
|
|
dlload "libc.so.5"
|
2002-04-02 05:56:13 -05:00
|
|
|
end
|
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
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*)"
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
def str_qsort(ary, comp)
|
|
|
|
len = ary.length
|
|
|
|
r,rs = qsort(ary, len, DL.sizeof('P'), comp)
|
|
|
|
return rs[0].to_a('S', len)
|
2002-04-02 05:56:13 -05:00
|
|
|
end
|
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
Timeval = struct [
|
|
|
|
"long tv_sec",
|
|
|
|
"long tv_usec",
|
|
|
|
]
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
Timezone = struct [
|
|
|
|
"int tz_minuteswest",
|
|
|
|
"int tz_dsttime",
|
|
|
|
]
|
2002-06-10 14:23:39 -04:00
|
|
|
|
|
|
|
def my_compare(ptr1, ptr2)
|
|
|
|
ptr1.ptr.to_s <=> ptr2.ptr.to_s
|
|
|
|
end
|
|
|
|
COMPARE = callback "int my_compare(char**, char**)"
|
2002-04-02 05:56:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2002-06-09 13:47:34 -04:00
|
|
|
$cb1 = DL.callback('IPP'){|ptr1, ptr2|
|
2002-04-02 05:56:13 -05:00
|
|
|
str1 = ptr1.ptr.to_s
|
|
|
|
str2 = ptr2.ptr.to_s
|
|
|
|
str1 <=> str2
|
|
|
|
}
|
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
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
|
2002-06-10 14:23:39 -04:00
|
|
|
LIBC.qsort(ptr, ary.length, DL.sizeof('P'), LIBC::COMPARE)
|
2002-04-11 04:07:01 -04:00
|
|
|
p ptr.to_a('S', ary.length)
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-05-20 11:35:48 -04:00
|
|
|
tv = LIBC::Timeval.malloc
|
|
|
|
tz = LIBC::Timezone.malloc
|
2002-04-11 04:07:01 -04:00
|
|
|
LIBC.gettimeofday(tv, tz)
|
2002-04-02 05:56:13 -05:00
|
|
|
|
2002-04-11 04:07:01 -04:00
|
|
|
p Time.at(tv.tv_sec)
|