Access (): functions related to stat, open, chmod, chown, setuid, and setgid to determine whether a file has the permission to be accessed # includeunistd. h defines the function intaccess (constchar * pathname, intmode). The function indicates that access () checks whether an existing file can be read and written. Parameter mode
Access (): functions related to stat, open, chmod, chown, setuid, and setgid to determine whether a file has the permission to be accessed # includeunistd. h defines the function int access (const char * pathname, int mode). The function indicates that access () checks whether an existing file can be read/written. Parameter mode
Access (): determines whether a user has the permission to access a file.
RelatedFunction
Stat, open, chmod, chown, setuid, setgid
Header file
# Include
DefinitionFunction
Int access (const char * pathname, int mode );
FunctionDescription
Access () checks whether an existing file can be read/written. The mode parameter can be combined in several cases: R_ OK, W_ OK, X_ OK, and F_ OK. R_ OK, W_ OK, and X_ OK are used to check whether the file has read, write, and execution permissions. F_ OK is used to determine whether the file exists. Because access () is only used for permission verification and does not care about the file format or content, if a directory is represented as "writable ", indicates that new files can be created in the directory, rather than being processed as files. For example, you will find that all DOS Files have the "executable" permission, but execve () will fail to be executed.
Return Value
If all the permissions to be checked have passed the check, a value of 0 is returned, indicating that the operation is successful. If a permission is disabled,-1 is returned.
Error Code
The file specified by the EACCESS parameter pathname does not meet the required permissions.
The file to be tested by EROFS is stored in the read-only file system.
The pathname pointer of the EFAULT parameter exceeds the accessible memory space.
The mode Val parameter mode is incorrect.
The pathname parameter of ENAMETOOLONG is too long.
The pathname parameter of ENOTDIR is a directory.
Insufficient ENOMEM core memory
The pathname parameter of ELOOP has too many symbolic connections.
Eio I/O access error.
Additional instructions
Be especially careful when using access () for user authentication. For example, making an open () empty file after access () may cause system security problems.
Example
# Include
Int main ()
{
If (access ("/etc/passwd", R_ OK) = 0)
Printf ("/etc/passwd can be read \ n ");
}
Run
/Etc/passwd can be read