1 /* vi: set sw=4 ts=4: */
3 * *printf implementations for busybox
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* Mar 12, 2003 Manuel Novoa III
12 * While fwrite(), fputc(), fputs(), etc. all set the stream error flag
13 * on failure, the *printf functions are unique in that they can fail
14 * for reasons not related to the actual output itself. Among the possible
15 * reasons for failure which don't set the streams error indicator,
16 * SUSv3 lists EILSEQ, EINVAL, and ENOMEM.
18 * In some cases, it would be desirable to have a group of *printf()
19 * functions available that _always_ set the stream error indicator on
20 * failure. That would allow us to defer error checking until applet
21 * exit. Unfortunately, there is no standard way of setting a streams
22 * error indicator... even though we can clear it with clearerr().
25 /* Mar 22, 2006 Rich Felker III
27 * Actually there is a portable way to set the error indicator. See below.
28 * It is not thread-safe as written due to a race condition with file
29 * descriptors but since BB is not threaded that does not matter. It can be
30 * made thread-safe at the expense of slightly more code, if this is ever
31 * needed in the future.
41 int bb_vfprintf(FILE * __restrict stream,
42 const char * __restrict format,
47 if ((rv = vfprintf(stream, format, arg)) < 0) {
48 /* The following sequence portably sets the error flag for
49 * stream on any remotely POSIX-compliant implementation. */
51 int errno_save = errno;
52 int fd = fileno(stream);
57 /* Force an attempted write to nonexistant fd => EBADF */
60 /* Restore the stream's original fd */
71 int bb_vprintf(const char * __restrict format, va_list arg)
73 return bb_vfprintf(stdout, format, arg);
78 int bb_fprintf(FILE * __restrict stream,
79 const char * __restrict format, ...)
84 va_start(arg, format);
85 rv = bb_vfprintf(stream, format, arg);
93 int bb_printf(const char * __restrict format, ...)
98 va_start(arg, format);
99 rv = bb_vfprintf(stdout, format, arg);