#include <unistd.h> int access(const char *path, int amode);Service Program Name: QP0LLIB1
The access() function determines whether a file can be accessed in a particular manner. When checking whether a job has appropriate permissions, access() looks at the real user ID (UID) and group ID (GID), not the effective IDs. Adopted authority is not used.
(Input) A pointer to the null-terminated path name for the file to be checked for accessibility.
This parameter is assumed to be represented in the CCSID (coded character set identifier) currently in effect for the job. If the CCSID of the job is 65535, this parameter is assumed to be represented in the default CCSID of the job.
const char *path is the name of the file whose accessibility you want to determine. If the named file is a symbolic link, access() resolves the symbolic link.
See QlgAccess-- Determine File Accessibility (using NLS-enabled path name) for a description and an example of supplying the path in any CCSID.
(Input) A bitwise representation of the access permissions to be checked.
The following symbols, which are defined in the <unistd.h> header file, can be used in amode:
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 the file, you cannot use OR with any of the other symbols. If any other bits are set in amode, access() returns the [EINVAL] error.
If the job has *ALLOBJ special authority, access() will
indicate success for R_OK, W_OK, or X_OK even if none of the permission bits
are set.
Authorization Required for
access()
Object Referred to | Authority Required | errno |
---|---|---|
Each directory in the path name preceding the object to be tested | *X | EACCES |
Object when R_OK is specified | *R | EACCES |
Object when W_OK is specified | *W | EACCES |
Object when X_OK is specified | *X | EACCES |
Object when R_OK | W_OK is specified | *RW | EACCES |
Object when R_OK | X_OK is specified | *RX | EACCES |
Object when W_OK | X_OK is specified | *WX | EACCES |
Object when R_OK | W_OK | X_OK is specified | *RWX | EACCES |
Object when F_OK is specified | None | None |
If access() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.
Error condition | Additional information |
---|---|
[EACCES] |
If you are accessing a remote file through the Network File System, update operations to file permissions at the server are not reflected at the client until updates to data that is stored locally by the Network File System take place. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.) Access to a remote file may also fail due to different mappings of user IDs (UID) or group IDs (GID) on the local and remote systems. |
[EAGAIN] | |
[EBADFID] | |
[EBADNAME] | |
[EBUSY] | |
[ECONVERT] | |
[EDAMAGE] | |
[EFAULT] | |
[EFILECVT] | |
[EINVAL] | |
[EIO] | |
[EINTR] | |
[ELOOP] | |
[ENAMETOOLONG] | |
[ENOENT] | |
[ENOSPC] | |
[ENOTAVAIL] | |
[ENOTDIR] | |
[ENOTSAFE] | |
[ENOTSUP] | |
[EROOBJ] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETXTBSY] | |
[EUNKNOWN] |
If interaction with a file server is required to access the object, errno could indicate one of the following errors:
Error condition | Additional information |
---|---|
[EADDRNOTAVAIL] | |
[ECONNABORTED] | |
[ECONNREFUSED] | |
[ECONNRESET] | |
[EHOSTDOWN] | |
[EHOSTUNREACH] | |
[ENETDOWN] | |
[ENETRESET] | |
[ENETUNREACH] | |
[ESTALE] |
If you are accessing a remote file through the Network File System, the file may have been deleted at the server. |
[ETIMEDOUT] | |
[EUNATCH] |
The following messages may be sent from this function:
Message ID | Error Message Text |
---|---|
CPE3418 E | Possible APAR condition or hardware failure. |
CPFA0D4 E | File system error occurred. Error number &1. |
CPF3CF2 E | Error(s) occurred during running of &1 API. |
CPF9872 E | Program or service program &1 in library &2 ended. Reason code &3. |
Local access to remote files through the Network File System may produce unexpected results due to conditions at the server. Once a file is open, subsequent requests to perform operations on the file can fail because file attributes are checked at the server on each request. If permissions on the file are made more restrictive at the server or the file is unlinked or made unavailable by the server for another client, your operation on an open file descriptor will fail when the local Network File System receives these updates. The local Network File System also impacts operations that retrieve file attributes. Recent changes at the server may not be available at your client yet, and old values may be returned from operations. (Several options on the Add Mounted File System (ADDMFS) command determine the time between refresh operations of local data.)
If the object exists on a volume formatted in Universal Disk Format (UDF), the authorization that is checked for the object and preceding directories in the path name follows the rules described in Authorization Required for access() . If the object exists on a volume formatted in some other media format, no authorization checks are made on the object or preceding directories. The volume authorization list is checked for the requested authority regardless of the volume media format.
See Code disclaimer information for information pertaining to code examples.
The following example determines how a file is accessed:
#include <stdio.h> #include <unistd.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:
The output from a user with read and execute access is:
You have read access to '/' You have search access to '/'
Top | UNIX-Type APIs | APIs by category |