summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/vfprintf.c
Commit message (Collapse)AuthorAgeFilesLines
* Implement printf(3) family %m format string extension.Konstantin Belousov2018-05-221-0/+7
| | | | | | | | | Reviewed by: ed, dim (code only) Sponsored by: Mellanox Technologies MFC after: 1 week Notes: svn path=/head/; revision=334031
* General further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-201-0/+2
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 3-Clause license. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Special thanks to Wind River for providing access to "The Duke of Highlander" tool: an older (2014) run over FreeBSD tree was useful as a starting point. Notes: svn path=/head/; revision=326025
* Make stdio deferred cancel-safe.Konstantin Belousov2017-06-291-2/+2
| | | | | | | | | | | | | | | | | | | | | | | If used with fopen(3)/fdopen(3)-ed FILEs, stdio accurately uses non-cancellable internal versions of the functions, i.e. it seems to be fine with regard to cancellation. But if the funopen(3) and f{r,w}open(3) functions were used to open the FILE, and corresponding user functions create cancellation points (they typically have no other choice), then stdio code at least leaks FILE' lock. The change installs cleanup handler which unlocks FILE. Some minimal restructuring of the code was required to make it use common return place to satisfy hand-rolled pthread_cleanup_pop() requirements. Noted by: eugen Reviewed by: eugen, vangyzen Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D11246 Notes: svn path=/head/; revision=320472
* Revert r310138Enji Cooper2016-12-221-31/+0
| | | | | | | | | | | | | | | Adding %b support to vfprintf for parity with kernel space requires more discussion/review. In particular, many parties were concerned over introducing a non-standard format qualifier to *printf(3) which didn't already exist in other OSes, e.g. Linux, thus making code which used %b harder to port to other operating systems. Requested by: many Notes: svn path=/head/; revision=310433
* vfprintf(3): Add support for kernel %b formatConrad Meyer2016-12-161-0/+31
| | | | | | | | | | | | | | | This is a direct port of the kernel %b format. I'm unclear on if (more) non-portable printf extensions will be a problem. I think it's desirable to have userspace formats include all kernel formats, but there may be competing goals I'm not aware of. Reviewed by: no one, unfortunately Sponsored by: Dell EMC Isilon Differential Revision: https://reviews.freebsd.org/D8426 Notes: svn path=/head/; revision=310138
* Don't check for __SERR which may stick from one of any previous stdioAndrey A. Chernov2016-08-251-0/+6
| | | | | | | | | | functions. __SERR is for user and the rest of stdio code do not check it for error sensing internally, only set it. In vf(w)printf.c here it is more easy to save __SERR, clear and restore it. Notes: svn path=/head/; revision=304810
* Adjust errno on failed prepwrite.Pedro F. Giffuni2014-07-201-1/+3
| | | | | | | | | Obtained from: Apple Inc. (Libc 997.90.3) Phabric: D442 MFC after: 1 week Notes: svn path=/head/; revision=268930
* Renumber clauses to reduce diffs to other versionsEd Maste2013-04-231-1/+1
| | | | | | | | | | NetBSD, OpenBSD, and Android's Bionic all number the clauses 1 through 3, so follow suit to make comparison easier. Acked-by: imp@ Notes: svn path=/head/; revision=249808
* If the size passed to {,v}s{w,n}printf is larger than INT_MAX+1David Schultz2012-04-211-0/+3
| | | | | | | | | | | | | | | | | | | | (i.e., the return value would overflow), set errno to EOVERFLOW and return an error. This improves the chances that buggy applications -- for instance, ones that pass in a negative integer as the size due to a bogus calculation -- will fail in safe ways. Returning an error in these situations is specified by POSIX, but POSIX appears to have an off-by-one error that isn't duplicated in this change. Previously, some of these functions would silently cap the size at INT_MAX+1, and others would exit with an error after writing more than INT_MAX characters. PR: 39256 MFC after: 2 weeks Notes: svn path=/head/; revision=234531
* Implement xlocale APIs from Darwin, mainly for use by libc++. This adds aDavid Chisnall2011-11-201-25/+37
| | | | | | | | | | | | | | | load of _l suffixed versions of various standard library functions that use the global locale, making them take an explicit locale parameter. Also adds support for per-thread locales. This work was funded by the FreeBSD Foundation. Please test any code you have that uses the C standard locale functions! Reviewed by: das (gdtoa changes) Approved by: dim (mentor) Notes: svn path=/head/; revision=227753
* - Use an initializer macro to initialize fields in 'fake' FILE objects usedJohn Baldwin2010-03-111-1/+1
| | | | | | | | | | | | | | | by *sprintf(), etc. - Explicitly initialize _fl_mutex to PTHREAD_MUTEX_INITIALIZER for all FILE objects. This is currently a nop on FreeBSD, but is import for other platforms (or in the future) where PTHREAD_MUTEX_INITIALIZER is not simply zero. PR: threads/141198 Reported by: Jeremy Huddleston @ Apple MFC after: 2 weeks Notes: svn path=/head/; revision=205021
* Replace a dozen lines of code with a call to strnlen() / wcsnlen().David Schultz2009-02-281-16/+1
| | | | Notes: svn path=/head/; revision=189138
* Make sure %zd treats negative arguments properly on 32-bit platforms.David Schultz2009-02-281-1/+1
| | | | | | | | | | Fix harmless but related bugs in %_$zd and %_$tu. PR: 131880 MFC after: 1 week Notes: svn path=/head/; revision=189131
* Add support for multibyte thousands_sep encodings, e.g., U+066C.David Schultz2009-01-221-61/+104
| | | | | | | | | | | The integer thousands' separator code is rewritten in order to avoid having to preallocate a buffer for the largest possible digit string with the most possible instances of the longest possible multibyte thousands' separator. The new version inserts thousands' separators for integers using the same code as floating point. Notes: svn path=/head/; revision=187582
* Add support for multibyte decimal_point encodings, e.g., U+066B.David Schultz2009-01-191-7/+9
| | | | Notes: svn path=/head/; revision=187421
* When f[w]printf() is called on an unbuffered file like stdout, itDavid Schultz2009-01-171-7/+12
| | | | | | | | | | | | | | | | | | | sets up a fake buffered FILE and then effectively calls itself recursively. Unfortunately, gcc doesn't know how to do tail call elimination in this case, and actually makes things worse by inlining __sbprintf(). This means that f[w]printf() to stderr was allocating about 5k of stack on 64-bit platforms, much of which was never used. I've reorganized things to eliminate the waste. In addition to saving some stack space, this improves performance in my tests by anywhere from 5% to 17% (depending on the test) when -fstack-protector is enabled. I found no statistically significant performance difference when stack protection is turned off. (The tests redirected stderr to /dev/null.) Notes: svn path=/head/; revision=187369
* Reduce code duplication by moving functions that are identical in bothDavid Schultz2009-01-151-213/+0
| | | | | | | | vfprintf.c and vfwprintf.c (except for char/wchar_t differences) to a common header file. Notes: svn path=/head/; revision=187284
* Convert the insidious macros that handle printf()'s buffering intoDavid Schultz2009-01-151-48/+16
| | | | | | | | | | | | | | slightly less evil inline functions, and move the buffering state into a struct. This will make it possible for helper routines to produce output for printf() directly, making it possible to untangle the code somewhat. In wprintf(), use the same buffering mechanism to reduce diffs to printf(). This has the side-effect of causing wprintf() to catch write errors that it previously ignored. Notes: svn path=/head/; revision=187277
* Consolidate some variable initializations. No substantive change.David Schultz2008-12-111-7/+7
| | | | Notes: svn path=/head/; revision=185904
* Move the xprintf hook to where it belongs; it shouldn't be in theDavid Schultz2008-12-101-6/+4
| | | | | | | middle of vfprintf's variable declarations. Notes: svn path=/head/; revision=185825
* Correctly handle malloc() failure. While here, reduce the code size aDavid Schultz2008-06-291-3/+9
| | | | | | | bit by removing some calls to the inline function addtype(). Notes: svn path=/head/; revision=180106
* Reduce the level of duplication between vfprintf() and vfwprintf()David Schultz2008-06-291-486/+1
| | | | | | | | | by moving the positional argument handling code to a new file, printf-pos.c, and moving common definitions to printflocal.h. No functional change intended. Notes: svn path=/head/; revision=180104
* Begin de-spaghettifying the code that handles positional arguments.David Schultz2008-06-291-94/+153
| | | | | | | | | | | | | | | | | In particular, encapsulate the state of the type table in a struct, and add inline functions to initialize, free, and manipulate that state. This replaces some ugly macros that made proper error handling impossible. While here, remove an unneeded test for NULL and a variable that is initialized (many times!) but never used. The compiler didn't catch these because of rampant use of the same variable to mean different things in different places. This commit should not cause any changes in functionality. Notes: svn path=/head/; revision=180102
* Next stage of stdio cleanup: Retire __sFILEX and merge the fields back intoJohn Baldwin2008-04-171-1/+2
| | | | | | | | | | | | | | | | | | | | | | __sFILE. This was supposed to be done in 6.0. Some notes: - Where possible I restored the various lines to their pre-__sFILEX state. - Retire INITEXTRA() and just initialize the wchar bits (orientation and mbstate) explicitly instead. The various places that used INITEXTRA didn't need the locking fields or _up initialized. (Some places needed _up to exist and not be off the end of a NULL or garbage pointer, but they didn't require it to be initialized to a specific value.) - For now, stdio.h "knows" that pthread_t is a 'struct pthread *' to avoid namespace pollution of including all the pthread types in stdio.h. Once we remove all the inlines and make __sFILE private it can go back to using pthread_t, etc. - This does not remove any of the inlines currently and does not change any of the public ABI of 'FILE'. MFC after: 1 month Reviewed by: peter Notes: svn path=/head/; revision=178287
* We should never zero-pad INF or NaN (yielding silly strings like "00inf")David Schultz2007-05-081-0/+1
| | | | | | | even if the programmer asks for zero padding. Notes: svn path=/head/; revision=169355
* Per Regents of the University of Calfornia letter, remove advertisingWarner Losh2007-01-091-4/+0
| | | | | | | | | clause. # If I've done so improperly on a file, please let me know. Notes: svn path=/head/; revision=165903
* Fix rounding of 0xf for hex fp formats.David Schultz2007-01-031-2/+2
| | | | | | | PR: 90333 Notes: svn path=/head/; revision=165742
* Use correct type in va_arg argument.Alexander Kabaev2006-09-211-1/+1
| | | | Notes: svn path=/head/; revision=162523
* Add missing #if's for NO_FLOATING_POINTPoul-Henning Kamp2006-04-011-2/+4
| | | | Notes: svn path=/head/; revision=157381
* Add an extensible version of our *printf(3) implementation to libcPoul-Henning Kamp2005-12-161-0/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | on probationary terms: it may go away again if it transpires it is a bad idea. This extensible printf version will only be used if either environment variable USE_XPRINTF is defined or one of the extension functions are called. or the global variable __use_xprintf is set greater than zero. In all other cases our traditional printf implementation will be used. The extensible version is slower than the default printf, mostly because less opportunity for combining I/O operation exists when faced with extensions. The default printf on the other hand is a bad case of spaghetti code. The extension API has a GLIBC compatible part and a FreeBSD version of same. The FreeBSD version exists because the GLIBC version may run afoul of our FILE * locking in multithreaded programs and it even further eliminate the opportunities for combining I/O operations. Include three demo extensions which can be enabled if desired: time (%T), hexdump (%H) and strvis (%V). %T can format time_t (%T), struct timeval (%lT) and struct timespec (%llT) in one of two human readable duration formats: "%.3llT" -> "20349.245" "%#.3llT" -> "5h39m9.245" %H will hexdump a sequence of bytes and takes a pointer and a length argument. The width specifies number of bytes per line. "%4H" -> "65 72 20 65" "%+4H" -> "0000 65 72 20 65" "%#4H" -> "65 72 20 65 |er e|" "%+#4H" -> "0000 65 72 20 65 |er e|" %V will dump a string in strvis format. "%V" -> "Hello\tWor\377ld" (C-style) "%0V" -> "Hello\011Wor\377ld" (octal) "%+V" -> "Hello%09Wor%FFld" (http-style) Tests, comments, bugreports etc are most welcome. Notes: svn path=/head/; revision=153486
* /* You're not supposed to hit this problem */Poul-Henning Kamp2005-12-131-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | For some denormalized long double values, a bug in __hldtoa() (called from *printf()'s %A format) results in a base 16 digit being rounded up from 0xf to 0x10. When this digit is subsequently converted to string format, an index of 10 reaches past the end of the uppper-case hex/char array, picking up whatever the code segment happen to contain at that address. This mostly seem to be some character from the upper half of the byte range. When using the %a format instead of %A, the first character past the end of the lowercase hex/char table happens to be index 0 in the uppercase hex/char table hextable and therefore the string representation features a '0', which is supposedly correct. This leads me to belive that the proper fix _may_ be as simple as masking all but the lower four bits off after incrementing a hex-digit in libc/gdtoa/_hdtoa.c:roundup(). I worry however that the upper bit in 0x10 indicates a carry not carried. Until das@ or bde@ finds time to visit this issue, extend the hexdigit arrays with a 17th index containing '?' so that we get a invalid but consistent and printable output in both %a and %A formats whenever this bug strikes. This unmasks the bug in the %a format therefore solving the real issue may both become easier and more urgent. Possibly related to: PR 85080 With help by: bde@ Notes: svn path=/head/; revision=153375
* Speed up __wcsconv() (and hence the printf() %ls format):Tim J. Robbins2005-07-241-34/+30
| | | | | | | | - use wcsrtombs() instead of a wcrtomb() loop where possible. - avoid wcrtomb() loop when output precision is small. Notes: svn path=/head/; revision=148363
* Be bug-for-bug compatible with the C standard with respect toDavid Schultz2005-04-161-2/+8
| | | | | | | printf("%#.0o", 0). Cite an amusing passage from a defect report. Notes: svn path=/head/; revision=145172
* Don't forget to va_end() the va_list we get from va_copy().Dag-Erling Smørgrav2004-08-261-0/+1
| | | | | | | | Submitted by: Sean McNeil <sean@mcneil.com> MFC after: 3 days Notes: svn path=/head/; revision=134332
* The third operand of the conditional operator should have type void too.Stefan Farfeleder2004-06-081-1/+1
| | | | | | | Approved by: das (mentor) Notes: svn path=/head/; revision=130242
* Rename cantwrite() to prepwrite(). The latter is less confusing,David Schultz2004-06-081-1/+1
| | | | | | | since the macro isn't really a predicate, and it has side-effects. Notes: svn path=/head/; revision=130231
* - To make it easier to compile *printf() and *scanf() withoutDavid Schultz2004-05-021-24/+16
| | | | | | | | | | | | floating-point support, remove default definition of FLOATING_POINT from the source, and change the compile-time option to NO_FLOATING_POINT. - Remove the HEXFLOAT option. It saves an insignificant amount of space (<0.1% of the size of libc on i386) and complicates vfprintf() and checkfmt(). Notes: svn path=/head/; revision=128819
* Use the correct size to allocate, copy and clear argument type tablesTim J. Robbins2004-04-221-6/+9
| | | | | | | | | | | | | after their change from an array of char to an array of enum. This fixes problems that occurred when using positional arguments in format strings, particularly with more than STATIC_ARG_TBL_SIZE (8) of them. PR: 65841 Submitted by: Steven Smith (mostly) Notes: svn path=/head/; revision=128550
* Prepare to handle trivial state-dependent encodings. Full support forTim J. Robbins2004-04-071-4/+12
| | | | | | | | state-dependent encodings with locking shifts will come later if there is demand for it. Notes: svn path=/head/; revision=128002
* Implement __hdtoa() and __hldtoa() and enable printf() support for %aDavid Schultz2004-01-181-5/+2
| | | | | | | and %A, which print floating-point numbers in hexadecimal. Notes: svn path=/head/; revision=124667
* Fix some bugs affecting the %a and %A format specifiers. SinceDavid Schultz2004-01-181-6/+16
| | | | | | | | | | | | | | | | these are not fully implemented and ifdef'd out, the bugs have never manifested themselves. Specifically: - Fix a memory leak in the case where %a follows another floating-point format. - Make the %a/%A code behave like %e/%E with respect to precision. - It is no longer valid to assume that '-' and '0x' are mutually exclusive. - Address other minor issues. Notes: svn path=/head/; revision=124657
* Pass NULL instead of a pointer to a zeroed mbstate_t object.Tim J. Robbins2003-11-051-9/+4
| | | | Notes: svn path=/head/; revision=122105
* %E-like %g and %G conversions should remove trailing zeroes unlessDavid Schultz2003-04-191-0/+7
| | | | | | | | | | | the # flag is present. Implement this behavior and add a comment describing it. Noticed by: Enache Adrian <enache@rdslink.ro> Pointy hat to: das Notes: svn path=/head/; revision=113723
* Fix a bug where printf was erroneously printing a decimal point forDavid Schultz2003-04-141-8/+10
| | | | | | | | | | | | %f and sufficiently short %g specifiers where the precision was explicitly zero, no '#' flag was specified, and the floating point argument was > 0 and <= 0.5. While at it, add some comments to better explain the relevant bits of code. Noticed by: Christoph Kukulies <kuku@physik.rwth-aachen.de> Notes: svn path=/head/; revision=113468
* __wcsconv(): free(convbuf) before returning NULLAndrey A. Chernov2003-04-071-2/+4
| | | | Notes: svn path=/head/; revision=113196
* Today just isn't my day. Remove some old commented out code that snuckDavid Schultz2003-04-071-1/+1
| | | | | | | | | into the last commit. Noticed by: mike Notes: svn path=/head/; revision=113194
* - %e conversions with precision 0 should not cause a decimal point toDavid Schultz2003-04-071-18/+22
| | | | | | | | | | | be printed. - Fix %f conversions where the number of significant digits is < expt. This would be a one-line change were it not for thousands separators. Noticed by tjr. - Remove some unnecessary code in the parsing of precision specifiers. Notes: svn path=/head/; revision=113191
* Rework the floating point code in printf(). Significant changes:David Schultz2003-04-051-151/+189
| | | | | | | | | | | | | | | | | | | | | - We used to round long double arguments to double. Now we print them properly. - Bugs involving '%F', corner cases of '#' and 'g' format specifiers, and the '.*' precision specifier have been fixed. - Added support for the "'" specifier to print thousands' grouping characters in a locale-dependent manner. - Implement the __vfprintf() side of hexadecimal floating point support. All that is still needed is a routine to convert the mantissa to hex digits one nibble at a time in the style of ultoa(). Reviewed by: silence on standards@ Notes: svn path=/head/; revision=113146
* Correct some buffer sizes.David Schultz2003-04-051-10/+16
| | | | | | | | | | | | | - __vfprintf()'s 'buf' has never been used for floating point, so don't define it in terms of (incorrect) constants describing floating point numbers. The actual size needed depends on sizeof(uintmax_t) and locale details, so I slightly overestimated. - We don't need a 308-character buffer to store the string "308". With long doubles and %a we need more than three characters, though. Notes: svn path=/head/; revision=113142
* Kludge around a bug that results from printf() assuming thatDavid Schultz2003-03-141-1/+1
| | | | | | | | | | | | dtoa() is buggy. The bug would cause incorrect output to be generated when format strings such as '%5.0f' were used with nonzero numbers whose magnitude is less than 1. Reported by: df(1) by way of periodic(8) Reviewed by: mike Notes: svn path=/head/; revision=112224