about summary refs log tree commit diff
path: root/support
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2023-03-02 19:10:37 +0000
committerJoseph Myers <joseph@codesourcery.com>2023-03-02 19:10:37 +0000
commitdee2bea048b688b643a9a3b44b26ca9f7a706fe8 (patch)
tree3e7cd1057154850968d13023bc2e082cfc7940ad /support
parent51aeab9a363a0d000d0912aa3d6490463a26fba2 (diff)
downloadglibc-dee2bea048b688b643a9a3b44b26ca9f7a706fe8.tar.gz
glibc-dee2bea048b688b643a9a3b44b26ca9f7a706fe8.tar.xz
glibc-dee2bea048b688b643a9a3b44b26ca9f7a706fe8.zip
C2x scanf binary constant handling
C2x adds binary integer constants starting with 0b or 0B, and supports
those constants for the %i scanf format (in addition to the %b format,
which isn't yet implemented for scanf in glibc).  Implement that scanf
support for glibc.

As with the strtol support, this is incompatible with previous C
standard versions, in that such an input string starting with 0b or 0B
was previously required to be parsed as 0 (with the rest of the input
potentially matching subsequent parts of the scanf format string).
Thus this patch adds 12 new __isoc23_* functions per long double
format (12, 24 or 36 depending on how many long double formats the
glibc configuration supports), with appropriate header redirection
support (generally very closely following that for the __isoc99_*
scanf functions - note that __GLIBC_USE (DEPRECATED_SCANF) takes
precedence over __GLIBC_USE (C2X_STRTOL), so the case of GNU
extensions to C89 continues to get old-style GNU %a and does not get
this new feature).  The function names would remain as __isoc23_* even
if C2x ends up published in 2024 rather than 2023.

When scanf %b support is added, I think it will be appropriate for all
versions of scanf to follow C2x rules for inputs to the %b format
(given that there are no compatibility concerns for a new format).

Tested for x86_64 (full glibc testsuite).  The first version was also
tested for powerpc (32-bit) and powerpc64le (stdio-common/ and wcsmbs/
tests), and with build-many-glibcs.py.
Diffstat (limited to 'support')
-rw-r--r--support/Makefile1
-rw-r--r--support/xfreopen.c31
-rw-r--r--support/xstdio.h1
3 files changed, 33 insertions, 0 deletions
diff --git a/support/Makefile b/support/Makefile
index a304c5cdc0..d52c472755 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -123,6 +123,7 @@ libsupport-routines = \
   xfclose \
   xfopen \
   xfork \
+  xfreopen \
   xftruncate \
   xgetline \
   xgetsockname \
diff --git a/support/xfreopen.c b/support/xfreopen.c
new file mode 100644
index 0000000000..e2f204cb50
--- /dev/null
+++ b/support/xfreopen.c
@@ -0,0 +1,31 @@
+/* freopen with error checking.
+   Copyright (C) 2023 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+FILE *
+xfreopen (const char *path, const char *mode, FILE *stream)
+{
+  FILE *fp = freopen (path, mode, stream);
+  if (fp == NULL)
+    FAIL_EXIT1 ("could not open %s (mode \"%s\"): %m", path, mode);
+  return fp;
+}
diff --git a/support/xstdio.h b/support/xstdio.h
index 58cc7c0692..5410d42579 100644
--- a/support/xstdio.h
+++ b/support/xstdio.h
@@ -26,6 +26,7 @@ __BEGIN_DECLS
 
 FILE *xfopen (const char *path, const char *mode);
 void xfclose (FILE *);
+FILE *xfreopen (const char *path, const char *mode, FILE *stream);
 
 /* Read a line from FP, using getline.  *BUFFER must be NULL, or a
    heap-allocated pointer of *LENGTH bytes.  Return the number of