1 /* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
2  *
3  * GNU Library General Public License (LGPL) version 2 or later.
4  *
5  * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
6  */
7 
8 #include <features.h>
9 
10 #ifdef __USE_GNU
11 #include "_stdio.h"
12 #include <stdarg.h>
13 
14 
vdprintf(int filedes,const char * __restrict format,va_list arg)15 int vdprintf(int filedes, const char * __restrict format, va_list arg)
16 {
17 	FILE f;
18 	int rv;
19 #ifdef __STDIO_BUFFERS
20 	char buf[64];				/* TODO: provide _optional_ buffering? */
21 
22 	f.__bufend = (unsigned char *) buf + sizeof(buf);
23 	f.__bufstart = (unsigned char *) buf;
24 	__STDIO_STREAM_DISABLE_GETC(&f);
25 	__STDIO_STREAM_DISABLE_PUTC(&f);
26 	__STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
27 #endif
28 
29 /* 	__STDIO_STREAM_RESET_GCS(&f); */
30 #ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
31 	f.__cookie = &(f.__filedes);
32 	f.__gcs.read = NULL;
33 	f.__gcs.write = _cs_write;
34 	f.__gcs.seek = NULL;
35 	f.__gcs.close = NULL;
36 #endif
37 
38 	f.__filedes = filedes;
39 	f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);
40 
41 #ifdef __UCLIBC_HAS_WCHAR__
42 	f.__ungot_width[0] = 0;
43 #endif
44 #ifdef __STDIO_MBSTATE
45 	__INIT_MBSTATE(&(f.__state));
46 #endif
47 
48 /* _vfprintf_internal doesn't do any locking, locking init is here
49  * only because of fflush_unlocked. TODO? */
50 #if (defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__)) && defined(__UCLIBC_HAS_THREADS__)
51 	f.__user_locking = 1;		/* Set user locking. */
52 	STDIO_INIT_MUTEX(f.__lock);
53 #endif
54 	f.__nextopen = NULL;
55 
56 #ifdef __USE_OLD_VFPRINTF__
57 	rv = vfprintf(&f, format, arg);
58 #else
59 	rv = _vfprintf_internal(&f, format, arg);
60 #endif
61 
62 #ifdef __STDIO_BUFFERS
63 	/* If not buffering, then fflush is unnecessary. */
64 	if ((rv > 0) && fflush_unlocked(&f)) {
65 		rv = -1;
66 	}
67 #endif
68 
69 	assert(rv >= -1);
70 
71 	return rv;
72 }
73 libc_hidden_def(vdprintf)
74 #endif
75