trying to emulate linux command in x86_64 assembly
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
but as you can see i have to declare file name . and i don't wanna do that. now my assumption is to do something with getdents64 sys_call and list files in some buffer and then unlink them. but i don't actually know how to do that. any help would be appreciated. thank you
Please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.
The asterisk in rm * is interpreted by the shell. The shell performs a directory lookup, and you are right: It's probably getdents. You will have to open the current directory and use the file descriptor thus obtained to make the call to getdents. Then loop over the getdents results and unlink the names.
The getdents man page (see link above) contains a sample C program. I will leave it up to you to turn that into assembly code.
So i am trying to make a program that loops through directory, and deletes every file in it, so i am using getdents64 system call to do this. and since getdents returns number of bytes read from directory. i cannot loop through it. so i obviously need to point to the filename in a directory. but i really don't know how to do it,
this code will not work, when you run strace on it you will get this output, obiously i have put the output of a program running without a loop, you can guess what happens when i run it in a loop
global _start
section .data
dir : db "test",0
len: equ 2040 ;define buffer size
section .bss
buffer: resb len
section .text
_start:
;open folder
mov rax, 2 ;sys_open
mov rdi, dir ;folder to open
mov rsi, 0 ;read only
mov rdx, 0
syscall
cmp rax, 0 ;if there is no folder go to exit
jbe exit
list:
mov rdi, rax ;directory in rdi
mov rax, 217 ;sys_getdents64
mov rsi, buffer
mov rdx, len ;length of the
syscall
xchg r10, rax ;save buffer in r10 to loop through it
xor rax, rax ;zero out rax for the next system call
close:
mov rax, 3 ;sys_close
syscall
unlink:
cmp r10, 0 ;if done, exit the program
jbe exit
mov rax, 87 ;sys_unlink
mov rdi, r10 ;list of files
syscall
jmp unlink
exit:
mov rax, 60 ;sys_exit
mov rdi, 80
syscall
Please post your thread only once. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being merged with your previous thread of same subject.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.