1import sys 2from cffi import FFI 3 4if not sys.platform.startswith('linux'): 5 raise Exception("Linux-only demo") 6 7 8ffi = FFI() 9ffi.cdef(""" 10 11 typedef void DIR; 12 typedef long ino_t; 13 typedef long off_t; 14 15 struct dirent { 16 ino_t d_ino; /* inode number */ 17 off_t d_off; /* offset to the next dirent */ 18 unsigned short d_reclen; /* length of this record */ 19 unsigned char d_type; /* type of file; not supported 20 by all file system types */ 21 char d_name[256]; /* filename */ 22 }; 23 24 int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result); 25 int openat(int dirfd, const char *pathname, int flags); 26 DIR *fdopendir(int fd); 27 int closedir(DIR *dirp); 28 29""") 30ffi.set_source("_readdir", None) 31 32if __name__ == '__main__': 33 ffi.compile() 34