aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/mingw/misc/mingw-access.c
blob: 6f405ab99cca8dd0e154ff5148eaa83a485446a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
 * This file has no copyright assigned and is placed in the Public Domain.
 * This file is part of the mingw-w64 runtime package.
 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
 */
#include <windows.h>
#include <errno.h>
#include <io.h>

int __cdecl __mingw_access(const char *fname, int mode);

int __cdecl __mingw_access(const char *fname, int mode)
{
  DWORD attr;

  if (fname == NULL || (mode & ~(F_OK | X_OK | W_OK | R_OK)))
  {
    errno = EINVAL;
    return -1;
  }

  attr = GetFileAttributesA(fname);
  if (attr == INVALID_FILE_ATTRIBUTES)
  {
    switch (GetLastError())
    {
      case ERROR_FILE_NOT_FOUND:
      case ERROR_PATH_NOT_FOUND:
        errno = ENOENT;
        break;
      case ERROR_ACCESS_DENIED:
        errno = EACCES;
        break;
      default:
        errno = EINVAL;
    }
    return -1;
  }

  if (attr & FILE_ATTRIBUTE_DIRECTORY)
  {
    /* All directories have read & write access */
    return 0;
  }

  if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
  {
    /* no write permission on file */
    errno = EACCES;
    return -1;
  }
  else
    return 0;
}