Scanning directories

classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Scanning directories

Gavin Wraith-2
Is it possible to extend the os library with
a directory-scanning function, like Perl's
'readdir', and still keep within ANSI C?
Of course we can each implement something
for our own platform but it would be nice
to be able to do this in a more portable way.

I have a function 'dirlist' that takes
a string as an argument. It returns nil
followed by an error message if the argument
is not the pathname of a directory for some
filing system (that supports directories) or if the 
directory is not readable. Otherwise it returns
a table whose indices are the leafnames of
the files within and whose values are 
tables of attributes (filetype, size, permissions,
date, etc). I find I use it quite a lot, and it
is something I would like to see standardized.

-- 
Gavin Wraith ([hidden email])
Home page: http://www.wraith.u-net.com/


Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Daniel Silverstone
On Wed, 2003-10-29 at 12:02, Gavin Wraith wrote:
> Is it possible to extend the os library with
> a directory-scanning function, like Perl's
> 'readdir', and still keep within ANSI C?
> Of course we can each implement something
> for our own platform but it would be nice
> to be able to do this in a more portable way.

There is no directory listing feature in ANSI C as far as I know. You
could popen a call to ls, but that's about all I can suggest.

It'd be more useful to write a library which loadlib can call, to add
these features in. Perhaps similar to the POSIX library which used to
exist for Lua 4.0

D.

-- 
Daniel Silverstone                       http://www.digital-scurf.org/
Hostmaster, Webmaster, and Chief Code Wibbler: Digital-Scurf Unlimited
GPG Public key available from keyring.debian.org       KeyId: 20687895



Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

David Jones-2
In reply to this post by Gavin Wraith-2
In message <Marcel-1.53-1029120224-d07ADmg@...>, Gavin Wraith writ
es:
> Is it possible to extend the os library with
> a directory-scanning function, like Perl's
> 'readdir', and still keep within ANSI C?

No.  You mean ISO C btw.  It's a bit of a shame isn't it?

Cheers,
 drj

Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Steve Elkins-2
In reply to this post by Daniel Silverstone
On Wed, 29 Oct 2003 12:09:00 +0000
Daniel Silverstone <[hidden email]> wrote:

> It'd be more useful to write a library which loadlib can call, to add
> these features in. Perhaps similar to the POSIX library which used to
> exist for Lua 4.0

I'm just making sure, you do know that 

  http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/

has one for 5.0?  It doesn't do all of posix, but it's very useful.


Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Luiz Henrique de Figueiredo
In reply to this post by Gavin Wraith-2
>There is no directory listing feature in ANSI C as far as I know. You
>could popen a call to ls, but that's about all I can suggest.

In the good old days you could open "." and read its contents, but this now
gives an error (at least in Linux). :-(

% cat .
cat: .: Is a directory

>It'd be more useful to write a library which loadlib can call, to add
>these features in. Perhaps similar to the POSIX library which used to
>exist for Lua 4.0

There *is* a posix library for Lua 5.0 at
	http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/

But perhaps it is easy to write a small portable library for directory
processing. It can't be hard to write a readdir emulation for other platforms.
It's probably already available somewhere in the web and so it'll be simply a
matter of wrapping it for Lua (using the relevant POSIX binding).

If someone knows of a multiplatform readdir emulation, please let me know.
--lhf

Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Daniel Silverstone
In reply to this post by Steve Elkins-2
On Wed, 2003-10-29 at 12:24, Steve Elkins wrote:
> I'm just making sure, you do know that 
>   http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/
> has one for 5.0?  It doesn't do all of posix, but it's very useful.

I hadn't spotted that. Perhaps I should package it for Debian

-- 
Daniel Silverstone                       http://www.digital-scurf.org/
Hostmaster, Webmaster, and Chief Code Wibbler: Digital-Scurf Unlimited
GPG Public key available from keyring.debian.org       KeyId: 20687895



Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Martin Spernau
In reply to this post by Luiz Henrique de Figueiredo
Luiz Henrique de Figueiredo wrote:

But perhaps it is easy to write a small portable library for directory
processing. It can't be hard to write a readdir emulation for other platforms.
It's probably already available somewhere in the web and so it'll be simply a
matter of wrapping it for Lua (using the relevant POSIX binding).

If someone knows of a multiplatform readdir emulation, please let me know.
--lhf
Hi!
as we noted some time ago, the LuaCheia code has had this functionality. The code is still there, but currently not used (as it is pending to be made into a module)
http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/cheiafs.c?rev=1.17&view=auto
The Win32 'emulation' is in these files:
http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/dirent.c?rev=1.2&view=auto
http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/dirent.h?rev=1.2&view=auto

This works ok on Linux and Win32, I'm not sure if OSX was tested...

The code does need some work to be really usefull, but maybe some finds it usefull already.

If there is interest and maybe some additional platforms contributed, I'd love to do a proper Lua-module from it.

cheers, Martin



Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Philipp Janda
Martin Spernau wrote:
Hi!

Hi!

as we noted some time ago, the LuaCheia code has had this functionality. The code is still there, but currently not used (as it is pending to be made into a module) http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/cheiafs.c?rev=1.17&view=auto

I spotted a minor bug in the code above:

#define LC_ISPATHSEP(C) (C=="/" ? 1 : 0 )
                         ^^^^^^
should probably be C=='/' or strcmp( C, "/" )!=0

And the same for
#define LC_ISPATHSEP(C) (C!="\\" ? (C=="/" ? 1 : 0 ) : 1)
                         ^^^^^^^    ^^^^^^
#define LC_ISPATHSEP(C) (C==":" ? 1 : 0 )
                         ^^^^^^


The Win32 'emulation' is in these files:
http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/dirent.c?rev=1.2&view=auto http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/dirent.h?rev=1.2&view=auto

This works ok on Linux and Win32, I'm not sure if OSX was tested...

The code does need some work to be really usefull, but maybe some finds it usefull already.

If there is interest and maybe some additional platforms contributed, I'd love to do a proper Lua-module from it.

cheers, Martin


Philipp





Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

David Jones-2
In message <bnoobt$nhg$1@...>, Philipp Janda writes:
> 
> > as we noted some time ago, the LuaCheia code has had this functionality. 
> > The code is still there, but currently not used (as it is pending to be 
> > made into a module)
> > http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/cheiaf
>>s.c?rev=1.17&view=auto 
> 
> I spotted a minor bug in the code above:
> 
> #define LC_ISPATHSEP(C) (C=="/" ? 1 : 0 )
>                           ^^^^^^
> should probably be C=='/' or strcmp( C, "/" )!=0
> 
> And the same for
> #define LC_ISPATHSEP(C) (C!="\\" ? (C=="/" ? 1 : 0 ) : 1)
>                           ^^^^^^^    ^^^^^^
> #define LC_ISPATHSEP(C) (C==":" ? 1 : 0 )
>                           ^^^^^^

I realise this is a lua list not a C list but aren't the above (aside
from the bug you pointed out) better written as:

#define LC_ISPATHSEP(C) ((C)=='/')
#define LC_ISPATHSEP(C) ((C)=='\\'||(C)=='/')
#define LC_ISPATHSEP(C) ((C)==':')

The middle one has a multiple evaluation issue which I won't bother
addressing.

Cheers,
 drj

Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Martin Spernau
Thanks very much Folks, I'll try and put those fixes in next time I update the code!
-Martin
David Jones wrote:

In message <bnoobt$nhg$1@...>, Philipp Janda writes:
as we noted some time ago, the LuaCheia code has had this functionality. The code is still there, but currently not used (as it is pending to be made into a module)
http://cvs.sourceforge.net/viewcvs.py/luacheia/luacheia/src/libcheia/cheiaf
s.c?rev=1.17&view=auto
I spotted a minor bug in the code above:

#define LC_ISPATHSEP(C) (C=="/" ? 1 : 0 )
                         ^^^^^^
should probably be C=='/' or strcmp( C, "/" )!=0

And the same for
#define LC_ISPATHSEP(C) (C!="\\" ? (C=="/" ? 1 : 0 ) : 1)
                         ^^^^^^^    ^^^^^^
#define LC_ISPATHSEP(C) (C==":" ? 1 : 0 )
                         ^^^^^^

I realise this is a lua list not a C list but aren't the above (aside
from the bug you pointed out) better written as:

#define LC_ISPATHSEP(C) ((C)=='/')
#define LC_ISPATHSEP(C) ((C)=='\\'||(C)=='/')
#define LC_ISPATHSEP(C) ((C)==':')

The middle one has a multiple evaluation issue which I won't bother
addressing.

Cheers,
drj





Reply | Threaded
Open this post in threaded view
|

Re: Scanning directories

Luiz Henrique de Figueiredo
In reply to this post by Gavin Wraith-2
I've written a simple library for listing directories:
	http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/ldir.tar.gz	

It is based on a very simple interface that should be easy to implement for
several platforms. I include an implementation for POSIX systems and I hope
to get contributions for all other platforms that matter. The README contains
some suggestions of starting points.

Here is the abstraction that I use; it should be easier to port than than to
provide a full readdir emulation:

 typedef ... *dir_handle;
 dir_handle dir_open(const char *path);
 int dir_close(dir_handle d);
 const char *dir_entry(dir_handle d);
 const char *dir_error(void);

If you're only interested in POSIX systems, then you don't need this library,
because my POSIX library already has this and more.

All suggestions welcome. Enjoy.
--lhf