Check if a directory is empty using C on Linux
Is there a more efficient way to check for an empty directory, especially if it has 1000s of files if not empty
The way you wrote your code it doesn't matter how many files it has (you break
if n > 2). So your code is using a maximum of 5 calls. I don't think there's any way to (portably) make it faster.