Looking for C source code for snprintf()
Asked Answered
J

3

6

I need to port snprintf() to another platform that does not fully support GLibC.

I am looking for the underlying declaration in the Glibc 2.14 source code. I follow many function calls, but get stuck on vfprintf(). It then seems to call _IO_vfprintf(), but I cannot find the definition. Probably a macro is obfuscating things.

I need to see the real C code that scans the format string and calculates the number of bytes it would write if input buffer was large enough.

I also tried looking in newlib 1.19.0, but I got stuck on _svfprintf_r(). I cannot find the definition anywhere.

Can someone point me to either definition or another one for snprintf()?

Jestinejesting answered 15/6, 2011 at 10:12 Comment(0)
G
4

http://www.ijs.si/software/snprintf/ has what they claim is a portable implementation of snprintf, including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf. Perhaps it can help.

Gouache answered 15/6, 2011 at 10:42 Comment(1)
However good it seems, this implementation does NOT support any float/double formatting, so it is far from complete.Breastbone
A
6

I've spent quite a while digging the sources to find _svfprintf_r() (and friends) definitions in the Newlib. Since OP asked about it, I'll post my finding for the poor souls who need those as well. The following holds true for Newlib 1.20.0, but I guess it is more or less the same across different versions.

The actual sources are located in the vfprintf.c file. There is a macro _VFPRINTF_R set to one of _svfiprintf_r, _vfiprintf_r, _svfprintf_r, or _vfprintf_r (depending on the build options), and then the actual implementation function is defined accordingly:

int
_DEFUN(_VFPRINTF_R, (data, fp, fmt0, ap),
       struct _reent *data _AND
       FILE * fp           _AND
       _CONST char *fmt0   _AND
       va_list ap)
{
    ...
Anesthesia answered 1/3, 2013 at 11:49 Comment(0)
G
4

http://www.ijs.si/software/snprintf/ has what they claim is a portable implementation of snprintf, including vsnprintf.c, asnprintf, vasnprintf, asprintf, vasprintf. Perhaps it can help.

Gouache answered 15/6, 2011 at 10:42 Comment(1)
However good it seems, this implementation does NOT support any float/double formatting, so it is far from complete.Breastbone
M
4

The source code of the GNU C library (glibc) is hosted on sourceware.org.

Here is a link to the implementation of vfprintf(), which is called by snprintf(): https://sourceware.org/git/?p=glibc.git;a=blob;f=stdio-common/vfprintf-internal.c

Minnieminnnie answered 28/3, 2016 at 14:17 Comment(6)
I checked the URL you provided. I see: #ifndef COMPILE_WPRINTF ... # define vfprintf _IO_vfprintf_internal, but where is _IO_vfprintf_internal defined? Let me know if I misunderstand.Jestinejesting
The definition of vfprintf() starts at line 1235.Minnieminnnie
vfprintf() now starts at line 1244.Quittance
It JUMPs a lot!Monomania
The implementation seems to be hidden. Not surprised.Bootery
@steve, it's not hidden. It has been moved to vfprintf-internal.c. I'll update the link.Minnieminnnie

© 2022 - 2024 — McMap. All rights reserved.