access

Determines whether a file or directory exists and whether it can be accessed.

Library:LibC
Classification:POSIX
Service:File and Directory I/O

Syntax

  #include <unistd.h> 
   
  int access (
     const char  *path,   
     int          mode); 
  

Parameters

path

(IN) Specifies the pathname of the file or directory to check for accessibility.

mode

(IN) Specifies the access permission mode for the file with a bitwise-inclusive OR of the following flags:

Flag

Value

Description

R_OK

1

Tests for read permission.

W_OK

2

Tests for write permission.

X_OK

4

For a NetWare directory, always returns success. For a NetWare file, returns success if the file is readable.

F_OK

8

Tests for the existence of the file. You can also pass in 0 to test for existence.

Return Values

If successful, returns 0, indicating that the file or directory exists and can be accessed with the specified mode. Otherwise, returns -1 and sets errno to one of the following:

Decimal

Constant

Description

1

ENOENT

A component of path does not name an existing file or path is an empty string.

6

EACCES

Permission bits of the file mode do not permit the requested access, or search permission is denied on a component of the path prefix.

9

EINVAL

The value of the mode parameter is invalid.

65

ENAMETOOLONG

The length of the path argument exceeds {PATH_MAX} or a pathname component is longer than {NAME_MAX}.

67

ENOTDIR

A component of the path prefix is not a directory.

76

EROFS

Write access is requested for a file on a read-only file system.

See Also

chmod