access() COHERENT System Call access()
Check if a file can be accessed in a given mode
#include <access.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:
AREAD Read a file
ALIST List the contents of a directory
AWRITE Write into a file
ADEL Delete files from a directory
AEXEC Execute a file
ASRCH Search a directory
AAPPND Append to a file
ACREAT 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