1

I looked everywhere for any question like this, to no avail. I am trying to get acquainted with x86 ASM, using NASM in Ubuntu. I want to write a program targeting only files with a .txt extension, so I want to iterate through the current directory, save these files to an array (or just push them on the stack), and iterate through them, modifying them using sys_open and the file descriptors.

Now I could not figure out for the life of me how to find the files, so I thought maybe there is a way to just call /bin/find and just use the results, but hit another wall, as I am pretty certain you cannot access/use the things that are returned through an interrupt/syscall.


%include    'functions.asm'



SECTION .data

cmd     db  '/bin/find', 0h

arg1        db  '.', 0h

arg2        db  '-type', 0h

arg3        db  'f', 0h

arg4        db  '-name', 0h

arg5        db  '*.txt', 0h

args        dd  cmd

        dd  arg1

        dd  arg2

        dd  arg3

        dd  arg4

        dd  arg5

        dd  0h

environment dd  0h



SECTION .text

global _start



_start:



    mov edx, environment

    mov ecx, args

    mov ebx, cmd

    mov eax, 11

    int 80h



    call    quit

This is how I called /bin/find to basically list all .txt files in the current directory.

Can anyone help me out in finding a way to do this? Any reading material that is recommended? Again, the goal is an 'array' of file names or file descriptors so that I can iterate through those specific files.

CrazyPhil
  • 73
  • 6
  • 2
    Are you sure you want to fork/exec `find`'s and pipe its output back into your program? The normal way is to open the directory yourself and call `getdents` to get a buffer of filename / inode structs (https://man7.org/linux/man-pages/man2/getdents.2.html). (Or use the `readdir` POSIX library function, which under the hood uses Linux `getdents`.) Use `strace ls` to see the system calls it makes. I'd recommend doing this in C first, if you aren't already familiar with POSIX / Linux system calls, since that's a separate thing to learn from doing it in asm. – Peter Cordes Mar 20 '22 at 22:24
  • 2
    Near duplicate of [Recursively list directory content, plus check if file is directory](https://stackoverflow.com/q/8194466) but that's using libc readdir instead of a raw getdents system call. Still recommend you take a look at how it's going about it. – Peter Cordes Mar 20 '22 at 22:36

0 Answers0