c - May printf (or fprintf or dprintf) return ("successfully") less (but nonnegative) than the number of "all bytes"? -


the manual says that

upon successful return, these functions [printf, dprintf etc.] return number of characters printed.

the manual not mention whethet may number less (but yet nonnegative) length of "final" (substitutions , formattings done) string. nor mentions how check whether (or achieve that) string completely written.

the dprintf function operates on file descriptor. similarily write function, manual does mention that

on success, number of bytes written returned (zero indicates nothing written). not error if number smaller number of bytes requested;

so if want write string have enclose n = write() in while-loop. should have same in case of dprintf or printf?

my understanding of documentation dprintf either fail or output output. agree gray area (and might not understand well); i'm guessing partial output kind of failure (so returns negative size).

here implementation of musl-libc:

in stdio/dprintf.c dprintf function calls vdprintf

but in stdio/vdprintf.c have:

static size_t wrap_write(file *f, const unsigned char *buf, size_t len) {     return __stdio_write(f, buf, len); }  int vdprintf(int fd, const char *restrict fmt, va_list ap) {     file f = {         .fd = fd, .lbf = eof, .write = wrap_write,         .buf = (void *)fmt, .buf_size = 0,         .lock = -1     };     return vfprintf(&f, fmt, ap); } 

so dprintf returning size vfprintf (and fprintf....) does.

however, if concerned, you'll better use snprintf or asprintf output memory buffer, , explicitly use write(2) on buffer.

look stdio/__stdio_write.c implementation of __stdio_write (it uses writev(2) vector of 2 data chunks in loop).

in other words, not care; if need sure every byte has been written expect (for example if file descriptor http socket), suggest buffer explicitly (e.g. calling snprintf and/or asprintf) yourself, use explicit write(2).

ps. might check source code of your particular c standard library providing dprintf; gnu glibc see notably libio/iovdprintf.c


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -