1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2005,2008 Oracle.  All rights reserved.
5 *
6 * $Id: printf.c,v 1.8 2008/01/08 20:58:08 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13/*
14 * printf --
15 *
16 * PUBLIC: #ifndef HAVE_PRINTF
17 * PUBLIC: int printf __P((const char *, ...));
18 * PUBLIC: #endif
19 */
20#ifndef HAVE_PRINTF
21int
22#ifdef STDC_HEADERS
23printf(const char *fmt, ...)
24#else
25printf(fmt, va_alist)
26	const char *fmt;
27	va_dcl
28#endif
29{
30	va_list ap;
31	size_t len;
32	char buf[2048];    /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */
33
34#ifdef STDC_HEADERS
35	va_start(ap, fmt);
36#else
37	va_start(ap);
38#endif
39	len = (size_t)vsnprintf(buf, sizeof(buf), fmt, ap);
40#ifdef HAVE_BREW
41	/*
42	 * The BREW vsnprintf function return count includes the trailing
43	 * nul-termination character.
44	 */
45	if (len > 0 && len <= sizeof(buf) && buf[len - 1] == '\0')
46		--len;
47#endif
48
49	va_end(ap);
50
51	/*
52	 * We implement printf/fprintf with fwrite, because Berkeley DB uses
53	 * fwrite in other places.
54	 */
55	return (fwrite(
56	    buf, sizeof(char), (size_t)len, stdout) == len ? (int)len: -1);
57}
58#endif /* HAVE_PRINTF */
59
60/*
61 * fprintf --
62 *
63 * PUBLIC: #ifndef HAVE_PRINTF
64 * PUBLIC: int fprintf __P((FILE *, const char *, ...));
65 * PUBLIC: #endif
66 */
67#ifndef HAVE_PRINTF
68int
69#ifdef STDC_HEADERS
70fprintf(FILE *fp, const char *fmt, ...)
71#else
72fprintf(fp, fmt, va_alist)
73	FILE *fp;
74	const char *fmt;
75	va_dcl
76#endif
77{
78	va_list ap;
79	size_t len;
80	char buf[2048];    /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */
81
82#ifdef STDC_HEADERS
83	va_start(ap, fmt);
84#else
85	va_start(ap);
86#endif
87	len = vsnprintf(buf, sizeof(buf), fmt, ap);
88#ifdef HAVE_BREW
89	/*
90	 * The BREW vsnprintf function return count includes the trailing
91	 * nul-termination character.
92	 */
93	if (len > 0 && len <= sizeof(buf) && buf[len - 1] == '\0')
94		--len;
95#endif
96
97	va_end(ap);
98
99	/*
100	 * We implement printf/fprintf with fwrite, because Berkeley DB uses
101	 * fwrite in other places.
102	 */
103	return (fwrite(
104	    buf, sizeof(char), (size_t)len, fp) == len ? (int)len: -1);
105}
106#endif /* HAVE_PRINTF */
107
108/*
109 * vfprintf --
110 *
111 * PUBLIC: #ifndef HAVE_PRINTF
112 * PUBLIC: int vfprintf __P((FILE *, const char *, va_list));
113 * PUBLIC: #endif
114 */
115#ifndef HAVE_PRINTF
116int
117vfprintf(fp, fmt, ap)
118	FILE *fp;
119	const char *fmt;
120	va_list ap;
121{
122	size_t len;
123	char buf[2048];    /* !!!: END OF THE STACK DON'T TRUST SPRINTF. */
124
125	len = vsnprintf(buf, sizeof(buf), fmt, ap);
126#ifdef HAVE_BREW
127	/*
128	 * The BREW vsnprintf function return count includes the trailing
129	 * nul-termination character.
130	 */
131	if (len > 0 && len <= sizeof(buf) && buf[len - 1] == '\0')
132		--len;
133#endif
134
135	/*
136	 * We implement printf/fprintf with fwrite, because Berkeley DB uses
137	 * fwrite in other places.
138	 */
139	return (fwrite(
140	    buf, sizeof(char), (size_t)len, fp) == len ? (int)len: -1);
141}
142#endif /* HAVE_PRINTF */
143