master
 1/**
 2 * This file has no copyright assigned and is placed in the Public Domain.
 3 * This file is part of the mingw-w64 runtime package.
 4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 5 */
 6#include <windows.h>
 7#include <errno.h>
 8#include <io.h>
 9
10int __cdecl __mingw_access(const char *fname, int mode);
11
12int __cdecl __mingw_access(const char *fname, int mode)
13{
14  DWORD attr;
15
16  if (fname == NULL || (mode & ~(F_OK | X_OK | W_OK | R_OK)))
17  {
18    errno = EINVAL;
19    return -1;
20  }
21
22  attr = GetFileAttributesA(fname);
23  if (attr == INVALID_FILE_ATTRIBUTES)
24  {
25    switch (GetLastError())
26    {
27      case ERROR_FILE_NOT_FOUND:
28      case ERROR_PATH_NOT_FOUND:
29        errno = ENOENT;
30        break;
31      case ERROR_ACCESS_DENIED:
32        errno = EACCES;
33        break;
34      default:
35        errno = EINVAL;
36    }
37    return -1;
38  }
39
40  if (attr & FILE_ATTRIBUTE_DIRECTORY)
41  {
42    /* All directories have read & write access */
43    return 0;
44  }
45
46  if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
47  {
48    /* no write permission on file */
49    errno = EACCES;
50    return -1;
51  }
52  else
53    return 0;
54}