]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
tools/nolibc: add dprintf() and vdprintf()
authorThomas Weißschuh <thomas.weissschuh@linutronix.de>
Fri, 11 Apr 2025 09:00:47 +0000 (11:00 +0200)
committerThomas Weißschuh <linux@weissschuh.net>
Tue, 22 Apr 2025 08:58:22 +0000 (10:58 +0200)
dprintf() and vdprintf() are printf() variants printing directly into a
filedescriptor. As FILE in nolibc is based directly on filedescriptors,
the implementation is trivial.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Acked-by: Willy Tarreau <w@1wt.eu>
tools/include/nolibc/stdio.h

index b32b8b794015276ab6242c2be18f860c095f90a3..262d0da4da9062e0c83b55661b2509f36548cf88 100644 (file)
@@ -351,6 +351,30 @@ int printf(const char *fmt, ...)
        return ret;
 }
 
+static __attribute__((unused, format(printf, 2, 0)))
+int vdprintf(int fd, const char *fmt, va_list args)
+{
+       FILE *stream;
+
+       stream = fdopen(fd, NULL);
+       if (!stream)
+               return -1;
+       /* Technically 'stream' is leaked, but as it's only a wrapper around 'fd' that is fine */
+       return vfprintf(stream, fmt, args);
+}
+
+static __attribute__((unused, format(printf, 2, 3)))
+int dprintf(int fd, const char *fmt, ...)
+{
+       va_list args;
+       int ret;
+
+       va_start(args, fmt);
+       ret = vdprintf(fd, fmt, args);
+       va_end(args);
+       return ret;
+}
+
 static __attribute__((unused))
 int vsscanf(const char *str, const char *format, va_list args)
 {