在 2019/1/6 9:19, Mateusz 写道:
> W dniu 05.01.2019 o 20:59, Johannes Pfau pisze:
> Maybe it would make sense to add this in crt/io.h? There's already a 
> __USE_MINGW_ACCESS guarded __mingw_access wrapper function which could be 
> extended. And as far as I can see, GCC already sets the __USE_MINGW_ACCESS 
> define, so it likely wouldn't need any changes.
> I've attached patch to mingw-w64 that replaces _access in msvcr110/120 and 
> ucrtbase with Liu Hao version (for me it is too big for inline).
> 

This looks bad to be, as it breaks code which expects the MSVC behavior.

> After patching mingw-w64 (and before building) please go to mingw-w64-crt 
> folder and execute
> aclocal
> automake
> to regenerate makefiles.
> 
> This patch changes only lib files -- it should work.

Oops sorry, the second argument to `access()` can be a bitwise OR'd
mask, so `switch (mode)` is horribly wrong. An amended version is provided:

```
int access(const char *pathname, int mode)
{
  DWORD attr;
  if (mode & ~(F_OK | R_OK | W_OK | X_OK))
    {
      /* Invalid mode specified.  */
      errno = EINVAL;
      return -1;
    }
  attr = GetFileAttributesA(pathname);
  if (attr == INVALID_FILE_ATTRIBUTES)
    {
      /* Set `errno` accordingly.  */
      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 (mode & W_OK)
    {
      /* Directories are always writeable.
       * Normal files are writeable if not read-only.  */
      if (!(attr & FILE_ATTRIBUTE_DIRECTORY) &&
          (attr & FILE_ATTRIBUTE_READONLY))
        {
          errno = EACCES;
          return -1;
        }
    }
  /* On Windows all files are presumed to be readable and executable.
   * They can be opened in a command prompt without any arguments.  */
  return 0;
}
```

N.B. I will not approve this patch because I authored this function.
Somebody else please test it and report any issues. Thanks in advance.

> 
> Regards,
> Mateusz
> 
> 
> 
-- 
Best regards,
LH_Mouse

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to