]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/io/fprintf.h: fprinte(): Add macro
authorAlejandro Colomar <alx@kernel.org>
Wed, 23 Jul 2025 12:19:52 +0000 (14:19 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 17 Jul 2026 13:48:04 +0000 (08:48 -0500)
fprinte() is like fprintec(), but uses errno instead of an error code.

It is implemented as a macro so that it can read the value in errno
before evaluating any of its arguments, thus not corrupting it.

It also preserves errno so that the error can be printed more than once.
This is useful because we often print to stderr or log_get_logfd(), and
immediately after print to the system log with syslog(3).

Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/io/fprintf.h

index 356d72151223c047bcaf1c1ac2d92045490abffb..3848d7c84e3ee9c127cc4c619ffe609000d832ca 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <string.h>
 #include "attr.h"
 
 
+// fprinte - FILE* print errno
+#define fprinte(stream, ...)  do                                      \
+{                                                                     \
+       int  e__;                                                     \
+                                                                     \
+       e__ = errno;                                                  \
+       fprintec(stream, e__, __VA_ARGS__);                           \
+       errno = e__;                                                  \
+} while (0)
+
+
 format_attr(printf, 3, 4)
 inline int fprintec(FILE *restrict stream, int e,
     const char *restrict fmt, ...);