From e923a4fc391909428914d3bebfb02b5431be338a Mon Sep 17 00:00:00 2001
From: John Scott <jscott@posteo.net>
Date: Fri, 16 Jul 2021 07:34:29 -0400
Subject: [PATCH] support the __STDC_WANT_LIB_EXT2__ feature test macro

this is intended to aid applications that wish to use the functionality
specified in ISO/IEC TR 24731-2:2010. Most of these functions are
specified by POSIX, except for (v)asprintf and some of the wide
character functions.

we don't define __STDC_ALLOC_LIB__ since the latter are not implemented.
Also, implementations are required to diagnose when the definition of
__STDC_WANT_LIB_EXT2__ changes after including additional headers, but
we currently don't check this.
---
 include/stdio.h  | 16 +++++++++++-----
 include/string.h |  9 +++++++--
 include/wchar.h  |  7 ++++++-
 3 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/include/stdio.h b/include/stdio.h
index 3604198c..15687ba3 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -131,9 +131,19 @@ FILE *tmpfile(void);
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
  || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
- || defined(_BSD_SOURCE)
+ || defined(_BSD_SOURCE) \
+ ||(defined(__STDC_WANT_LIB_EXT2__) && __STDC_WANT_LIB_EXT2__)
 FILE *fmemopen(void *__restrict, size_t, const char *__restrict);
 FILE *open_memstream(char **, size_t *);
+int asprintf(char **, const char *, ...);
+int vasprintf(char **, const char *, __isoc_va_list);
+ssize_t getdelim(char **__restrict, size_t *__restrict, int, FILE *__restrict);
+ssize_t getline(char **__restrict, size_t *__restrict, FILE *__restrict);
+#endif
+
+#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
 FILE *fdopen(int, const char *);
 FILE *popen(const char *, const char *);
 int pclose(FILE *);
@@ -149,8 +159,6 @@ int getc_unlocked(FILE *);
 int getchar_unlocked(void);
 int putc_unlocked(int, FILE *);
 int putchar_unlocked(int);
-ssize_t getdelim(char **__restrict, size_t *__restrict, int, FILE *__restrict);
-ssize_t getline(char **__restrict, size_t *__restrict, FILE *__restrict);
 int renameat(int, const char *, int, const char *);
 char *ctermid(char *);
 #define L_ctermid 20
@@ -180,8 +188,6 @@ int fileno_unlocked(FILE *);
 int getw(FILE *);
 int putw(int, FILE *);
 char *fgetln(FILE *, size_t *);
-int asprintf(char **, const char *, ...);
-int vasprintf(char **, const char *, __isoc_va_list);
 #endif
 
 #ifdef _GNU_SOURCE
diff --git a/include/string.h b/include/string.h
index 795a2abc..fa3ad27e 100644
--- a/include/string.h
+++ b/include/string.h
@@ -65,14 +65,19 @@ int strerror_r (int, char *, size_t);
 char *stpcpy(char *__restrict, const char *__restrict);
 char *stpncpy(char *__restrict, const char *__restrict, size_t);
 size_t strnlen (const char *, size_t);
-char *strdup (const char *);
-char *strndup (const char *, size_t);
 char *strsignal(int);
 char *strerror_l (int, locale_t);
 int strcoll_l (const char *, const char *, locale_t);
 size_t strxfrm_l (char *__restrict, const char *__restrict, size_t, locale_t);
 #endif
 
+#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) \
+ ||(defined(__STDC_WANT_LIB_EXT2__) && __STDC_WANT_LIB_EXT2__)
+char *strdup (const char *);
+char *strndup (const char *, size_t);
+#endif
+
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
  || defined(_BSD_SOURCE)
 void *memccpy (void *__restrict, const void *__restrict, int, size_t);
diff --git a/include/wchar.h b/include/wchar.h
index 88eb55b1..6f8af081 100644
--- a/include/wchar.h
+++ b/include/wchar.h
@@ -157,7 +157,6 @@ size_t wcsftime_l (wchar_t *__restrict, size_t, const wchar_t *__restrict, const
 
 #if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
  || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE)  || defined(_BSD_SOURCE)
-FILE *open_wmemstream(wchar_t **, size_t *);
 size_t mbsnrtowcs(wchar_t *__restrict, const char **__restrict, size_t, size_t, mbstate_t *__restrict);
 size_t wcsnrtombs(char *__restrict, const wchar_t **__restrict, size_t, size_t, mbstate_t *__restrict);
 wchar_t *wcsdup(const wchar_t *);
@@ -172,6 +171,12 @@ int wcscoll_l(const wchar_t *, const wchar_t *, locale_t);
 size_t wcsxfrm_l(wchar_t *__restrict, const wchar_t *__restrict, size_t, locale_t);
 #endif
 
+#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) \
+ ||(defined(__STDC_WANT_LIB_EXT2__) && __STDC_WANT_LIB_EXT2__)
+FILE *open_wmemstream(wchar_t **, size_t *);
+#endif
+
 #if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
 int wcwidth (wchar_t);
 int wcswidth (const wchar_t *, size_t);
-- 
2.30.2