Re: [PATCH] read-file: add variants that clear internal memory

2020-05-29 Thread Bruno Haible
Hi Daiki,

> On a different note, it was suggested to disable stdio buffering if
> RF_SENSITIVE is set.  I am attaching a patch for this.

Reading from a regular file in an unbuffered way can be terribly slow.
But here, fread_file reads in large chunks, therefore it's OK.

Also, in the specification of fread_file, I would add a note. Maybe like
this?

diff --git a/lib/read-file.c b/lib/read-file.c
index 36780cc..f13c528 100644
--- a/lib/read-file.c
+++ b/lib/read-file.c
@@ -43,8 +43,11 @@
*LENGTH.  On errors, *LENGTH is undefined, errno preserves the
values set by system functions (if any), and NULL is returned.
 
-   If the RF_SENSITIVE flag is set in FLAGS, the memory buffer
-   internally allocated will be cleared upon failure.  */
+   If the RF_SENSITIVE flag is set in FLAGS:
+ - You should control the buffering of STREAM using 'setvbuf'.  Either
+   clear the buffer of STREAM after closing it, or disable buffering of
+   STREAM before calling this function.
+ - The memory buffer internally allocated will be cleared upon failure.  */
 char *
 fread_file (FILE *stream, int flags, size_t *length)
 {




Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-29 Thread Bruno Haible
Daiki Ueno wrote:
> this change seems to
> break MinGW cross build, because GetFinalPathNameByHandleFunc is defined
> only if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA), but referred to from
> _gl_convert_FILETIME_to_timespec without the guard:

Oops, indeed. With the appropriate _WIN32_WINNT setting, I'm getting errors in
all three files:

C:\cygwin64\home\bruno\testdir2\gllib\gettimeofday.c(95): error C2065: 
'GetSystemTimePreciseAsFileTimeFunc': undeclared identifier

C:\cygwin64\home\bruno\testdir2\gllib\isatty.c(99): error C2065: 
'GetNamedPipeClientProcessIdFunc': undeclared identifier
C:\cygwin64\home\bruno\testdir2\gllib\isatty.c(100): error C2065: 
'QueryFullProcessImageNameFunc': undeclared identifier

C:\cygwin64\home\bruno\testdir2\gllib\stat-w32.c(259): error C2065: 
'GetFinalPathNameByHandleFunc': undeclared identifier

This patch fixes it.


2020-05-29  Bruno Haible  

Fix compilation error on native Windows (regression from 2020-05-28).
Reported by Daiki Ueno.
* lib/gettimeofday.c (GetSystemTimePreciseAsFileTimeFunc): Define as
macro when not using dynamic loading.
* lib/isatty.c (GetNamedPipeClientProcessIdFunc,
QueryFullProcessImageNameFunc): Likewise.
* lib/stat-w32.c (GetFileInformationByHandleExFunc,
GetFinalPathNameByHandleFunc): Likewise.

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 3d53115..93914ba 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -56,6 +56,10 @@ initialize (void)
   initialized = TRUE;
 }
 
+# else
+
+#  define GetSystemTimePreciseAsFileTimeFunc GetSystemTimePreciseAsFileTime
+
 # endif
 
 #endif
diff --git a/lib/isatty.c b/lib/isatty.c
index fc771d1..4c5b8e3 100644
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -71,6 +71,11 @@ initialize (void)
   initialized = TRUE;
 }
 
+#else
+
+# define GetNamedPipeClientProcessIdFunc GetNamedPipeClientProcessId
+# define QueryFullProcessImageNameFunc QueryFullProcessImageName
+
 #endif
 
 static BOOL IsConsoleHandle (HANDLE h)
diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index 02ad9ab..cca12dd 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -78,6 +78,11 @@ initialize (void)
   initialized = TRUE;
 }
 
+#else
+
+# define GetFileInformationByHandleExFunc GetFileInformationByHandleEx
+# define GetFinalPathNameByHandleFunc GetFinalPathNameByHandle
+
 #endif
 
 /* Converts a FILETIME to GMT time since 1970-01-01 00:00:00.  */




Re: [PATCH] gettimeofday: do not use LoadLibrary when built for Windows Store apps

2020-05-29 Thread Steve Lhomme

On 2020-05-29 2:04, Bruno Haible wrote:

Hi,

Steve Lhomme wrote:

LoadLibrary is forbidden in such apps (can only load DLLs from within the app
package).
The API entries are available to all apps linking with the Windows API as found
here:
https://docs.microsoft.com/en-us/uwp/win32-and-com/win32-apis


Thanks for these infos.


+#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) && _WIN32_WINNT >= 
0x0A00 /* _WIN32_WINNT_WIN10 */


The GetSystemTimePreciseAsFileTime function is available starting with Windows 
8,
therefore
   - the condition with WINAPI_FAMILY_PARTITION is not necessary,
   - the condition _WIN32_WINNT >= 0x0A00 is overly restrictive;
 _WIN32_WINNT >= _WIN32_WINNT_WIN8 will work just as well.


OK. I thought GetSystemTimePreciseAsFileTime was not available in Win8 
UWP apps. But it seems it was: 
https://docs.microsoft.com/en-us/windows/win32/apiindex/windows-81-api-sets



I have added a page about the native Windows APIs at
https://gitlab.com/ghwiki/gnow-how/-/wikis/Platforms/Native_Windows

Then here is a patch to avoid LoadLibrary when possible.


2020-05-28  Bruno Haible  

Avoid dynamic loading of Windows API functions when possible.
Reported by Steve Lhomme  in
.
* lib/gettimeofday.c (GetProcAddress,
GetSystemTimePreciseAsFileTimeFuncType,
GetSystemTimePreciseAsFileTimeFunc, initialized, initialize): Don't
define in a build for Windows 8 or higher.
* lib/isatty.c (GetProcAddress, GetNamedPipeClientProcessIdFuncType,
GetNamedPipeClientProcessIdFunc, QueryFullProcessImageNameFuncType,
QueryFullProcessImageNameFunc, initialized, initialize): Don't define
in a build for Windows Vista or higher.
* lib/stat-w32.c (GetProcAddress, GetFileInformationByHandleExFuncType,
GetFileInformationByHandleExFunc, GetFinalPathNameByHandleFuncType,
GetFinalPathNameByHandleFunc, initialized, initialize): Likewise.

diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 1980479..3d53115 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -33,9 +33,11 @@
  
  #ifdef WINDOWS_NATIVE
  
+# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)

+
  /* Avoid warnings from gcc -Wcast-function-type.  */
-# define GetProcAddress \
-   (void *) GetProcAddress
+#  define GetProcAddress \
+(void *) GetProcAddress
  
  /* GetSystemTimePreciseAsFileTime was introduced only in Windows 8.  */

  typedef void (WINAPI * GetSystemTimePreciseAsFileTimeFuncType) (FILETIME 
*lpTime);
@@ -54,6 +56,8 @@ initialize (void)
initialized = TRUE;
  }
  
+# endif

+
  #endif
  
  /* This is a wrapper for gettimeofday.  It is used only on systems

@@ -84,8 +88,10 @@ gettimeofday (struct timeval *restrict tv, void *restrict tz)
   .  */
FILETIME current_time;
  
+# if !(_WIN32_WINNT >= _WIN32_WINNT_WIN8)

if (!initialized)
  initialize ();
+# endif
if (GetSystemTimePreciseAsFileTimeFunc != NULL)
  GetSystemTimePreciseAsFileTimeFunc (¤t_time);
else
diff --git a/lib/isatty.c b/lib/isatty.c
index 6cdc0fb..fc771d1 100644
--- a/lib/isatty.c
+++ b/lib/isatty.c
@@ -39,9 +39,11 @@
  # include 
  #endif
  
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

+
  /* Avoid warnings from gcc -Wcast-function-type.  */
-#define GetProcAddress \
-  (void *) GetProcAddress
+# define GetProcAddress \
+   (void *) GetProcAddress
  
  /* GetNamedPipeClientProcessId was introduced only in Windows Vista.  */

  typedef BOOL (WINAPI * GetNamedPipeClientProcessIdFuncType) (HANDLE hPipe,
@@ -69,6 +71,8 @@ initialize (void)
initialized = TRUE;
  }
  
+#endif

+
  static BOOL IsConsoleHandle (HANDLE h)
  {
DWORD mode;
@@ -84,8 +88,10 @@ static BOOL IsCygwinConsoleHandle (HANDLE h)
BOOL result = FALSE;
ULONG processId;
  
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

if (!initialized)
  initialize ();
+#endif
  
/* GetNamedPipeClientProcessId

   

diff --git a/lib/stat-w32.c b/lib/stat-w32.c
index b9163f5..02ad9ab 100644
--- a/lib/stat-w32.c
+++ b/lib/stat-w32.c
@@ -40,18 +40,20 @@
  #include "pathmax.h"
  #include "verify.h"
  
+#if !(_WIN32_WINNT >= _WIN32_WINNT_VISTA)

+
  /* Avoid warnings from gcc -Wcast-function-type.  */
-#define GetProcAddress \
-  (void *) GetProcAddress
+# define GetProcAddress \
+   (void *) GetProcAddress
  
-#if _GL_WINDOWS_STAT_INODES == 2

+# if _GL_WINDOWS_STAT_INODES == 2
  /* GetFileInformationByHandleEx was introduced only in Windows Vista.  */
  typedef DWORD (WINAPI * GetFileInformationByHandleExFuncType) (HANDLE hFile,
 
FILE_INFO_BY_HANDLE_CLASS fiClass,
 LPVOID 
lpBuffer,
 

wmemchr, wmemcmp, wmemcpy, wmemmove, wmemset: Fix autoconf test

2020-05-29 Thread Bruno Haible
On IRIX 6.5, with cc, I'm seeing a compilation error:

cc -n32 -DHAVE_CONFIG_H -I. -I..  -DGNULIB_STRICT_CHECKING=1 
-I/u/guest/bruno/prefix-n32-cc/include  -g -c -o wcsdup.o wcsdup.c
cc-1119 cc: ERROR File = wcsdup-impl.h, Line = 24
  The "return" expression type differs from the function return type.

  return wmemcpy (copy, s, n);
 ^

1 error detected in the compilation of "wcsdup.c".
gmake[4]: *** [Makefile:3494: wcsdup.o] Error 2

The cause is that the test program in wmemcpy.m4 is only compiled, not linked;
this succeeds even without a wmemcpy() function being declared. Thus,
HAVE_WMEMCPY gets the value 1, and gnulib's wchar.h replacement does not
declare 'wmemcpy'. Then wcsdup.c fails to compile.

This patch fixes it.


2020-05-29  Bruno Haible  

wmemchr, wmemcmp, wmemcpy, wmemmove, wmemset: Fix autoconf test.
* m4/wmemchr.m4 (gl_FUNC_WMEMCHR): Link, not only compile, the test
program.
* m4/wmemcmp.m4 (gl_FUNC_WMEMCMP): Likewise.
* m4/wmemcpy.m4 (gl_FUNC_WMEMCPY): Likewise.
* m4/wmemmove.m4 (gl_FUNC_WMEMMOVE): Likewise.
* m4/wmemset.m4 (gl_FUNC_WMEMSET): Likewise.

diff --git a/m4/wmemchr.m4 b/m4/wmemchr.m4
index b8bbf07..62bd89c 100644
--- a/m4/wmemchr.m4
+++ b/m4/wmemchr.m4
@@ -1,4 +1,4 @@
-# wmemchr.m4 serial 3
+# wmemchr.m4 serial 4
 dnl Copyright (C) 2011-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,7 +10,7 @@ AC_DEFUN([gl_FUNC_WMEMCHR],
   dnl We cannot use AC_CHECK_FUNCS here, because the MSVC 9 header files
   dnl provide this function as an inline function definition.
   AC_CACHE_CHECK([for wmemchr], [gl_cv_func_wmemchr],
-[AC_COMPILE_IFELSE(
+[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
 /* Tru64 with Desktop Toolkit C has a bug:  must be included before
.
diff --git a/m4/wmemcmp.m4 b/m4/wmemcmp.m4
index 4584d1f..701e73f 100644
--- a/m4/wmemcmp.m4
+++ b/m4/wmemcmp.m4
@@ -1,4 +1,4 @@
-# wmemcmp.m4 serial 3
+# wmemcmp.m4 serial 4
 dnl Copyright (C) 2011-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,7 +10,7 @@ AC_DEFUN([gl_FUNC_WMEMCMP],
   dnl We cannot use AC_CHECK_FUNCS here, because the MSVC 9 header files
   dnl provide this function as an inline function definition.
   AC_CACHE_CHECK([for wmemcmp], [gl_cv_func_wmemcmp],
-[AC_COMPILE_IFELSE(
+[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
 /* Tru64 with Desktop Toolkit C has a bug:  must be included before
.
diff --git a/m4/wmemcpy.m4 b/m4/wmemcpy.m4
index 3309248..c9d749d 100644
--- a/m4/wmemcpy.m4
+++ b/m4/wmemcpy.m4
@@ -1,4 +1,4 @@
-# wmemcpy.m4 serial 3
+# wmemcpy.m4 serial 4
 dnl Copyright (C) 2011-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,7 +10,7 @@ AC_DEFUN([gl_FUNC_WMEMCPY],
   dnl We cannot use AC_CHECK_FUNCS here, because the MSVC 9 header files
   dnl provide this function as an inline function definition.
   AC_CACHE_CHECK([for wmemcpy], [gl_cv_func_wmemcpy],
-[AC_COMPILE_IFELSE(
+[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
 /* Tru64 with Desktop Toolkit C has a bug:  must be included before
.
diff --git a/m4/wmemmove.m4 b/m4/wmemmove.m4
index 61598e4..adbc12d 100644
--- a/m4/wmemmove.m4
+++ b/m4/wmemmove.m4
@@ -1,4 +1,4 @@
-# wmemmove.m4 serial 3
+# wmemmove.m4 serial 4
 dnl Copyright (C) 2011-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,7 +10,7 @@ AC_DEFUN([gl_FUNC_WMEMMOVE],
   dnl We cannot use AC_CHECK_FUNCS here, because the MSVC 9 header files
   dnl provide this function as an inline function definition.
   AC_CACHE_CHECK([for wmemmove], [gl_cv_func_wmemmove],
-[AC_COMPILE_IFELSE(
+[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
 /* Tru64 with Desktop Toolkit C has a bug:  must be included before
.
diff --git a/m4/wmemset.m4 b/m4/wmemset.m4
index e6aa3fb..0148ea3 100644
--- a/m4/wmemset.m4
+++ b/m4/wmemset.m4
@@ -1,4 +1,4 @@
-# wmemset.m4 serial 3
+# wmemset.m4 serial 4
 dnl Copyright (C) 2011-2020 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -10,7 +10,7 @@ AC_DEFUN([gl_FUNC_WMEMSET],
   dnl We cannot use AC_CHECK_FUNCS here, because the MSVC 9 header files
   dnl provide this function as an inline function definition.
   AC_CACHE_CHECK([for wmemset], [gl_cv_func_wmemset],
-[AC_COMPILE_IFELSE(
+[AC_LINK_IFELSE(
[AC_LANG_PROGRAM([[
 /* Tru64 with Desktop Toolkit C has a bug:  must be included before
.




fnmatch: rely on more gnulib modules

2020-05-29 Thread Bruno Haible
The 'fnmatch' module currently tests for wmemcpy() differently than wmemcpy.m4
does. It also lacks the workarounds from the 'btowc' and 'iswctype' modules.

Therefore here is a proposed patch to let this module use more from gnulib.
Accordingly, the code becomes simpler.

Paul, is this OK from the perspective of a future merge with glibc?


2020-05-29  Bruno Haible  

fnmatch: Rely on more gnulib modules.
* modules/fnmatch (Depends-on): Add btowc, isblank, iswctype, wmemchr,
wmempcpy, mempcpy.
* lib/fnmatch.c: Assume that HAVE_WCTYPE_H, HAVE_BTOWC, HAVE_ISWCTYPE,
HAVE_WMEMCHR, HAVE_WMEMPCPY, HAVE_ISBLANK, HAVE_DECL_ISBLANK,
HAVE_MEMPCPY are all 1.
* m4/fnmatch.m4 (gl_PREREQ_FNMATCH): Don't test for btowc, isblank,
iswctype, mempcpy, wmemchr, wmemcpy, wmempcpy, .

>From 1c61fb1eee1d63a110575258803637865a119762 Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Fri, 29 May 2020 14:01:54 +0200
Subject: [PATCH] fnmatch: Rely on more gnulib modules.

* modules/fnmatch (Depends-on): Add btowc, isblank, iswctype, wmemchr,
wmempcpy, mempcpy.
* lib/fnmatch.c: Assume that HAVE_WCTYPE_H, HAVE_BTOWC, HAVE_ISWCTYPE,
HAVE_WMEMCHR, HAVE_WMEMPCPY, HAVE_ISBLANK, HAVE_DECL_ISBLANK,
HAVE_MEMPCPY are all 1.
* m4/fnmatch.m4 (gl_PREREQ_FNMATCH): Don't test for btowc, isblank,
iswctype, mempcpy, wmemchr, wmemcpy, wmempcpy, .
---
 ChangeLog   | 11 
 lib/fnmatch.c   | 80 +
 m4/fnmatch.m4   |  6 ++---
 modules/fnmatch |  6 +
 4 files changed, 42 insertions(+), 61 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 566dfab..2d56a7b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,16 @@
 2020-05-29  Bruno Haible  
 
+	fnmatch: Rely on more gnulib modules.
+	* modules/fnmatch (Depends-on): Add btowc, isblank, iswctype, wmemchr,
+	wmempcpy, mempcpy.
+	* lib/fnmatch.c: Assume that HAVE_WCTYPE_H, HAVE_BTOWC, HAVE_ISWCTYPE,
+	HAVE_WMEMCHR, HAVE_WMEMPCPY, HAVE_ISBLANK, HAVE_DECL_ISBLANK,
+	HAVE_MEMPCPY are all 1.
+	* m4/fnmatch.m4 (gl_PREREQ_FNMATCH): Don't test for btowc, isblank,
+	iswctype, mempcpy, wmemchr, wmemcpy, wmempcpy, .
+
+2020-05-29  Bruno Haible  
+
 	wmemchr, wmemcmp, wmemcpy, wmemmove, wmemset: Fix autoconf test.
 	* m4/wmemchr.m4 (gl_FUNC_WMEMCHR): Link, not only compile, the test
 	program.
diff --git a/lib/fnmatch.c b/lib/fnmatch.c
index 224512d..db4da5e 100644
--- a/lib/fnmatch.c
+++ b/lib/fnmatch.c
@@ -32,17 +32,8 @@
 #include 
 #include 
 #include 
-
-#define WIDE_CHAR_SUPPORT \
-  (HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
-   && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
-
-/* For platform which support the ISO C amendment 1 functionality we
-   support user defined character classes.  */
-#if defined _LIBC || WIDE_CHAR_SUPPORT
-# include 
-# include 
-#endif
+#include 
+#include 
 
 /* We need some of the locale data (the collation sequence information)
but there is no interface to get this information in general.  Therefore
@@ -82,50 +73,33 @@ extern int fnmatch (const char *pattern, const char *string, int flags);
 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
 
 
-# if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
-#  define isblank(c) ((c) == ' ' || (c) == '\t')
-# endif
-
 # define STREQ(s1, s2) (strcmp (s1, s2) == 0)
 
-# if defined _LIBC || WIDE_CHAR_SUPPORT
-/* The GNU C library provides support for user-defined character classes
-   and the functions from ISO C amendment 1.  */
-#  ifdef CHARCLASS_NAME_MAX
-#   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
-#  else
+/* Provide support for user-defined character classes, based on the functions
+   from ISO C 90 amendment 1.  */
+# ifdef CHARCLASS_NAME_MAX
+#  define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
+# else
 /* This shouldn't happen but some implementation might still have this
problem.  Use a reasonable default value.  */
-#   define CHAR_CLASS_MAX_LENGTH 256
-#  endif
+#  define CHAR_CLASS_MAX_LENGTH 256
+# endif
 
-#  ifdef _LIBC
-#   define IS_CHAR_CLASS(string) __wctype (string)
-#  else
-#   define IS_CHAR_CLASS(string) wctype (string)
-#  endif
+# ifdef _LIBC
+#  define IS_CHAR_CLASS(string) __wctype (string)
+# else
+#  define IS_CHAR_CLASS(string) wctype (string)
+# endif
 
-#  ifdef _LIBC
-#   define ISWCTYPE(WC, WT) __iswctype (WC, WT)
-#  else
-#   define ISWCTYPE(WC, WT) iswctype (WC, WT)
-#  endif
+# ifdef _LIBC
+#  define ISWCTYPE(WC, WT) __iswctype (WC, WT)
+# else
+#  define ISWCTYPE(WC, WT) iswctype (WC, WT)
+# endif
 
-#  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
+# if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
 /* In this case we are implementing the multibyte character handling.  */
-#   define HANDLE_MULTIBYTE 1
-#  endif
-
-# else
-#  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, 'xdigit'.  */
-
-#  define IS_CHAR_CLASS(string)   \
-   (STREQ (string, "alpha") || STREQ (

Re: [PATCH] read-file: add variants that clear internal memory

2020-05-29 Thread Daiki Ueno
Bruno Haible  writes:

>> On a different note, it was suggested to disable stdio buffering if
>> RF_SENSITIVE is set.  I am attaching a patch for this.
>
> Reading from a regular file in an unbuffered way can be terribly slow.
> But here, fread_file reads in large chunks, therefore it's OK.
>
> Also, in the specification of fread_file, I would add a note. Maybe like
> this?

Thank you, amended the documentation and pushed.

Regards,
-- 
Daiki Ueno



do not use GetModuleHandle when built for Windows Store apps

2020-05-29 Thread Bruno Haible
GetModuleHandle is like LoadLibrary: it is used for dynamic lookup of
Windows API functions. And Windows Store apps can't use it.

This patch fixes some of the issues. The other ones, in poll.c and select.c,
are harder to fix, because it's not easy to link directly against ntdll.dll [1].

[1] 
https://stackoverflow.com/questions/35509388/link-to-ntdll-lib-and-call-functions-inside-ntdll-dll


2020-05-29  Bruno Haible  

Avoid dynamic lookup of Windows API functions when possible.
* lib/getaddrinfo.c (GetProcAddress, getaddrinfo_func,
freeaddrinfo_func, getnameinfo_func, getaddrinfo_ptr, freeaddrinfo_ptr,
getnameinfo_ptr): Don't define in a build for Windows XP or higher.
(use_win32_p): Define differently.
* lib/link.c (GetProcAddress, CreateHardLinkFuncType,
CreateHardLinkFunc, initialized, initialize): Don't define in a build
for Windows XP or higher.

diff --git a/lib/getaddrinfo.c b/lib/getaddrinfo.c
index 99cb709..1db9be8 100644
--- a/lib/getaddrinfo.c
+++ b/lib/getaddrinfo.c
@@ -86,9 +86,11 @@ freeaddrinfo (struct addrinfo *ai)
 
 # ifdef WINDOWS_NATIVE
 
+#  if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
+
 /* Avoid warnings from gcc -Wcast-function-type.  */
-#  define GetProcAddress \
-(void *) GetProcAddress
+#   define GetProcAddress \
+ (void *) GetProcAddress
 
 typedef int (WSAAPI *getaddrinfo_func) (const char*, const char*,
 const struct addrinfo*,
@@ -135,6 +137,29 @@ use_win32_p (void)
 
   return 1;
 }
+
+#  else
+
+static int
+use_win32_p (void)
+{
+  static int done = 0;
+
+  if (!done)
+{
+  done = 1;
+
+  gl_sockets_startup (SOCKETS_1_1);
+}
+
+  return 1;
+}
+
+#   define getaddrinfo_ptr getaddrinfo
+#   define freeaddrinfo_ptr freeaddrinfo
+#   define getnameinfo_ptr getnameinfo
+
+#  endif
 # endif
 
 static bool
@@ -161,6 +186,7 @@ getaddrinfo (const char *restrict nodename,
  const char *restrict servname,
  const struct addrinfo *restrict hints,
  struct addrinfo **restrict res)
+#undef getaddrinfo
 {
   struct addrinfo *tmp;
   int port = 0;
@@ -362,6 +388,7 @@ getaddrinfo (const char *restrict nodename,
 /* Free 'addrinfo' structure AI including associated storage.  */
 void
 freeaddrinfo (struct addrinfo *ai)
+#undef freeaddrinfo
 {
 # ifdef WINDOWS_NATIVE
   if (use_win32_p ())
@@ -388,6 +415,7 @@ getnameinfo (const struct sockaddr *restrict sa, socklen_t 
salen,
  char *restrict node, socklen_t nodelen,
  char *restrict service, socklen_t servicelen,
  int flags)
+#undef getnameinfo
 {
 # ifdef WINDOWS_NATIVE
   if (use_win32_p ())
diff --git a/lib/link.c b/lib/link.c
index 8e079d2..8680e3e 100644
--- a/lib/link.c
+++ b/lib/link.c
@@ -30,9 +30,11 @@
 #  define WIN32_LEAN_AND_MEAN
 #  include 
 
+#  if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
+
 /* Avoid warnings from gcc -Wcast-function-type.  */
-#  define GetProcAddress \
-(void *) GetProcAddress
+#   define GetProcAddress \
+ (void *) GetProcAddress
 
 /* CreateHardLink was introduced only in Windows 2000.  */
 typedef BOOL (WINAPI * CreateHardLinkFuncType) (LPCSTR lpFileName,
@@ -53,14 +55,24 @@ initialize (void)
   initialized = TRUE;
 }
 
+#  else
+
+#   define CreateHardLinkFunc CreateHardLink
+
+#  endif
+
 int
 link (const char *file1, const char *file2)
 {
   char *dir;
   size_t len1 = strlen (file1);
   size_t len2 = strlen (file2);
+
+#  if !(_WIN32_WINNT >= _WIN32_WINNT_WINXP)
   if (!initialized)
 initialize ();
+#  endif
+
   if (CreateHardLinkFunc == NULL)
 {
   /* System does not support hard links.  */




Re: fnmatch: rely on more gnulib modules

2020-05-29 Thread Paul Eggert
On 5/29/20 5:02 AM, Bruno Haible wrote:

> Paul, is this OK from the perspective of a future merge with glibc?

Yes, it's an improvement in that department; thanks.



Re: fnmatch: rely on more gnulib modules

2020-05-29 Thread Bruno Haible
> > Paul, is this OK from the perspective of a future merge with glibc?
> 
> Yes, it's an improvement in that department; thanks.

OK. I've pushed it.

Bruno




gnulib-tool: Fix link errors with a particular set of modules on mingw

2020-05-29 Thread Bruno Haible
A testdir created by this command:

./gnulib-tool --create-testdir --dir=../testdir --single-configure clean-temp 
crypto/gc getaddrinfo getlogin getlogin_r gettimeofday isatty link localename 
opendir physmem poll putenv read readdir relocatable-prog rename rewinddir 
select sethostname socket stat fstat tmpdir tmpfile uname utime write cond lock

produces link errors on mingw, regarding symbols such as rpl_malloc, rpl_strdup,
etc., all found in the generated gllib/libgnu.a.

This patch fixes it.


2020-05-29  Bruno Haible  

gnulib-tool: Fix link errors with a particular set of modules on mingw.
* gnulib-tool (func_emit_tests_Makefile_am): Add ../lib/libgnu.a to
LDADD a second time, after the second occurrence of libtests.a.
* pygnulib/GLEmiter.py (tests_Makefile_am): Likewise.

diff --git a/gnulib-tool b/gnulib-tool
index 01c6745..20a2a3e 100755
--- a/gnulib-tool
+++ b/gnulib-tool
@@ -4165,8 +4165,6 @@ func_emit_tests_Makefile_am ()
   echo "  -I${testsbase_inverse} -I\$(srcdir)/${testsbase_inverse} \\"
   echo "  -I${testsbase_inverse}/${sourcebase-lib} 
-I\$(srcdir)/${testsbase_inverse}/${sourcebase-lib}"
   echo
-  local_ldadd_before=''
-  local_ldadd_after=''
   if $use_libtests; then
 # All test programs need to be linked with libtests.a.
 # It needs to be passed to the linker before ${libname}.${libext}, since
@@ -4176,10 +4174,10 @@ func_emit_tests_Makefile_am ()
 # module whose dependency to 'progname' is voluntarily omitted).
 # The LIBTESTS_LIBDEPS can be passed to the linker once or twice, it does
 # not matter.
-local_ldadd_before=' libtests.a'
-local_ldadd_after=' libtests.a $(LIBTESTS_LIBDEPS)'
+echo "LDADD = libtests.a 
${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext} libtests.a 
${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext} 
\$(LIBTESTS_LIBDEPS)"
+  else
+echo "LDADD = ${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}"
   fi
-  echo "LDADD =${local_ldadd_before} 
${testsbase_inverse}/${sourcebase-lib}/${libname}.${libext}${local_ldadd_after}"
   echo
   if $use_libtests; then
 echo "libtests_a_SOURCES ="
diff --git a/pygnulib/GLEmiter.py b/pygnulib/GLEmiter.py
index f0746f8..441d295 100644
--- a/pygnulib/GLEmiter.py
+++ b/pygnulib/GLEmiter.py
@@ -1123,8 +1123,6 @@ AC_DEFUN([%V1%_LIBSOURCES], [
 (testsbase_inverse, sourcebase, testsbase_inverse, sourcebase)
 emit += '\n'
 
-local_ldadd_before = string()
-local_ldadd_after = string()
 if libtests:
 # All test programs need to be linked with libtests.a.
 # It needs to be passed to the linker before ${libname}.${libext}, 
since
@@ -1134,11 +1132,12 @@ AC_DEFUN([%V1%_LIBSOURCES], [
 # 'error' module whose dependency to 'progname' is voluntarily 
omitted).
 # The LIBTESTS_LIBDEPS can be passed to the linker once or twice, 
it does
 # not matter.
-local_ldadd_before = ' libtests.a'
-local_ldadd_after = ' libtests.a $(LIBTESTS_LIBDEPS)'
-emit += 'LDADD =%s %s/%s/%s.%s%s\n\n' % \
-(local_ldadd_before, testsbase_inverse, sourcebase, libname, 
libext,
- local_ldadd_after)
+emit += 'LDADD = libtests.a %s/%s/%s.%s libtests.a %s/%s/%s.%s 
$(LIBTESTS_LIBDEPS)\n\n' % \
+(testsbase_inverse, sourcebase, libname, libext,
+ testsbase_inverse, sourcebase, libname, libext)
+else:
+emit += 'LDADD = %s/%s/%s.%s\n\n' % \
+(testsbase_inverse, sourcebase, libname, libext)
 if libtests:
 emit += 'libtests_a_SOURCES =\n'
 # Here we use $(LIBOBJS), not @LIBOBJS@. The value is the same. 
However,