gtpc2m0bC/C++ Language Support User's Guide

access-Determine Whether a File Can Be Accessed

This function determines how the requesting process can access a file. When checking to see if a process has appropriate permissions, the access function looks at the real user ID (UID) and real group ID (GID) of the process, not the effective UID and effective GID.

Format

#include <unistd.h>
int access(const char *pathname, int how);

pathname
The name of the file whose accessibility you want to test.

how
Indicates the access modes you want to test.

The following symbols are defined in the unistd.h header file for use in the how parameter:

F_OK
Tests whether the file exists.

R_OK
Tests whether the process can read the file.

W_OK
Tests whether the process can write the file.

X_OK
Tests whether the process can search the directory.

You can take the bitwise inclusive OR of any or all of the last three symbols to test several access modes at once. If you are using F_OK to test for the existence of a file, you cannot use OR with any of the other symbols.

Normal Return

The returned value of access is zero if the specified access is permitted.

Error Return

If unsuccessful, the access function returns -1 and sets sets errno to one of the following:

EACCES
The process does not have appropriate permissions to access the file in the specified way or does not have search permission on some component of the pathname prefix.

EINVAL
The value of how is incorrect.

ELOOP
A loop exists in the symbolic links. This error is issued if the number of symbolic links detected in the resolution is greater than POSIX_SYMLOOP (a value defined in the limits.h header file).

ENAMETOOLONG
pathname is longer than PATH_MAX characters, or some component of pathname is longer than NAME_MAX.

ENOENT
There is no file named pathname or the pathname parameter is an empty string.

ENOTDIR
Some component of the pathname prefix is not a directory.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Examples

The following example determines how a file is accessed.

#include <unistd.h>
#include <stdio.h>
 
main() {
  char path[]="/";
 
  if (access(path, F_OK) != 0)
    printf("'%s' does not exist!\n", path);
  else {
    if (access(path, R_OK) == 0)
      printf("You have read access to '%s'\n", path);
    if (access(path, W_OK) == 0)
      printf("You have write access to '%s'\n", path);
    if (access(path, X_OK) == 0)
      printf("You have search access to '%s'\n", path);
  }
}

Output

You have read access to '/'
You have search access to '/'

Related Information

chmod-Change the Mode of a File or Directory.

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.