check to see if a file or directory can be accessed
#include <unistd.h> int access( const char *path, int amode );
The access() function checks 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), access() uses the real user ID and real group ID in place of the effective user and group IDs.
The value of amode is either the existence test, F_OK, or a bitwise ORing of the following access permissions to be checked, as defined in the header <unistd.h>:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main( int argc, char **argv ) { if( argc!= 2 ) { fprintf( stderr, "use: readable <filename>\n" ); exit( 1 ); } if( !access( argv[1], R_OK ) ) { printf( "ok to read %s\n", argv[1] ); exit( 0 ); } else { perror( argv[1] ); exit( 1 ); } return (EXIT_SUCCESS); }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes, but modifies errno |
Thread | Yes |
chmod(), errno, fstat(), open(), stat()