Skip to content

Instantly share code, notes, and snippets.

@zwetan
Created March 2, 2014 14:46
Show Gist options
  • Save zwetan/9307610 to your computer and use it in GitHub Desktop.
Save zwetan/9307610 to your computer and use it in GitHub Desktop.
more advanced example to list directories with redtamarin
import C.errno.*;
import C.stdlib.*;
import C.dirent.*;
import C.fcntl.*;
import C.sys.stat.*;
trace( "----" );
/* note:
inspired by "Find And Open a File" example
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopendir.html#
in this path
$ tree -hla tmp
tmp
├── [ 0] .abc
├── [ 0] .def
├── [ 12] ghi
└── [ 48] jkl
*/
var statbuf:status;
var d:DIR;
var dfd:int;
var ffd:int;
var dp:dirent;
var dte:Date;
if( (d = fdopendir( dfd = open( "./tmp", O_RDONLY ) )) == null )
{
trace( "Cannot open './tmp' directory" );
trace( "Error# (" + int(errno) + ") " + new CError(errno) ); // Error# (9) EBADF: Bad file descriptor
exit(1);
}
while( (dp = readdir(d)) != null )
{
// ignore entries starting with dot
if( dp.d_name.charAt(0) == "." )
{
continue;
}
statbuf = new status();
ffd = stat( "./tmp/" + dp.d_name, statbuf );
dte = new Date();
dte.setTime( statbuf.st_mtime * 1000 );
trace( "entry: " + dp.d_name );
trace( " - size = " + statbuf.st_size + " bytes" );
trace( " - time = " + String( dte ) );
}
closedir( d ); // this implicitly closes dfd
trace( "----" );
trace( "bye bye program" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment