which C function is best to check for directory EXISTANCE?

Polytropon freebsd at edvax.de
Wed Dec 19 00:46:37 UTC 2012


On Tue, 18 Dec 2012 16:26:10 -0800, Gary Kline wrote:
> 
> 	which C function is best to check for the existence of a 
> 	*directory*?  say that I am want to make postitve that "/tmp/foo/"
> 	exists.

That can be done with the opendir() function; its manpage
lists other interesting "follow-up functions" to work with.



> 	ALso: which will make sure that the directory AND ffile 
> 	"tmp/foo/filename12345" exists and that I have read access to it?

You can use readdir() and parse its output. If you already know
the file name, use a fopen() call for the mode you're trying to
open it with (e. g. "r" or "w") to see if the permissions are
set accordingly. Also ferror() can be used to test for specific
errors that may occur during file operations.



> 	in other words, can playing around with access() and stat() be
> 	best?

That also sounds possible. In case you're going to continue to
operate with the file in question, certain functions sound better.
But for the "pure testing", access() and stat() provide sufficient
functionality.


> is there any new dirstat("PATh") that would work?

$ man dirstat
No manual entry for dirstat

:-)



> 	just want to see which way is best?

Depends. :-)




-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list