|
Home > Archive > Unix Programming > June 2004 > Testing for an empty directory with readdir
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Testing for an empty directory with readdir
|
|
|
| Howdy... Can anyone offer any suggestions on how to test whether a directory
is empty in a opendir/readdir/closedir scheme? I thought that I could
increment a counter for every thing that readdir finds, and if it gets to 2
( . and .. ) and then gets null, then that directory is empty... something
like that, maybe, but i'm having trouble with the implementation. Any
thoughts?
Guidance is, as always, appreciated.
void
printAllFileInfo( void ){
struct dirent *dstat;
DIR *dirp;
int i;
for(i=0;i<dlistlen;i++){
dirp = opendir( dlist[i] );
printf("\n%s:\n",dlist[i]);
while( dstat=readdir(dirp) ){ /* something something here....? */
if( strcmp(dstat->d_name,".") == 0 ||
strcmp(dstat->d_name,"..") == 0 )
continue;
/* pass the path and the filename to printOneFileInfo */
printOneFileInfo( dlist[i],dstat->d_name );
}
}
closedir(dirp);
}
| |
| Pascal Bourguignon 2004-06-29, 3:16 am |
| "John" <iamlevi3@hotmail.com> writes:
> Howdy... Can anyone offer any suggestions on how to test whether a directory
> is empty in a opendir/readdir/closedir scheme? I thought that I could
> increment a counter for every thing that readdir finds, and if it gets to 2
> ( . and .. ) and then gets null, then that directory is empty... something
> like that, maybe, but i'm having trouble with the implementation. Any
> thoughts?
It depends on the file system type. Not all file systems include . and
... in directories.
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| Maurizio Loreti 2004-06-29, 3:16 am |
| "John" <iamlevi3@hotmail.com> writes:
> Howdy... Can anyone offer any suggestions on how to test whether a
> directory is empty in a opendir/readdir/closedir scheme?
That depends from how your filesystem is implemented. Under Linux
(pseudocode):
file_counter <- 0
loop over readdir {
if (dirent.d_ino is 0) {
/* Empty inode (removed file) */
} else if (dirent.d_name is . OR dirent.d_iname is ..) {
/* Pseudo-files (current and up directory) */
} else {
a real entry; increment file_counter
}
}
--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Dept. of Physics, Univ. of Padova, Italy ROT13: ybergv@cq.vasa.vg
|
|
|
|
|