“ran zhang” <rzhang@vamcointernational.com> wrote in message
news:9paelh$e9e$3@inn.qnx.com…
help says that every opendir has to have closedir in order to re-allocate
the memory that openDir uses…
For every opendir, there will be a closedir. Nothing says that you can’t
have more then one outstanding opendir().
I think where you might get confuse is the dirp variable. See because
the variable is on the stack everytime scan_dir is called a new dirp
variable is created. It is not “same” variable. This is why it is often
said that C can be use to write recursive code. Everytime you enter a
fonction,
local variable are created on the stack .
Do i Need to put the closedir inside both if statments in order to avoid
memory leak??
Mario Charest <> mcharest@clipzinformatic.com> > wrote in message
news:9paddm$cuh$> 1@inn.qnx.com> …
“ran zhang” <> rzhang@vamcointernational.com> > wrote in message
news:9pab04$aju$> 3@inn.qnx.com> …
The example doesn’t do it recursively, it stops at the 1st level…
alreays
return 4096 as the size for the sub-directory, any suggestion on
fixing
it?
Yes do it recursively > > That means let the fonction that scan the
directory
call itself when it detect a directory. When you reach the bottom of
the
directory
you return.
I could look like:
int scan_dir( const char dir_name )
{
DIR dirp;
struct dirent* direntp;
int size=0;
struct stat buf;
dirp = opendir( dir_name );
if( dirp == NULL ) {
perror( “can’t open dir” );
return 0;
} else {
while ( (direntp = readdir( dirp )) != NULL ) {
stat ( direntp->d_name, &buf );
// — get size of standard file
if ( S_ISREG( buf.st_mode) ) {
size += buf.st_size;
}
// — if it’s a directory recurse in it.
if ( S_ISDIR( buf.st_mode ) {
size += scan_dir( direntp->d_name );
}
}
}
closedir( dirp );
return size;
}
I haven’t try the code above, I just made it up.
It’s probably missing things like ignoring files “.” and “…”.
Mario Charest <> mcharest@clipzinformatic.com> > wrote in message
news:9pa6d7$57r$> 1@inn.qnx.com> …
“Operating System for Tech Supp” <> os@qnx.com> > wrote in message
news:9pa4g5$1g1$> 2@nntp.qnx.com> …
ran zhang <> rzhang@vamcointernational.com> > wrote:
can anyone supply me with some source code about how to write
‘du’?
i need to ge the size of each file in a directory, and sum them
up,
and
if there are sub-directory, i ned to do it recursively… it
seems
difficult, can someone help??
Look at opendir() and readdir(), I think the help has an example.
My goal is to get the size of a directory, so I can update my
RtProgress
during a ‘copy’ command…
Check out stat()
-Adam
\