Testing Access to a File

Sometimes it is necessary to test the access of a file system object prior to its actual use. For example, if your application must provide the pathname of an executable to another application, you might want to make sure that you possess execute rights on that file. Testing access ahead of time may be simpler for corrective action. This is accomplished with the UNIX function access(2):

#include <unistd.h>

int access(const char *path, int mode);

mode:

F_OK, R_OK, W_OK and/or X_OK

The pathname of the object to be tested for access is provided in the first argument. The mode argument contains the bit-wise OR of the following values:

File exists F_OK
Read access R_OK
Write access W_OK
Execute access X_OK

The real user ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.