Testing for an empty directory with readdir
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Testing for an empty directory with readdir




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Testing for an empty directory with readdir  
John


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 08:16 AM

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);
}







[ Post a follow-up to this message ]



    Re: Testing for an empty directory with readdir  
Pascal Bourguignon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 08:16 AM

"John" <iamlevi3@hotmail.com> writes:

> Howdy... Can anyone offer any suggestions on how to test whether a directo
ry
> 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...  somethin
g
> 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





[ Post a follow-up to this message ]



    Re: Testing for an empty directory with readdir  
Maurizio Loreti


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
06-29-04 08: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                         [url]http://www.pd.infn.it/~loreti/mlo.html[/ur
l]
Dept. of Physics, Univ. of Padova, Italy              ROT13: ybergv@cq.vasa.
vg





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:03 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register