Joshua Isom wrote:
I'm trying to use libc to be able to do a directory listing, and and if
I used the DATATYPE_CSTR I get a segfault, and if I use DATATYPE_STRING,
It's neither nor. A DATATYPE_CSTR would be a pointer to a cstring. But
when you look at the definition of struct dirent, you see an inlined
array of chars with size 256. After fixing this and specifying d_fileno
is DATATYPE_INT64, the program works:
leo
.sub _main @MAIN
.local pmc libc
.local pmc opendir
.local pmc readdir
.local pmc closedir
null libc
dlfunc opendir, libc, 'opendir', 'pt'
dlfunc readdir, libc, 'readdir', 'pp'
dlfunc closedir, libc, 'closedir', 'ip'
store_global 'libc::opendir', opendir
store_global 'libc::readdir', readdir
store_global 'libc::closedir', closedir
.local pmc curdir
curdir = libc::opendir(".")
.local OrderedHash entry
entry = libc::readdir(curdir)
entry = libc::readdir(curdir)
entry = libc::readdir(curdir)
.include "datatypes.pasm"
new $P2, .OrderedHash
set $P2["d_fileno"], .DATATYPE_INT64
push $P2, 0
push $P2, 0
set $P2["d_reclen"], .DATATYPE_SHORT
push $P2, 0
push $P2, 0
set $P2["d_type"], .DATATYPE_CHAR
push $P2, 0
push $P2, 0
set $P2["d_name"], .DATATYPE_CHAR
push $P2, 256
push $P2, 0 # 11
assign entry, $P2
$I1 = 0
loop:
$I0 = entry["d_name";$I1]
unless $I0 goto ex
chr $S0, $I0
print $S0
inc $I1
goto loop
ex:
print "\n"
.end