aacccceessss() -- System Call (libc)

Check if a file can be accessed in a given mode
#iinncclluuddee <uunniissttdd.hh>
iinntt aacccceessss(_f_i_l_e_n_a_m_e, _m_o_d_e) cchhaarr *_f_i_l_e_n_a_m_e; iinntt _m_o_d_e;

aacccceessss() checks whether a file or directory can be accessed in the mode you
wish.  _f_i_l_e_n_a_m_e is the full path  name of the file or directory you wish to
check.  _m_o_d_e is the mode in which you wish to access _f_i_l_e_n_a_m_e, as follows:

    FF_OOKK      File exists
    RR_OOKK      Read a file
    WW_OOKK      Write into a file
    XX_OOKK      Execute a file

The  header file  uunniissttdd.hh  defines these  values, which  may be  logically
combined to produce the _m_o_d_e argument.

If _m_o_d_e  is FF_OOKK, aacccceessss() tests only whether  _f_i_l_e_n_a_m_e exists, and whether
you have permission to search all directories that lead to it.

aacccceessss() returns  zero if _f_i_l_e_n_a_m_e  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.

aacccceessss() uses the _r_e_a_l user id and _r_e_a_l group id (rather than the _e_f_f_e_c_t_i_v_e
user id and _e_f_f_e_c_t_i_v_e group id), so set user id programs can use it.

_E_x_a_m_p_l_e
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);
}

_S_e_e _A_l_s_o
lliibbcc, ppaatthh(), uunniissttdd.hh
POSIX Standard, section 5.6.3

_N_o_t_e_s
When   the   superuser   rroooott   executes   aacccceessss(),  it   always   returns
readable/writable/executable  for  any  file  that  exists,  regardless  of
permissions.

Note that aacccceessss() used to be declared in header file <aacccceessss.hh>. It is now
prototyped in  header file <uunniissttdd.hh>,  to comply with  the POSIX standard.
<aacccceessss.hh> is  obsolete and has  been dropped from  COHERENT beginning with
release 4.2.
