[mercury-users] io.m, directory listing

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Oct 6 15:44:09 AEST 1999


On 05-Oct-1999, Tomas By <tomas at research.nj.nec.com> wrote:
> I'm trying to add a `dir' function to io.m but I'm getting strange
> results (ie lots of empty strings among the correct file names, file
> names containing non-printable codes, file names that doesn't actually
> exist but look like truncated versions of existing ones).
...
>     while (ep = readdir(dp),ep)
>     {
>       Files = c_my_list_cons((String)ep->d_name,Files);
>     }

>From the man page for readdir():

 |   The pointer returned by the readdir() function points to data which may be
 |   overwritten by another call to readdir() on the same directory stream.

You need to make a copy of the string ep->d_name, otherwise
it will get overwritten after each call.

You can do that using the copy/2 predicate in module `builtin',
which you can export to C in the same way that you did with c_my_list_cons.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list