summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2015-05-16 11:46:45 +0200
committerArmin Rigo <arigo@tunes.org>2015-05-16 11:46:45 +0200
commitdd66364acdb991b9219f59ceefb19b7c87099059 (patch)
tree2aba98915bb9e6de4b96034db9437982cdb52e72 /demo
parente9eb36247788e807353ff0b9649fae8628a9b828 (diff)
downloadcffi-dd66364acdb991b9219f59ceefb19b7c87099059.tar.gz
Modernize this dlopen() demo
Diffstat (limited to 'demo')
-rw-r--r--demo/readdir.py36
-rw-r--r--demo/readdir_build.py33
2 files changed, 39 insertions, 30 deletions
diff --git a/demo/readdir.py b/demo/readdir.py
index e63c85a..b966246 100644
--- a/demo/readdir.py
+++ b/demo/readdir.py
@@ -1,49 +1,25 @@
# A Linux-only demo
#
import sys
-from cffi import FFI
if not sys.platform.startswith('linux'):
raise Exception("Linux-only demo")
-
-ffi = FFI()
-ffi.cdef("""
-
- typedef void DIR;
- typedef long ino_t;
- typedef long off_t;
-
- struct dirent {
- ino_t d_ino; /* inode number */
- off_t d_off; /* offset to the next dirent */
- unsigned short d_reclen; /* length of this record */
- unsigned char d_type; /* type of file; not supported
- by all file system types */
- char d_name[256]; /* filename */
- };
-
- int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
- int openat(int dirfd, const char *pathname, int flags);
- DIR *fdopendir(int fd);
- int closedir(DIR *dirp);
-
-""")
-ffi.C = ffi.dlopen(None)
-
+from _readdir import ffi
+lib = ffi.dlopen(None)
def walk(basefd, path):
print '{', path
- dirfd = ffi.C.openat(basefd, path, 0)
+ dirfd = lib.openat(basefd, path, 0)
if dirfd < 0:
# error in openat()
return
- dir = ffi.C.fdopendir(dirfd)
+ dir = lib.fdopendir(dirfd)
dirent = ffi.new("struct dirent *")
result = ffi.new("struct dirent **")
while True:
- if ffi.C.readdir_r(dir, dirent, result):
+ if lib.readdir_r(dir, dirent, result):
# error in readdir_r()
break
if result[0] == ffi.NULL:
@@ -52,7 +28,7 @@ def walk(basefd, path):
print '%3d %s' % (dirent.d_type, name)
if dirent.d_type == 4 and name != '.' and name != '..':
walk(dirfd, name)
- ffi.C.closedir(dir)
+ lib.closedir(dir)
print '}'
diff --git a/demo/readdir_build.py b/demo/readdir_build.py
new file mode 100644
index 0000000..f97f404
--- /dev/null
+++ b/demo/readdir_build.py
@@ -0,0 +1,33 @@
+import sys
+from cffi import FFI
+
+if not sys.platform.startswith('linux'):
+ raise Exception("Linux-only demo")
+
+
+ffi = FFI()
+ffi.cdef("""
+
+ typedef void DIR;
+ typedef long ino_t;
+ typedef long off_t;
+
+ struct dirent {
+ ino_t d_ino; /* inode number */
+ off_t d_off; /* offset to the next dirent */
+ unsigned short d_reclen; /* length of this record */
+ unsigned char d_type; /* type of file; not supported
+ by all file system types */
+ char d_name[256]; /* filename */
+ };
+
+ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
+ int openat(int dirfd, const char *pathname, int flags);
+ DIR *fdopendir(int fd);
+ int closedir(DIR *dirp);
+
+""")
+ffi.set_source("_readdir", None)
+
+if __name__ == '__main__':
+ ffi.compile()