2014-01-28 22:37:09 +00:00
|
|
|
import ffi
|
2018-10-20 10:36:48 +01:00
|
|
|
import uctypes
|
2014-01-28 22:37:09 +00:00
|
|
|
|
|
|
|
libc = ffi.open("libc.so.6")
|
|
|
|
print("libc:", libc)
|
|
|
|
print()
|
|
|
|
|
|
|
|
# Declare few functions
|
2016-06-17 17:21:37 +01:00
|
|
|
perror = libc.func("v", "perror", "s")
|
2014-01-28 22:37:09 +00:00
|
|
|
time = libc.func("i", "time", "p")
|
2016-06-17 17:21:37 +01:00
|
|
|
open = libc.func("i", "open", "si")
|
2018-10-20 10:36:48 +01:00
|
|
|
qsort = libc.func("v", "qsort", "piiC")
|
2014-01-28 22:37:09 +00:00
|
|
|
# And one variable
|
|
|
|
errno = libc.var("i", "errno")
|
|
|
|
|
|
|
|
print("time:", time)
|
|
|
|
print("UNIX time is:", time(None))
|
|
|
|
print()
|
|
|
|
|
2018-10-20 10:36:48 +01:00
|
|
|
perror("perror before error")
|
2014-01-28 22:37:09 +00:00
|
|
|
open("somethingnonexistent__", 0)
|
2016-06-17 17:24:58 +01:00
|
|
|
print("errno object:", errno)
|
|
|
|
print("errno value:", errno.get())
|
2018-10-20 10:36:48 +01:00
|
|
|
perror("perror after error")
|
2014-01-28 22:37:09 +00:00
|
|
|
print()
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2014-01-28 22:37:09 +00:00
|
|
|
def cmp(pa, pb):
|
2018-10-20 10:36:48 +01:00
|
|
|
a = uctypes.bytearray_at(pa, 1)
|
|
|
|
b = uctypes.bytearray_at(pb, 1)
|
2014-01-28 22:37:09 +00:00
|
|
|
print("cmp:", a, b)
|
|
|
|
return a[0] - b[0]
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
|
2018-10-20 10:36:48 +01:00
|
|
|
cmp_cb = ffi.callback("i", cmp, "PP")
|
|
|
|
print("callback:", cmp_cb)
|
2014-01-28 22:37:09 +00:00
|
|
|
|
2016-06-17 17:24:58 +01:00
|
|
|
s = bytearray(b"foobar")
|
2014-01-28 22:37:09 +00:00
|
|
|
print("org string:", s)
|
2018-10-20 10:36:48 +01:00
|
|
|
qsort(s, len(s), 1, cmp_cb)
|
2016-06-17 17:24:58 +01:00
|
|
|
print("qsort'ed string:", s)
|