summaryrefslogtreecommitdiff
path: root/demo
diff options
context:
space:
mode:
authorArmin Rigo <arigo@tunes.org>2012-06-14 12:51:26 +0200
committerArmin Rigo <arigo@tunes.org>2012-06-14 12:51:26 +0200
commitc543522a5596b72c07fdae6ca1fd3cae5d0a276a (patch)
tree8bb1749f8af6d9bcca3c53a0634e348197b19e91 /demo
parenta14f8dc765cda6c8cb264fbf4fb148be085f73de (diff)
downloadcffi-c543522a5596b72c07fdae6ca1fd3cae5d0a276a.tar.gz
Add a second demo using verify().
Diffstat (limited to 'demo')
-rw-r--r--demo/readdir.py1
-rw-r--r--demo/readdir2.py56
2 files changed, 57 insertions, 0 deletions
diff --git a/demo/readdir.py b/demo/readdir.py
index 8deeb20..a5c4a35 100644
--- a/demo/readdir.py
+++ b/demo/readdir.py
@@ -24,6 +24,7 @@ ffi.cdef("""
int closedir(DIR *dirp);
""")
+ffi.C = ffi.rawload(None)
diff --git a/demo/readdir2.py b/demo/readdir2.py
new file mode 100644
index 0000000..bec3f32
--- /dev/null
+++ b/demo/readdir2.py
@@ -0,0 +1,56 @@
+# A Linux-only demo, using verify() instead of hard-coding the exact layouts
+#
+from cffi import FFI
+
+
+ffi = FFI()
+ffi.cdef("""
+
+ typedef ... DIR;
+
+ struct dirent {
+ unsigned char d_type;
+ char d_name[];
+ ...;
+ };
+
+ 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.verify("""
+#ifndef _ATFILE_SOURCE
+# define _ATFILE_SOURCE
+#endif
+#include <fcntl.h>
+#include <sys/types.h>
+#include <dirent.h>
+""")
+
+
+def walk(basefd, path):
+ print '{', path
+ dirfd = ffi.C.openat(basefd, path, 0)
+ if dirfd < 0:
+ # error in openat()
+ return
+ dir = ffi.C.fdopendir(dirfd)
+ dirent = ffi.new("struct dirent")
+ result = ffi.new("struct dirent *")
+ while True:
+ if ffi.C.readdir_r(dir, dirent, result):
+ # error in readdir_r()
+ break
+ if result[0] is None:
+ break
+ name = str(dirent.d_name)
+ print '%3d %s' % (dirent.d_type, name)
+ if dirent.d_type == 4 and name != '.' and name != '..':
+ walk(dirfd, name)
+ ffi.C.closedir(dir)
+ print '}'
+
+
+walk(-1, "/tmp")