

access()               COHERENT System Call              access()




Check if a file can be accessed in a given mode

#iinncclluuddee <aacccceessss.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;

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:


         AARREEAADD     Read a file
         AALLIISSTT     List the contents of a directory

         AAWWRRIITTEE    Write into a file
         AADDEELL      Delete files from a directory

         AAEEXXEECC     Execute a file
         AASSRRCCHH     Search a directory

         AAAAPPPPNNDD    Append to a file
         AACCRREEAATT    Create a file in a directory


The header file access.h defines these values, which may be logi-
cally combined to produce the mode argument.

If mode  is set to  zero, access tests only  whether filename ex-
ists, 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 number greater than zero if it cannot.

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 par-
ticular manner.


#include <access.h>
#include <stdio.h>



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


COHERENT Lexicon                                           Page 1




access()               COHERENT System Call              access()






    if (argc != 3) {
              fprintf(stderr, "Usage: access filename mode\n");
              exit(1);
    }



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



    case 'w':
              mode = AWRITE;
              break;



    case 'r':
              mode = AREAD;
              break;



    default:
              fprintf(stderr,
              "modes: x = execute, w = write, r = read\n");
              exit(1);
              break;
    }



    if (access(argv[1], mode)) {
              printf("file %s not found in mode %d\n", argv[1], mode);
              exit(0);
    } else
              printf("file %s accessible in mode %d\n",
               argv[1], mode);
    exit(0);
}


***** See Also *****

access.h, COHERENT system calls, path()






COHERENT Lexicon                                           Page 2


