.TH access() "" "" "System Call (libc)"
.PC "Check if a file can be accessed in a given mode"
\fB#include <unistd.h>\fR
\fBint access(\fIfilename, mode\^\fB) char *\fIfilename\^\fB; int \fImode\^\fB;\fR
.PP
.II uninstd.h
.B access()
checks whether a file or directory
can be accessed in the mode you wish.
.I filename
is the full path name of the file or directory you wish to check.
.I mode
is the mode in which you wish to access
.IR filename ,
as follows:
.DS
.ta 0.4i 1.4i
	\fBF_OK\fR	File exists
	\fBR_OK\fR	Read a file
	\fBW_OK\fR	Write into a file
	\fBX_OK\fR	Execute a file
.DE
.PP
The header file
.B unistd.h
defines these values, which
may be logically combined to produce the
.I mode
argument.
.PP
If
.I mode
is
.BR F_OK ,
.B access()
tests only whether
.I filename
exists, and whether you have permission to search all directories that
lead to it.
.PP
.B access()
returns zero if
.I filename
can be accessed in the requested mode, and a nonzero value if it cannot.
Note that the return value is the opposite of the intuitive value, i.e.,
zero means success rather than failure.
.PP
.B access()
uses the
.I real
user id and
.I real
group id (rather than the
.I effective
user id and
.I effective
group id),
so set user id programs can use it.
.SH Example
The following example checks if a file can be accessed in a
particular manner.
.DM
#include <unistd.h>
#include <stdio.h>
.DE
.DM
main(argc, argv)
int argc; char *argv[];
{
	int mode;
	extern int access();
.DE
.DM
	if (argc != 3) {
		fprintf(stderr, "usage: acc dir_name/file_name mode\en");
		exit(EXIT_FAILURE);
	}
.DE
.DM
	switch (*argv[2]) {
	case 'x':
		mode = X_OK;
		break;
.DE
.DM
	case 'w':
		mode = W_OK;
		break;
.DE
.DM
	case 'r':
		mode = R_OK;
		break;
.DE
.DM
	case 'f':
		mode = F_OK;
		break;
.DE
.DM
	default:
		fprintf(stderr, "Bad mode. Modes: f, x, r, w\en");
		exit(EXIT_FAILURE);
		break;
	}
.DE
.DM
	if (access(argv[1], mode))
		printf("file %s cannot be found in mode %d\en", argv[1], mode);
	else
		printf("file %s is accessible in mode %d\en", argv[1], mode);
	exit(EXIT_SUCCESS);
}
.DE
.SH "See Also"
.Xr libc, libc
.Xr path(), path.f
.Xr unistd.h unistd.h
.br
\*(PX Standard, \(sc5.6.3
.SH Notes
When the superuser
.B root
executes
.BR access() ,
it always returns readable/writable/executable
for any file that exists, regardless of permissions.
.PP
Note that
.B access()
used to be declared in header file
.BR <access.h> .
It is now prototyped in header file
.BR <unistd.h> ,
to comply with the POSIX standard.
.B <access.h>
is obsolete and has been dropped from \*(CO beginning with release 4.2.
