xref: /illumos-gate/usr/src/lib/libc/port/print/vdprintf.c (revision 34b5c75ae1f95c7b8ef561f3c7586e03a21f59e7)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2025 Hans Rosenfeld
14  */
15 
16 #include <stdarg.h>
17 #include <sys/types.h>
18 #include "print.h"
19 
20 int
vdprintf(int fildes,const char * format,va_list ap)21 vdprintf(int fildes, const char *format, va_list ap)
22 {
23 	FILE *file;
24 	int count;
25 	int ret;
26 
27 	file = fdopen(fildes, "w");
28 	if (file == NULL)
29 		return (EOF);
30 
31 	/*
32 	 * Make the FILE unbuffered, avoiding all kinds of headaches associated
33 	 * with buffering and recovering from potential late failure of delayed
34 	 * writes.
35 	 */
36 	(void) setvbuf(file, NULL, _IONBF, 0);
37 
38 	/*
39 	 * As this FILE is temporary and exists only for the runtime of this
40 	 * function, there should be no need for locking.
41 	 */
42 	SET_IONOLOCK(file);
43 
44 	count = vfprintf(file, format, ap);
45 
46 	(void) fdclose(file, NULL);
47 
48 	return (count);
49 }
50 
51 int
dprintf(int fildes,const char * format,...)52 dprintf(int fildes, const char *format, ...)
53 {
54 	int count;
55 	va_list ap;
56 
57 	va_start(ap, format);
58 	count = vdprintf(fildes, format, ap);
59 	va_end(ap);
60 
61 	return (count);
62 }
63