| # A Linux-only demo, using set_source() instead of hard-coding the exact layouts |
| if not sys.platform.startswith('linux'): |
| raise Exception("Linux-only demo") |
| # run readdir2_build first, then make sure the shared object is on sys.path |
| from _readdir2_cffi import ffi, lib |
| dirfd = lib.openat(basefd, path, 0) |
| dir = lib.fdopendir(dirfd) |
| dirent = ffi.new("struct dirent *") |
| result = ffi.new("struct dirent **") |
| if lib.readdir_r(dir, dirent, result): |
| if result[0] == ffi.NULL: |
| name = ffi.string(dirent.d_name) |
| print '%3d %s' % (dirent.d_type, name) |
| if dirent.d_type == lib.DT_DIR and name != '.' and name != '..': |