summaryrefslogtreecommitdiff
path: root/src/clib/printf.c
blob: f36eeb15f2e5d3412f1676a58e297b7485e4054c (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2005, 2015 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"

/*
 * printf --
 *
 * PUBLIC: #ifndef HAVE_PRINTF
 * PUBLIC: int printf __P((const char *, ...));
 * PUBLIC: #endif
 */
#ifndef HAVE_PRINTF
int
#ifdef STDC_HEADERS
printf(const char *fmt, ...)
#else
printf(fmt, va_alist)
	const char *fmt;
	va_dcl
#endif
{
	va_list ap;
	size_t len;
	char buf[2048];    /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */

#ifdef STDC_HEADERS
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	len = (size_t)vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	/*
	 * We implement printf/fprintf with fwrite, because Berkeley DB uses
	 * fwrite in other places.
	 */
	return (fwrite(
	    buf, sizeof(char), (size_t)len, stdout) == len ? (int)len: -1);
}
#endif /* HAVE_PRINTF */

/*
 * fprintf --
 *
 * PUBLIC: #ifndef HAVE_PRINTF
 * PUBLIC: int fprintf __P((FILE *, const char *, ...));
 * PUBLIC: #endif
 */
#ifndef HAVE_PRINTF
int
#ifdef STDC_HEADERS
fprintf(FILE *fp, const char *fmt, ...)
#else
fprintf(fp, fmt, va_alist)
	FILE *fp;
	const char *fmt;
	va_dcl
#endif
{
	va_list ap;
	size_t len;
	char buf[2048];    /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */

#ifdef STDC_HEADERS
	va_start(ap, fmt);
#else
	va_start(ap);
#endif
	len = vsnprintf(buf, sizeof(buf), fmt, ap);
	va_end(ap);

	/*
	 * We implement printf/fprintf with fwrite, because Berkeley DB uses
	 * fwrite in other places.
	 */
	return (fwrite(
	    buf, sizeof(char), (size_t)len, fp) == len ? (int)len: -1);
}
#endif /* HAVE_PRINTF */

/*
 * vfprintf --
 *
 * PUBLIC: #ifndef HAVE_PRINTF
 * PUBLIC: int vfprintf __P((FILE *, const char *, va_list));
 * PUBLIC: #endif
 */
#ifndef HAVE_PRINTF
int
vfprintf(fp, fmt, ap)
	FILE *fp;
	const char *fmt;
	va_list ap;
{
	size_t len;
	char buf[2048];    /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */

	len = vsnprintf(buf, sizeof(buf), fmt, ap);

	/*
	 * We implement printf/fprintf with fwrite, because Berkeley DB uses
	 * fwrite in other places.
	 */
	return (fwrite(
	    buf, sizeof(char), (size_t)len, fp) == len ? (int)len: -1);
}
#endif /* HAVE_PRINTF */