Author Topic: Playing with getdents...  (Read 13785 times)

Offline pikestar

  • Jr. Member
  • *
  • Posts: 5
Playing with getdents...
« on: July 28, 2011, 11:18:21 PM »
Trying to learn assembly and I'm trying to play with files at the moment. I want to be able to see what's in a directory so I've used the getdents call to read the information. The problem I'm struggling with now is how to make use of what I've got. The entries don't seem to be fixed length and also only the file name is terminated by a 0. Also in the code below I'm reading to a 600 byte buffer but I know the directory I'm reading require more like 900. I had thought of putting it all on the stack instead of the reserved bytes but as I'm new to assembly I not sure if that would make any difference. I had imagined the eax would be set to -ve to show there was more to read but it's not! Does anybody have more experience with this system call??
Code: [Select]


section .text
global _start

_start:

;open the directory
;------------------
mov eax,5 ;system call for open
mov ebx,dir ;the file we want to open
mov ecx,0 ;read only
int 80h

;check file valid
;----------------

test eax,eax

jns read_dir

jmp exit_prog


read_dir:

mov dword[fd],eax ;move the file descripter for dir to the variable
mov eax,141 ;getdents sys_call
mov ebx,dword[fd] ;the file descripter for dir
mov ecx,dirent ;pointer to dirent struct
mov edx,600 ;buffer
int 80h


print_info:
;print what we read
;------------------

mov eax,4 ;write
mov ebx,1 ;stdio
mov ecx,dirent ;output
mov edx,600 ;length
int 80h

exit_prog:

;exit
;----

mov eax,1 ;sys call exit
int 80h


section .data

dir db '../../Downloads',0
mesg db 'reading dir',10
mesgLen equ $-mesg
errormsg db 'error',10
errorLen equ $-errormsg

section .bss

fd resd 1

dirent resb 600

« Last Edit: July 28, 2011, 11:19:58 PM by pikestar »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Playing with getdents...
« Reply #1 on: July 29, 2011, 12:03:31 AM »
"man 2 getdents" tells me "this is not the function you are interested in"... but I think it is. I think I've used it, but will have to "review"... I seem to recall that there's a maximum it will read, no matter how big your buffer is. I think it returns (in eax) the number of bytes read, regardless of whether there's more to read or not. When it returns zero, you're done.

I tried your code - had to change the name of the directory to open - and it seemed to "work". To "make sense" of it, you'll probably have to skip over inode, offset to next dirent, and length of this dirent, then calculate the length of the filename (or use puts() or printf() that expect a zero-terminated string... but you seem to like int 80h)...

It may be easier to use sys_readdir, although it's "obsolete".

I'll have to "review"... I'll get back to ya. If I forget, remind me!

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Playing with getdents...
« Reply #2 on: July 29, 2011, 04:21:43 PM »
A little "review" reminds me that I was having some "issues" with this code. I've got it partly sorted out (I guess), but I'm still not happy with it. I'm attaching what I've got. Paradoxically, the one named "readdir5" uses sys_getdents, the one named "ls0" uses sys_readdir. Go figure. They each have "good parts", I think. There might be one "good" file between 'em, but neither is quite there yet. I'm posting them anyway, 'cause I think there might be parts that could be helpful to you in your experiments, and I don't know when (if ever) I'll get to improving it/them...

Best,
Frank


Offline pikestar

  • Jr. Member
  • *
  • Posts: 5
Re: Playing with getdents...
« Reply #3 on: August 01, 2011, 03:51:11 PM »
A little "review" reminds me that I was having some "issues" with this code. I've got it partly sorted out (I guess), but I'm still not happy with it. I'm attaching what I've got. Paradoxically, the one named "readdir5" uses sys_getdents, the one named "ls0" uses sys_readdir. Go figure. They each have "good parts", I think. There might be one "good" file between 'em, but neither is quite there yet. I'm posting them anyway, 'cause I think there might be parts that could be helpful to you in your experiments, and I don't know when (if ever) I'll get to improving it/them...

Best,
Frank

Cheers I'll have a look through them and let you know how I get on

regard