COHERENT manpages

This page displays the COHERENT manpage for access() [Check if a file can be accessed in a given mode].

List of available manpages
Index


access() -- System Call (libc)

Check if a file can be accessed in a given mode
#include <unistd.h>
int access(filename, mode) char *filename; int mode;

access() checks whether a file or directory can be accessed in the mode you
wish.  filename is the full path  name of the file or directory you wish to
check.  mode is the mode in which you wish to access filename, as follows:

    F_OK      File exists
    R_OK      Read a file
    W_OK      Write into a file
    X_OK      Execute a file

The  header file  unistd.h  defines these  values, which  may be  logically
combined to produce the mode argument.

If mode  is F_OK, access() tests only whether  filename exists, and whether
you have permission to search all directories that lead to it.

access() returns  zero if 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.

access() uses the real user id and real group id (rather than the effective
user id and effective group id), so set user id programs can use it.

Example

The following  example checks  if a  file can be  accessed in  a particular
manner.

#include <unistd.h>
#include <stdio.h>

main(argc, argv)
int argc; char *argv[];
{
    int mode;
    extern int access();

    if (argc != 3) {
        fprintf(stderr, "usage: acc dir_name/file_name mode\n");
        exit(EXIT_FAILURE);
    }

    switch (*argv[2]) {
    case 'x':
        mode = X_OK;
        break;

    case 'w':
        mode = W_OK;
        break;

    case 'r':
        mode = R_OK;
        break;

    case 'f':
        mode = F_OK;
        break;

    default:
        fprintf(stderr, "Bad mode. Modes: f, x, r, w\n");
        exit(EXIT_FAILURE);
        break;
    }

    if (access(argv[1], mode))
        printf("file %s cannot be found in mode %d\n", argv[1], mode);
    else
        printf("file %s is accessible in mode %d\n", argv[1], mode);
    exit(EXIT_SUCCESS);
}

See Also

libc,
path(),
unistd.h
POSIX Standard, §5.6.3

Notes

When   the   superuser   root   executes   access(),  it   always   returns
readable/writable/executable  for  any  file  that  exists,  regardless  of
permissions.

Note that access() used to  be declared in header file <access.h>. It
is now prototyped in header file <unistd.h>, to comply with the POSIX
standard.  <access.h> is  obsolete and has been dropped from COHERENT
beginning with release 4.2.