_access

Description

Determines file-access permission.

#include <io.h> Required only for function declarations  
#include <errno.h> Required for definition of errno constants  

int _access( char *pathname, int mode );

pathname File or directory path name  
mode Permission setting  

Remarks

With files, the _access function determines whether the specified file exists and can be accessed in mode. The possible mode values and their meanings in the _access call are as follows:

Value Meaning

00 Check for existence only
02 Check for write permission
04 Check for read permission
06 Check for read and write permission

With directories, _access determines only whether the specified directory exists; in DOS, all directories have read and write access.

Return Value

The _access function returns the value 0 if the file has the given mode. A return value of –1 indicates that the named file does not exist or is not accessible in the given mode, and errno is set to one of the following values:

Value Meaning

EACCES Access denied: the file's permission setting does not allow the specified access.
ENOENT File or path name not found.

Compatibility

Standards:UNIX

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

Use _access for compatibility with ANSI naming conventions of non-ANSI functions. Use access and link with OLDNAMES.LIB for UNIX compatibility.

See Also

_chmod, _fstat, _open, _stat

Example

/* ACCESS.C: This example uses _access to check the file named "data"

* to see if it exists and if writing is allowed.

*/

#include <io.h>

#include <stdio.h>

#include <stdlib.h>

void main( void )

{

/* Check for existence */

if( (_access( "access.c", 0 )) != -1 )

{

printf( "File exists\n" );

/* Check for write permission */

if( (_access( "access.c", 2 )) != -1 )

printf( "File has write permission\n" );

}

}

Output

File exists

File has write permission