I got this question in a Cisco interview: write a function to find the size of a directory?
Following is the pseudocode for such a function, that follows a recursive approach. Please tell me if there can be any other approach also.
int directorySize(DirectoryHandle dh)
{
int size=0;
if (!dh)
{
DirectoryHandle dh1 = directoryOpen("Directory_path");
}
else
{
dh1 = dh;
}
while (dh1)
{
if (TRUE=IsDirectory(dh1))
{
size += directorysize(dh1);
}
else if (TRUE == IsFile(dh1))
{
FileHandle fh = dh1;
while (EOF != fh)
{
size++;
}
}
}
}
Canonical example of using nftw:
Note that as interview questions go, they will probably want to see you thinking about
The following code does address most of these issues in a pragmatic fashion:
.
This was posted as Fastest ways to get a directory Size and Size on disk before. Typical output: