aboutsummaryrefslogtreecommitdiff
path: root/newlib/libc/stdio
AgeCommit message (Collapse)AuthorFilesLines
2025-03-14newlib: fix uninitialized character count being used when printing float ↵Igor Petrov1-0/+1
without "_printf_float" being linked Patch fixes wrong number of written characters being returend from 'printf' family of functionsx when '_printf_float' is not linked (nano.specs). If user tries to print a floating point number anyway, returned number of written characters is not correct. For example in printf("%d%f", 1, 1.0); should return 1, but actaully returns 2.
2025-01-17Fixes compile failure if REENTRANT_SYSCALLS_PROVIDED and ↵Markus Eisenmann2-3/+2
MISSING_SYSCALL_NAMES defined If the macros REENTRANT_SYSCALLS_PROVIDED and MISSING_SYSCALL_NAMES are defined some _reent_*-functions are replaced by the system-call and this leads to compile-warning or a runtime-failure. * newlib/libc/stdio/fopen.c _open_r is replaces by open(), declared in <fcntl.h> * newlib/libc/stdio64/fopen64.c ditto * newlib/libc/stdio/rename.c _rename_r is rename() itself; i.e, fix recursion
2024-03-11libc/stdio: Remove wchar_t functions from NEWLIB_NANO_FORMATTED_IOTorbjörn SVENSSON1-26/+26
Regression tested in basepoints/gcc-14-9032-g945cb8490cb with arm-none-eabi. Example failure without this change: $ arm-none-eabi-g++ .../pr100611.C -std=c++20 --specs=nosys.specs -o /dev/null --specs=nano.specs .../ld: .../libc_nano.a(libc_a-svfwprintf.o): in function `_svfwprintf_r': (.text._svfwprintf_r+0xd8): undefined reference to `__ssputws_r' .../ld: (.text._svfwprintf_r+0x834): undefined reference to `__ssputws_r' .../ld: (.text._svfwprintf_r+0x85c): undefined reference to `__ssputws_r' .../ld: (.text._svfwprintf_r+0x8a8): undefined reference to `__ssputws_r' .../ld: (.text._svfwprintf_r+0x8dc): undefined reference to `__ssputws_r' .../ld: .../libc_nano.a(libc_a-svfwprintf.o):(.text._svfwprintf_r+0x900): more undefined references to `__ssputws_r' follow collect2: error: ld returned 1 exit status Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com> Co-authored-by: Yvan ROUX <yvan.roux@foss.st.com>
2024-01-22newlib: docs: add "Function " to every function nodeMike Frysinger1-75/+75
When creating a split manual with one-node-per-page, the main index.html ends up getting clobbered by the page for the index() function because it uses "@node index" which, for html, also creates an index.html page. To remedy this, add "Function " to every function node so now we output "Function-index.html" and avoid clobbering. It also namespaces every other function and helps make sure we don't clobber anything else. Otherwise, there isn't really much rendering difference as @node text is mostly internal. Node title text comes from @section instead.
2024-01-19fputwc: call __fputwc directlyCorinna Vinschen2-2/+7
Avoid another hop in the call stack. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2024-01-19ORIENT: avoid "expression has no effect" warningCorinna Vinschen3-3/+3
The warning "right-hand operand of comma expression has no effect [-Wunused-value]" crops up with -Wall in cases where ORIENT is called without checking or assigning its value. Explicitely void the expression in these cases to avoid the warning. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2024-01-19_fputwc_r: actually return result of __fputwcCorinna Vinschen1-1/+1
Compiling with -Wall uncovered a bug in _fputwc_r introduced in commit 09119463a1445 ("stdio: split byte- and wide-char-oriented low-level output functions"). The underlying function __fputwc has been accidentally called without fetching its return value. So the return value of _fputwc_r (and thus fputwc) was undefined. Fixes: 09119463a1445 ("stdio: split byte- and wide-char-oriented low-level output functions" Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-20stdio: drop unnecessary calls to ORIENTCorinna Vinschen7-20/+0
Now that the low-level functions set and test stream orientation, a few calls in API functions are redundant. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-20stdio: set and test stream orientation in low-level input function __srget_rCorinna Vinschen1-0/+5
This allows to set and test orientation correctly if input is only using macros from stdio.h. Wide-char-oriented functions must call __srefill_r directly. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-20stdio: split byte- and wide-char-oriented low-level output functionsCorinna Vinschen7-20/+140
Introduce function __swbufw_r and macros/inline-functions called __swputc_r. Call these functions/macros exclusively from wide-char functions. This allows to set and test the stream orientation correctly even if output is only performed using byte-oriented macros from stdio.h. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-20stdio: Fix bugs in commit 3d94e07c49b5Takashi Yano27-12/+43
The commit 3d94e07c49b5 has a few bugs which cause testsuite failure in libstdc++. This is due to excess orientation check in __srefill_r() and _ungetc_r(). Further, sscanf() family also calls ssvfscanf() family with fp->_file == -1. This causes undesired orientation set/check for sscanf() family. This patch fixes these problems. Also, as in GLibC, do not set orientation in ungetc, and only set, but do not check orientation in ungetwc. Fixes: 3d94e07c49b5 ("newlib: libc: Fix crash on fprintf to a wide-oriented stream.") Reported-by: Christophe Lyon <christophe.lyon@linaro.org> Reported-by: Joel Sherrill <joel@rtems.org> Co-developed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp> Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-17__sfvwrite_r: call __sputc_r rather than putc in __SCLE caseCorinna Vinschen1-1/+4
__sfvwrite_r is called under lock. There's no reason to call putc, locking the file recursively. Add a comment that locking is required when calling __sfvwrite_r. Fixes: 49d64538cd20 ("* libc/include/stdio.h (FILE): define __SCLE for "convert line endings" for Cygwin.") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-17v{fs}printf/v{fs}wprintf: create external output helpersCorinna Vinschen11-263/+312
So far, the printf family of functions has two output helper functions called __sprint_r and __sfputs_r. Both are called from all variants of vfprintf as well as vfwprintf. There are also analogue helper functions for the string-creating functions vsprintf/vswprintf called __ssprint_r and __ssputs_r. However, the helpers are built once when building vfprintf/vsprintf with the INTEGER_ONLY flag, and then they are part of the vfiprintf.c and vsiprintf.c files. The problem is this: Even if a process only calls vfwprintf or the non-INTEGER_ONLY vfprintf it will always have to include the INTEGER_ONLY vfiprintf. Otherwise the helper functions are undefined. Analogue for the string-creating functions. That's a useless waste of space by including one (or two) big, unused function, if newlib is linked in statically. Create new files to define the printf output helpers separately and split them into byte-oriented and wide-char-oriented functions. This allows to link only the required functions. Also, simplify the string output helpers and fix a potential (but unlikely) buffer overflow in __ssprint_r. Fixes: 8a0efa53e449 ("import newlib-2000-02-17 snapshot") Fixes: 6121968b198d ("* libc/include/stdio.h (__VALIST): Guard against multiple definition.") Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2023-11-10newlib: libc: Fix crash on fprintf to a wide-oriented stream.Takashi Yano19-40/+92
Previously, fprintf() on a wide-oriented stream crashes or outputs garbage. This is because a narrow char string which can be odd bytes in length is cast into a wide char string which should be even bytes in length in __sprint_r/__sfputs_r based on the __SWID flag. As a result, if the length is odd bytes, the reading buffer runs over the buffer length, which causes a crash. If the length is even bytes, garbage is printed. With this patch, any output to the stream which is set to different orientation fails with error just like glibc. Note that it behaves differently from other libc implementations such as BSD, musl and Solaris. Reviewed-by: Corinna Vinschen <corinna@vinschen.de> Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2023-04-26Fix _REENT_EMERGENCY() if TLS is enabledSebastian Huber1-1/+1
If the thread-local storage (TLS) support was enabled, the _REENT_EMERGENCY() object had the wrong size. It must be a buffer of length _REENT_EMERGENCY_SIZE and not just a single character.
2022-08-31Fix some Coverity Scan errors.Jeff Johnston2-0/+4
2022-08-31Restore _lock initialization in non-single threaded modeTorbjörn SVENSSON1-0/+4
When __SINGLE_THREAD__ is not defined, stdin, stdout and stderr needs to have their _lock instance initialized. The __sfp() method is not invoked for the 3 mentioned fds thus, the std() method needs to handle the initialization of the lock. This is more or less a revert of 382550072b49430f8c69adee937a0ba07bd385e6 Contributed by STMicroelectronics Signed-off-by: Torbjörn SVENSSON <torbjorn.svensson@foss.st.com>
2022-07-13Add --enable-newlib-reent-thread-local optionMatt Joyce2-0/+12
By default, Newlib uses a huge object of type struct _reent to store thread-specific data. This object is returned by __getreent() if the __DYNAMIC_REENT__ Newlib configuration option is defined. The reentrancy structure contains for example errno and the standard input, output, and error file streams. This means that if an application only uses errno it has a dependency on the file stream support even if it does not use it. This is an issue for lower end targets and applications which need to qualify the software according to safety standards (for example ECSS-E-ST-40C, ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333). If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct _reent is replaced by dedicated thread-local objects for each struct _reent member. The thread-local objects are defined in translation units which use the corresponding object.
2022-07-13Add _REENT_IS_NULL()Matt Joyce1-1/+2
In a follow up patch, struct _reent is optionally replaced by dedicated thread-local objects. In this case,_REENT is optionally defined to NULL. Add the _REENT_IS_NULL() macro to disable this check on demand.
2022-07-13Add _REENT_CLEANUP(ptr)Matt Joyce3-4/+4
Add a _REENT_CLEANUP() macro to encapsulate access to the __cleanup member of struct reent. This will help to replace the struct member with a thread-local storage object in a follow up patch.
2022-07-13Add _REENT_INC(ptr)Matt Joyce1-3/+3
Add a _REENT_INC() macro to encapsulate access to the _inc member of struct reent. This will help to replace the struct member with a thread-local storage object in a follow up patch.
2022-07-13Add _REENT_STDERR(ptr)Matt Joyce1-2/+2
Add a _REENT_STDERR() macro to encapsulate access to the _stderr member of struct reent. This will help to replace the struct member with a thread-local storage object in a follow up patch.
2022-07-13Add _REENT_STDOUT(ptr)Matt Joyce1-2/+2
Add a _REENT_STDOUT() macro to encapsulate access to the _stdout member of struct reent. This will help to replace the struct member with a thread-local storage object in a follow up patch.
2022-07-13Add _REENT_STDIN(ptr)Matt Joyce1-2/+2
Add a _REENT_STDIN() macro to encapsulate access to the _stdin member of struct reent. This will help to replace the struct member with a thread-local storage object in a follow up patch.
2022-07-13Add _REENT_ERRNO(ptr)Matt Joyce37-105/+105
Add a _REENT_ERRNO() macro to encapsulate the access to the _errno member of struct reent. This will help to replace the structure member with a thread-local storage object in a follow up patch. Replace uses of __errno_r() with _REENT_ERRNO(). Keep __errno_r() macro for potential users outside of Newlib.
2022-07-04Cygwin: stdio: don't try again to read after EOFKen Brown1-8/+0
This reverts commit 1f8f7e2d54, "* libc/stdio/refill.c (__srefill): Try again after EOF on Cygwin." If EOF is set on a file, the stdio input functions will now immediately return EOF rather than trying again to read. This aligns Cygwin's behavior to that of Linux. Addresses: https://cygwin.com/pipermail/cygwin/2022-June/251672.html
2022-06-10Use global stdio streams for all configurationsSebastian Huber3-73/+1
The _REENT_GLOBAL_STDIO_STREAMS was introduced by commit 668a4c8722090fffd10869dbb15b879651c1370d in 2017. Since then it was enabled by default for RTEMS. Recently, the option was enabled for Cygwin which previously used an alternative implementation to use global stdio streams. In Newlib, the stdio streams are defined to thread-specific pointers _reent::_stdin, _reent::_stdout and _reent::_stderr. If the option is disabled (the default for most systems), then these pointers are initialized to thread-specific FILE objects which use file descriptors 0, 1, and 2, respectively. There are at least three problems with this: (1) The thread-specific FILE objects are closed by _reclaim_reent(). This leads to problems with language run-time libraries that provide wrappers to the C/POSIX stdio streams (for example C++ and Ada), since they use the thread-specific FILE objects of the initialization thread. In case the initialization thread is deleted, then they use freed memory. (2) Since thread-specific FILE objects are used with a common output device via file descriptors 0, 1 and 2, the locking at FILE object level cannot ensure atomicity of the output, e.g. a call to printf(). (3) There are resource managment issues, see: https://sourceware.org/pipermail/newlib/2022/019558.html https://bugs.linaro.org/show_bug.cgi?id=5841 This patch enables the _REENT_GLOBAL_STDIO_STREAMS behaviour for all Newlib configurations and removes the option. This removes a couple of #ifdef blocks.
2022-06-10Fix __fp_lock_all() and __fp_unlock_all()Sebastian Huber1-0/+4
For _REENT_GLOBAL_STDIO_STREAMS, lock/unlock all FILE objects. In the repository, this function is only used by Cygwin during process forks. Since Cygwin enabled _REENT_GLOBAL_STDIO_STREAMS recently, without this fix no FILE object at all was locked.
2022-06-07Fix __sglue inititializationSebastian Huber1-4/+0
Do not initialize __sglue with the FILE objects of _GLOBAL_REENT to avoid a double use in the !_REENT_SMALL and !_REENT_GLOBAL_STDIO_STREAMS configurations which didn't use a thread-specific reentrancy structure.
2022-05-19Fix __sFILE::_lock initializationSebastian Huber1-8/+0
The __sFILE::_lock member is present if __SINGLE_THREAD__ is not defined. In this case, it is initialized in __sfp(). It is a bug to do it sometimes also in std().
2022-05-17Fix stdio exit handlingSebastian Huber1-12/+17
Make sure that the stdio exit handler is set in all stdio initialization paths. The bug was introduced by commit 26747c47bc0a1137e02e0377306d721cc3478855.
2022-05-13Remove __sglue member for one configurationMatt Joyce2-1/+13
Remove __sglue member of struct reent when _REENT_GLOBAL_STDIO_STREAMS is defined.
2022-05-13Add global __sglue object for all configurationsMatt Joyce4-9/+12
Added a new global __sglue object for all configurations. Decouples the global file object list from the _GLOBAL_REENT structure by using this new object instead of the __sglue member of _GLOBAL_REENT in __sfp() and _fwalk_sglue().
2022-05-13stdio: Replace _fwalk_reent() with _fwalk_sglue()Sebastian Huber6-17/+22
Replaced _fwalk_reent() with _fwalk_sglue(). The change adds an extra __sglue object as a parameter, which will allow the passing of a global __sglue object separate from the __sglue member of struct _reent. The global __sglue object will be added in a follow-on patch.
2022-05-13Add stdio_exit_handler()Matt Joyce1-1/+10
Add a dedicated stdio exit handler to avoid using _GLOBAL_REENT in exit().
2022-05-13Add CLEANUP_FILE defineMatt Joyce1-19/+19
Define the configuration-dependent constant CLEANUP_FILE for use in cleanup_stdio(). This will reduce duplicate code during the addition of a dedicated stdio atexit handler in a follow-on patch.
2022-05-13Move __sglue initializations to __sfp()Matt Joyce1-3/+7
Moved last remaining __sglue initializations from __sinit() to __sfp(). The move better encapsulates access to __sglue and facilitates its decoupling from struct _reent in a follow-on patch.
2022-05-13Remove __sinit_locks / __sinit_recursive_mutexMatt Joyce2-18/+3
Remove __sinit_lock_acquire() and __sinit_lock_release(). Replace these with __sfp_lock_acquire() and __sfp_lock_release(), respectively. This eliminates a potential deadlock issue between __sinit() and __sfp(). Remove now unused __sinit_recursive_mutex and __lock___sinit_recursive_mutex.
2022-05-13Add two __sglue initialization macrosMatt Joyce1-6/+1
Added _REENT_INIT_SGLUE and _REENT_INIT_SGLUE_ZEROED macros to initialize __sglue member of struct _reent. This allows further simplification of __sinit() and facilitates the removal of __sglue as a member of struct _reent for certain configurations in a follow-on patch.
2022-05-13Remove duplicate sglue initializationsMatt Joyce1-3/+0
Removed duplicate sglue initializations from __sinit(). These are already initialized in the _REENT_INIT macro in sys/reent.h. This simplification enables the reduction of _GLOBAL_REENT dependency in a follow-on patch.
2022-05-13Remove duplicate stdio initializationsMatt Joyce1-4/+0
Removed duplicate stdio initializations from __sinit(). These are already initialized in the _REENT_INIT macro in sys/reent.h. This simplification enables the reduction of _GLOBAL_REENT dependency in a follow-on patch.
2022-04-01newlib: Remove superfluous CHECK_STD_INIT() macroSebastian Huber1-9/+0
This macro is unused or expands to nothing.
2022-04-01Remove unused _cleanup()Matt Joyce1-8/+0
Removed the unused function _cleanup() from findfp.c.
2022-04-01Fix bug introduced in previous patch (44b60f0c)Matt Joyce4-7/+5
This fixes a bug introduced in a previous patch (Commit 44b60f0c: Make __sdidinit unused). Removed intitialization of __cleanup from __smakebuf_r(). All callers of __smakebuf_r() call __sinit() through the_CHECK_INIT macro, thus __cleanup is already initialized. This fix also allows _cleanup_r() to be made static. Changed its name to cleanup_stdio() and removed its declaration from local.h.
2022-03-31newlib: Rename __sfmoreglue() and make it staticSebastian Huber2-5/+3
Rename __sfmoreglue() in sfmoreglue() and make it static. This function is only used by __sfp() in the same translation unit. Remove use of register keyword.
2022-03-31Replace _fwalk() calls with _fwalk_reent()Sebastian Huber4-39/+12
Remove the _fwalk() implementation to avoid duplicated code with _fwalk_reent().
2022-03-30Commit patch from Tobias Burnus <tobias@codesourcery.com>Jeff Johnston2-4/+4
[PATCH] newlib: Only call _fputwc_r if ELIX_LEVEL >= 4 (nano-)vfprintf.c is enabled for ELIX_LEVEL >= 1. When _WIDE_ORIENT is set, its __sprint_r / __sfputs_r functions unconditionally called _fputwc_r which is only in ELEX_LEVEL >= 4. With this commit, the _WIDE support in (nano-)vfprintf.c is disabled for ELEX_LEVEL < 4.
2022-03-16newlib: libc: merge build up a directoryMike Frysinger3-2021/+205
Convert all the libc/ subdir makes into the top-level Makefile. This allows us to build all of libc from the top Makefile without using any recursive make calls. This is faster and avoids the funky lib.a logic where we unpack subdir archives to repack into a single libc.a. The machine override logic is maintained though by way of Makefile include ordering, and source file accumulation in libc_a_SOURCES. There's a few dummy.c files that are no longer necessary since we aren't doing the lib.a accumulating, so punt them. The winsup code has been pulling the internal newlib ssp library out, but that doesn't exist anymore, so change that to pull the objects.
2022-02-28newlib: libc: move stdio multiplex logic from build to source filesMike Frysinger16-502/+249
Rather than define per-object rules in the Makefile, have small files that define & include the right content. This simplifies the build rules, and makes understanding the source a little easier (imo) as it makes all the subdirs behave the same: you have 1 source file and it produces 1 object. It's also about the same amount of boiler plate, without having to define custom build rules that can fall out of sync. Some of these rules were already unnecessary as they were compiling a single source file into the same named object w/out custom flags, and Automake handles that for us completely. This will also be important as we merge the libc.a build into the top dir since it relies on a single flat list of objects for overrides. Also take the opportunity to clean up the unnecessary header deps in here. Automake provides dependency generation for free now.
2022-02-25newlib: libc: move configure into top-levelMike Frysinger1-13/+30
This kills off the last configure script under libc/ and folds it into the top newlib configure script. The a lot of the logic was already in the top configure script, so move what's left into a libc/acinclude.m4 file.