Check to see if a file or directory can be accessed
#include <unistd.h> int access( const char * path, int amode );
or a bitwise ORing of the following access permissions to be checked, as defined in the header <unistd.h>:
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The access() function checks to see if the file or directory specified by path exists and if it can be accessed with the file access permissions given by amode. However, unlike other functions (open() for example), it uses the real user ID and real group ID in place of the effective user and group IDs.
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main( int argc, char **argv ) { if( argc!= 2 ) { fprintf( stderr, "use: readable <filename>\n" ); return EXIT_FAILURE; } if( !access( argv[1], R_OK ) ) { printf( "ok to read %s\n", argv[1] ); return EXIT_SUCCESS; } else { perror( argv[1] ); return EXIT_FAILURE; } }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
chmod(), eaccess, errno, fstat(), open(), stat()