summaryrefslogtreecommitdiff
path: root/libc/stdio/vdprintf.c
blob: 6e28b14c0f6aab70226725a981913d658f283b9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Copyright (C) 2004       Manuel Novoa III    <mjn3@codepoet.org>
 *
 * GNU Library General Public License (LGPL) version 2 or later.
 *
 * Dedicated to Toni.  See uClibc/DEDICATION.mjn3 for details.
 */

#include <features.h>

#ifdef __USE_GNU
#include "_stdio.h"
#include <stdarg.h>

#ifdef __USE_OLD_VFPRINTF__
libc_hidden_proto(vfprintf)
#endif
libc_hidden_proto(fflush_unlocked)

libc_hidden_proto(vdprintf)
int vdprintf(int filedes, const char * __restrict format, va_list arg)
{
	FILE f;
	int rv;
#ifdef __STDIO_BUFFERS
	char buf[64];				/* TODO: provide _optional_ buffering? */

	f.__bufend = (unsigned char *) buf + sizeof(buf);
	f.__bufstart = (unsigned char *) buf;
	__STDIO_STREAM_DISABLE_GETC(&f);
	__STDIO_STREAM_DISABLE_PUTC(&f);
	__STDIO_STREAM_INIT_BUFREAD_BUFPOS(&f);
#endif

/* 	__STDIO_STREAM_RESET_GCS(&f); */
#ifdef __UCLIBC_HAS_GLIBC_CUSTOM_STREAMS__
	f.__cookie = &(f.__filedes);
	f.__gcs.read = NULL;
	f.__gcs.write = _cs_write;
	f.__gcs.seek = NULL;
	f.__gcs.close = NULL;
#endif

	f.__filedes = filedes;
	f.__modeflags = (__FLAG_NARROW|__FLAG_WRITEONLY|__FLAG_WRITING);

#ifdef __UCLIBC_HAS_WCHAR__
	f.__ungot_width[0] = 0;
#endif /* __UCLIBC_HAS_WCHAR__ */
#ifdef __STDIO_MBSTATE
	__INIT_MBSTATE(&(f.__state));
#endif /* __STDIO_MBSTATE */

/* _vfprintf_internal doesn't do any locking, locking init is here
 * only because of fflush_unlocked. TODO? */
#if (defined(__STDIO_BUFFERS) || defined(__USE_OLD_VFPRINTF__)) && defined(__UCLIBC_HAS_THREADS__)
	f.__user_locking = 1;		/* Set user locking. */
	__stdio_init_mutex(&f.__lock);
#endif
	f.__nextopen = NULL;

#ifdef __USE_OLD_VFPRINTF__
	rv = vfprintf(&f, format, arg);
#else
	rv = _vfprintf_internal(&f, format, arg);
#endif

#ifdef __STDIO_BUFFERS
	/* If not buffering, then fflush is unnecessary. */
	if ((rv > 0) && fflush_unlocked(&f)) {
		rv = -1;
	}
#endif

	assert(rv >= -1);

	return rv;
}
libc_hidden_def(vdprintf)
#endif