Re: "printf" bloated, Ok. But how to print then?
Laurent Bercot <[email protected]>
| Newsgroups | gmane.linux.lib.dietlibc |
|---|---|
| Message-ID | <[email protected]> |
> One question I couldn't yet fix is how to print to stdout/stderr?
write() does the job pretty well. :)
If you need more high-level I/O, you could for instance use the
excellent "buffer" interface, created by DJB and implemented for
instance in libowfat (http://www.fefe.de/libowfat/) or skalibs
(http://www.skarnet.org/software/skalibs/).
Shameless plug: the skalibs "buffer" implementation supports
non-blocking I/O.
> As the FAQ stated, "printf" and the like (fprintf ...) are bloated and
> dietlibc complains and stops compiling when seeing them.
> So, how then can I print a formatted data then?
The bloat comes from dynamically interpreting a format string.
So, the key is to not use a format string. Instead, separately format
all your arguments yourself, in order to make a string you will then
feed to stdout. The fmt_ (or _fmt) functions, found - again - in
libowfat or skalibs, can help you do this, and they're lighter than
printf (which has to include the code for *every* format function
and more).
Here's a rewrite of "printf("The result is: %u\n", result);" using
skalibs:
{
char fmt[UINT_FMT] ;
buffer_puts(buffer_1, "The result is: ") ;
buffer_putalign(buffer_1, fmt, uint_fmt(fmt, result)) ;
buffer_putflush(buffer_1, "\n", 1) ;
}
It takes longer to write, but the resulting binary is much smaller.
(And much cleaner than if you use stdio.)
--
Laurent