Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-07 Thread Liu Hao
在 2019/1/7 5:06, Mateusz 写道:
> W dniu 06.01.2019 o 16:17, Liu Hao pisze:
>> 在 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.
> 
> We could add __mingw_access to msvcr110/120 + ucrtbase libs and change a 
> little bit io.h for case if __USE_MINGW_ACCESS is defined (check for 
> __MSVCRT_VERSION__).
> 
> Or we could add __mingw_access to libmingwex and use exact the same version 
> for all msvcrt/msvcr*/ucrt if __USE_MINGW_ACCESS is defined.
> 
> Or we could add __mingw_access to libmingwex and use it only when 
> __MSVCRT_VERSION__ >= 0x1100 && defined(__USE_MINGW_ACCESS) and leave as is 
> now for __MSVCRT_VERSION__ < 0x1100.
> 

The second one sounds appropriate to me, as it is just we deal with
stdio functions.

> I've attached access from MSVC 2010 -- if we agree that mingw-w64 should be 
> patched in some way we could choose detail for _access/__mingw_access.
> 

PLEASE STOP COPYING CODE FROM MICROSOFT SDKS.

We are not willing to receive lawyer's letters. Publishing this code for
others to copy probably already violates its license.


-- 
Best regards,
LH_Mouse

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


Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-06 Thread Mateusz
W dniu 06.01.2019 o 16:17, Liu Hao pisze:
> 在 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.

We could add __mingw_access to msvcr110/120 + ucrtbase libs and change a little 
bit io.h for case if __USE_MINGW_ACCESS is defined (check for 
__MSVCRT_VERSION__).

Or we could add __mingw_access to libmingwex and use exact the same version for 
all msvcrt/msvcr*/ucrt if __USE_MINGW_ACCESS is defined.

Or we could add __mingw_access to libmingwex and use it only when 
__MSVCRT_VERSION__ >= 0x1100 && defined(__USE_MINGW_ACCESS) and leave as is now 
for __MSVCRT_VERSION__ < 0x1100.

>> 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:

I've attached access from MSVC 2010 -- if we agree that mingw-w64 should be 
patched in some way we could choose detail for _access/__mingw_access.

> 
> ```
> 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.

Noticed -- I've provided some code for general discussion how to solve the 
problem (and I copied your code).

Regards,
Mateusz
/***
*access.c - access function
*
*   Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
*   This file has the _access() function which checks on file accessability.
*
***/

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

/***
*int _access(path, amode) - check whether file can be accessed under mode
*
*Purpose:
*   Checks to see if the specified file exists and can be accessed
*   in the given mode.
*
*Entry:
*   _TSCHAR *path - pathname
*   int amode - access mode
*   (0 = exist only, 2 = write, 4 = read, 6 = read/write)
*
*Exit:
*   returns 0 if file has given mode
*   returns -1 and sets errno if file does not have given mode or
*   does not exist
*
*Exceptions:
*
***/

int __cdecl _taccess (
const _TSCHAR *path,
int amode
)
{
errno_t e;
e = _taccess_s(path,amode);

return e ? -1 : 0 ;
}

/***
*errno_t _access_s(path, amode) - check whether file can be accessed under mode
*
*Purpose:
*   Checks to see if the specified file exists and can be accessed
*   in the given mode.
*
*Entry:
*   _TSCHAR *path - pathname
*   int amode - access mode
*   (0 = exist only, 2 = write, 4 = read, 6 = read/write)
*
*Exit:
*   returns 0 if file has given mode
*   returns errno_t for any other errors
*
*Exceptions:
*
***/

errno_t __cdecl _taccess_s (
const _TSCHAR *path,
int amode
)
{

DWORD attr;

_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE( (path != NULL), EINVAL);
_VALIDATE_CLEAR_OSSERR_RETURN_ERRCODE( ((amode & (~6)) == 0), EINVAL);

attr = 

Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-06 Thread Liu Hao
在 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



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


Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-05 Thread Mateusz
W dniu 05.01.2019 o 20:59, Johannes Pfau pisze:
> Am 05.01.19 um 18:56 schrieb Mateusz:
>> ucrtbase.dll is affected too (msvcr100 works OK), so maybe it is better to 
>> add your access() function to msvcr110/120 and ucrtbase (as a replacement) 
>> instead of patching GCC.
>>
>> Regards,
>> Mateusz
>>
>>
> 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).

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.

Regards,
Mateusz
From 71f1504a4181d38118b4d7924f6b6fee0b3d275e Mon Sep 17 00:00:00 2001
From: Mateusz Brzostek 
Date: Sun, 6 Jan 2019 00:54:25 +0100
Subject: [PATCH] Add _access function that works for nul file to msvcr110/120
 and ucrtbase libs

Original implementation
Liu Hao 

Signed-off-by: Mateusz Brzostek 
---
 mingw-w64-crt/Makefile.am|  23 ++--
 mingw-w64-crt/def-include/msvcr110-common.def.in | 144 +++
 mingw-w64-crt/lib-common/ucrtbase.def.in |   4 +-
 mingw-w64-crt/lib32/msvcr110.def.in  |   4 +-
 mingw-w64-crt/lib32/msvcr120.def.in  |   4 +-
 mingw-w64-crt/lib32/msvcr120_app.def.in  |   4 +-
 mingw-w64-crt/lib32/msvcr120d.def.in |   4 +-
 mingw-w64-crt/lib64/msvcr110.def.in  |   4 +-
 mingw-w64-crt/lib64/msvcr120.def.in  |   4 +-
 mingw-w64-crt/lib64/msvcr120_app.def.in  |   4 +-
 mingw-w64-crt/lib64/msvcr120d.def.in |   4 +-
 mingw-w64-crt/stdio/access.c |  65 ++
 12 files changed, 241 insertions(+), 27 deletions(-)
 create mode 100644 mingw-w64-crt/def-include/msvcr110-common.def.in
 create mode 100644 mingw-w64-crt/stdio/access.c

diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index f8b0623..5c0b421 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -213,6 +213,10 @@ src_msvcrt=\
   stdio/fseeki64.c \
   stdio/mingw_lock.c
 
+src_msvcr110=\
+  $(src_msvcrt_common) \
+  stdio/access.c
+
 src_ucrtbase=\
   crt/ucrtbase_compat.c \
   stdio/ucrt_fprintf.c \
@@ -226,7 +230,8 @@ src_ucrtbase=\
   stdio/ucrt_vfprintf.c \
   stdio/ucrt_vprintf.c \
   stdio/ucrt_vsnprintf.c \
-  stdio/ucrt_vsprintf.c
+  stdio/ucrt_vsprintf.c \
+  stdio/access.c
 
 src_msvcrt32=\
   $(src_msvcrt) \
@@ -709,25 +714,25 @@ lib32_libmsvcr100_a_CPPFLAGS=$(CPPFLAGS32) 
-D__LIBMSVCRT__ $(extra_include) $(sy
 EXTRA_lib32_libmsvcr100_a_DEPENDENCIES=lib32/msvcr100.def
 
 lib32_LIBRARIES += lib32/libmsvcr110.a
-lib32_libmsvcr110_a_SOURCES = $(src_msvcrt_common) lib32/msvcr110.def.in
+lib32_libmsvcr110_a_SOURCES = $(src_msvcr110) lib32/msvcr110.def.in
 lib32_libmsvcr110_a_AR = $(DTDEF32) lib32/msvcr110.def && $(AR) $(ARFLAGS)
 lib32_libmsvcr110_a_CPPFLAGS=$(CPPFLAGS32) -D__LIBMSVCRT__ $(extra_include) 
$(sysincludes)
 EXTRA_lib32_libmsvcr110_a_DEPENDENCIES=lib32/msvcr110.def
 
 lib32_LIBRARIES += lib32/libmsvcr120.a
-lib32_libmsvcr120_a_SOURCES = $(src_msvcrt_common) lib32/msvcr120.def.in
+lib32_libmsvcr120_a_SOURCES = $(src_msvcr110) lib32/msvcr120.def.in
 lib32_libmsvcr120_a_AR = $(DTDEF32) lib32/msvcr120.def && $(AR) $(ARFLAGS)
 lib32_libmsvcr120_a_CPPFLAGS=$(CPPFLAGS32) -D__LIBMSVCRT__ $(extra_include) 
$(sysincludes)
 EXTRA_lib32_libmsvcr120_a_DEPENDENCIES=lib32/msvcr120.def
 
 lib32_LIBRARIES += lib32/libmsvcr120d.a
-lib32_libmsvcr120d_a_SOURCES = $(src_msvcrt_common) lib32/msvcr120d.def.in
+lib32_libmsvcr120d_a_SOURCES = $(src_msvcr110) lib32/msvcr120d.def.in
 lib32_libmsvcr120d_a_AR = $(DTDEF32) lib32/msvcr120d.def && $(AR) $(ARFLAGS)
 lib32_libmsvcr120d_a_CPPFLAGS=$(CPPFLAGS32) -D__LIBMSVCRT__ $(extra_include) 
$(sysincludes)
 EXTRA_lib32_libmsvcr120d_a_DEPENDENCIES=lib32/msvcr120d.def
 
 lib32_LIBRARIES += lib32/libmsvcr120_app.a
-lib32_libmsvcr120_app_a_SOURCES = $(src_msvcrt_common) 
lib32/msvcr120_app.def.in
+lib32_libmsvcr120_app_a_SOURCES = $(src_msvcr110) lib32/msvcr120_app.def.in
 lib32_libmsvcr120_app_a_AR = $(DTDEF32) lib32/msvcr120_app.def && $(AR) 
$(ARFLAGS)
 lib32_libmsvcr120_app_a_CPPFLAGS=$(CPPFLAGS32) -D__LIBMSVCRT__ 
$(extra_include) $(sysincludes)
 EXTRA_lib32_libmsvcr120_app_a_DEPENDENCIES=lib32/msvcr120_app.def
@@ -1046,25 +1051,25 @@ lib64_libmsvcr100_a_CPPFLAGS=$(CPPFLAGS64) 
-D__LIBMSVCRT__ $(extra_include) $(sy
 EXTRA_lib64_libmsvcr100_a_DEPENDENCIES=lib64/msvcr100.def
 
 lib64_LIBRARIES += lib64/libmsvcr110.a
-lib64_libmsvcr110_a_SOURCES = $(src_msvcrt_common) lib64/msvcr110.def.in
+lib64_libmsvcr110_a_SOURCES = $(src_msvcr110) lib64/msvcr110.def.in
 lib64_libmsvcr110_a_AR = $(DTDEF64) 

Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-05 Thread Johannes Pfau

Am 05.01.19 um 18:56 schrieb Mateusz:

ucrtbase.dll is affected too (msvcr100 works OK), so maybe it is better to add 
your access() function to msvcr110/120 and ucrtbase (as a replacement) instead 
of patching GCC.

Regards,
Mateusz


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.


Regards,

Johannes


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


Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-05 Thread Mateusz
W dniu 05.01.2019 o 15:32, Liu Hao pisze:
> 在 2019/1/5 21:14, Johannes Pfau 写道:
>> Liu Hao:
>>> There are a few resolution to this at different levels. The most ideal
>>> solution would be providing an `access()` function that behaves
>>> expectedly in gnulib or libiberty, so this recipe can be ported
>>> elsewhere. Modifying GCC source should be considered the least preferred
>>> and last option.
>>
>> OK, that's what I thought. I've attached a new patch for libiberty,
>> could you please have a quick look at it before I submit it to GCC?
>>
>> It currently only handles F_OK which is the only case which really
>> seems to matter for GCC. I do not know how to check if a file is effectively
>> readable/writeable/executable in the windows api. And it seems
>> reimplementing R_OK and all the other flags would mean effectively
>> rewriting the complete access function. What do you think?
>>
> 
> A complete version looks basically like this:
> 
> ```c
>   DWORD 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;
> }
>   switch (mode)
> {
> case F_OK:
> case R_OK:
>   // It is always existent and readable.
>   return 0;
> case W_OK:
>   // Directories are always writeable.
>   if (attr & FILE_ATTRIBUTE_DIRECTORY)
> return 0;
>   // Read-only files are not writeable.
>   if (attr & FILE_ATTRIBUTE_READONLY)
> {
>   errno = EACCES;
>   return -1;
> }
>   return 0;
> case X_OK:
>   // On Windows all files are presumed to be executable.
>   // They can be opened in a command prompt without any arguments.
>   return 0;
> default:
>   errno = EINVAL;
>   return -1;
> }
> ```

ucrtbase.dll is affected too (msvcr100 works OK), so maybe it is better to add 
your access() function to msvcr110/120 and ucrtbase (as a replacement) instead 
of patching GCC.

Regards,
Mateusz



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


Re: [Mingw-w64-public] [PATCH] GCC: Introduce xaccess function in libiberty

2019-01-05 Thread Liu Hao
在 2019/1/5 21:14, Johannes Pfau 写道:
> Liu Hao:
>> There are a few resolution to this at different levels. The most ideal
>> solution would be providing an `access()` function that behaves
>> expectedly in gnulib or libiberty, so this recipe can be ported
>> elsewhere. Modifying GCC source should be considered the least preferred
>> and last option.
> 
> OK, that's what I thought. I've attached a new patch for libiberty,
> could you please have a quick look at it before I submit it to GCC?
> 
> It currently only handles F_OK which is the only case which really
> seems to matter for GCC. I do not know how to check if a file is effectively
> readable/writeable/executable in the windows api. And it seems
> reimplementing R_OK and all the other flags would mean effectively
> rewriting the complete access function. What do you think?
> 

A complete version looks basically like this:

```c
  DWORD 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;
}
  switch (mode)
{
case F_OK:
case R_OK:
  // It is always existent and readable.
  return 0;
case W_OK:
  // Directories are always writeable.
  if (attr & FILE_ATTRIBUTE_DIRECTORY)
return 0;
  // Read-only files are not writeable.
  if (attr & FILE_ATTRIBUTE_READONLY)
{
  errno = EACCES;
  return -1;
}
  return 0;
case X_OK:
  // On Windows all files are presumed to be executable.
  // They can be opened in a command prompt without any arguments.
  return 0;
default:
  errno = EINVAL;
  return -1;
}
```

> Best regards,
> Johannes


-- 
Best regards,
LH_Mouse



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