;================================================================================= ; sys_getdents demonstration. ; ; Reads and prints a number of directories located one level up from the current ; path. ; ; The LSCR Project. ;================================================================================= format ELF include '../macros.inc' include '../../include/symbols.inc' include '../../include/structs.inc' extrn error_chk section '.text' executable public _start _start: mov eax, SYS_OPEN ; open a direcory located one level up mov ebx, filename_s xor ecx, ecx mov edx, S_IRWXU int 0x80 ccall error_chk, <"SYS_OPEN has failed: ">, exit mov ebx, eax ; read available directories mov eax, SYS_GETDENTS mov ecx, _dirent mov edx, sizeof.dirent ; try to read at least one directory entry int 0x80 ccall error_chk, <"SYS_GETDENTS has failed: ">, exit test eax, eax je exit mov [bytes_read], eax mov esi, _dirent ; parse through dir enries and print their names print_dir: lea edi, [esi+dirent.d_name] xor edx, edx xor eax, eax @@: inc edx scasb jne @b mov [edi-1], byte 0xa mov eax, SYS_WRITE mov ebx, STDOUT lea ecx, [esi+dirent.d_name] int 0x80 add si, [esi+dirent.d_reclen] ; get offset of the next entry mov eax, esi sub eax, _dirent cmp eax, [bytes_read] ; make sure we are in bounds jb print_dir exit: mov eax, SYS_EXIT xor ebx, ebx int 0x80 section '.data' writeable filename_s db "../",0 bytes_read rd 1 _dirent dirent