Joseph Martin wrote:
> I am a little bit of a programming newbie. That is I know the
> syntax, but not all the library routines available. Could anyone tell me
> how to check if a file exists? Do I open the file and see if it returns
> valid or NULL or is there a function that will check for me.
It depends upon whether you wish to check for whether the file:
1. exists
2. is readable/writeable by the current process
3. is readable/writeable by the current (real) uid/gid
4. is readable/writeable by a specific user
5. is actually a file (rather than a device, FIFO, ...)
2 is the simplest; you just open() it with the appropriate flags.
However, if it's a device or FIFO, doing so may have side-effects.
3 can be done using the access() function.
For the general case, you can use stat() or lstat(), which will return
all of the details, i.e. the type of the file, the owner, the group
the permissions etc.
Note that there are inherent race conditions with stat(), lstat() and
access(), i.e. by the time the call returns, the information may have
changed. This isn't a problem in most circumstances, but it isn't
adequate if there are any security issues involved. In that case, you
need to open() the file and then use fstat() on the descriptor.
--
Glynn Clements <[EMAIL PROTECTED]>