access -- Determine Access Mode

Format

#include <io.h>
int access(char *pathname, int mode);

Language Level: POSIX, XPG4, Extension
access determines whether the specified file exists and whether you can get access to it in the given mode. Possible values for the mode and their meaning in the access call are:

Value Meaning
06 Check for permission to read from and write to the file.
04 Check for permission to read from the file.
02 Check for permission to write to the file.
00 Check only for the existence of the file.

Note: In earlier releases of the C/C++ run-time library, access began with an underscore _access. Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _access to access for you.

Return Value
access returns 0 if you can get access to the file in the specified mode. A return value of -1 shows that the file does not exist or is inaccessible in the given mode, and the system sets errno to one of the following values:

Value Meaning
EACCESS Access is denied; the permission setting of the file does not allow you to get access to the file in the specified mode.
ENOENT The system cannot find the file or the path that you specified, or the file name was incorrect.
EINVAL The mode specified was not valid.
EOS2ERR The call to the operating system was not successful.

Example
This example checks for the existence of the file sample.dat. If the file does not exist, it is created.

#include <io.h>
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
   if (-1 == access("sample.dat", 00)) {
      printf("File sample.dat does not exist.\n");
      printf("Creating sample.dat.\n");
      system("echo Sample Program > sample.dat");
      if (0 == access("sample.dat", 00))
         printf("File sample.dat created.\n");
   }
   else
      printf("File sample.dat exists.\n");
   return 0;
   /************************************************
      The output should be:
      File sample.dat does not exist.
      Creating sample.dat.
      File sample.dat created.
   ************************************************/
}


chmod -- Change File Permission Setting
_sopen -- Open Shared File
umask -- Sets File Mask of Current Process
<io.h>