1/* $OpenLDAP$ */
2/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3 *
4 * Copyright 1998-2011 The OpenLDAP Foundation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
9 * Public License.
10 *
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
14 */
15
16#include "portable.h"
17
18#include <stdio.h>
19#include <ac/stdarg.h>
20#include <ac/string.h>
21#include <ac/ctype.h>
22#include <lutil.h>
23
24#if !defined(HAVE_VSNPRINTF) && !defined(HAVE_EBCDIC)
25/* Write at most n characters to the buffer in str, return the
26 * number of chars written or -1 if the buffer would have been
27 * overflowed.
28 *
29 * This is portable to any POSIX-compliant system. We use pipe()
30 * to create a valid file descriptor, and then fdopen() it to get
31 * a valid FILE pointer. The user's buffer and size are assigned
32 * to the FILE pointer using setvbuf. Then we close the read side
33 * of the pipe to invalidate the descriptor.
34 *
35 * If the write arguments all fit into size n, the write will
36 * return successfully. If the write is too large, the stdio
37 * buffer will need to be flushed to the underlying file descriptor.
38 * The flush will fail because it is attempting to write to a
39 * broken pipe, and the write will be terminated.
40 * -- hyc, 2002-07-19
41 */
42/* This emulation uses vfprintf; on OS/390 we're also emulating
43 * that function so it's more efficient just to have a separate
44 * version of vsnprintf there.
45 */
46#include <ac/signal.h>
47int ber_pvt_vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
48{
49	int fds[2], res;
50	FILE *f;
51	RETSIGTYPE (*sig)();
52
53	if (pipe( fds )) return -1;
54
55	f = fdopen( fds[1], "w" );
56	if ( !f ) {
57		close( fds[1] );
58		close( fds[0] );
59		return -1;
60	}
61	setvbuf( f, str, _IOFBF, n );
62	sig = signal( SIGPIPE, SIG_IGN );
63	close( fds[0] );
64
65	res = vfprintf( f, fmt, ap );
66
67	fclose( f );
68	signal( SIGPIPE, sig );
69	if ( res > 0 && res < n ) {
70		res = vsprintf( str, fmt, ap );
71	}
72	return res;
73}
74#endif
75
76#ifndef HAVE_SNPRINTF
77int ber_pvt_snprintf( char *str, size_t n, const char *fmt, ... )
78{
79	va_list ap;
80	int res;
81
82	va_start( ap, fmt );
83	res = vsnprintf( str, n, fmt, ap );
84	va_end( ap );
85	return res;
86}
87#endif /* !HAVE_SNPRINTF */
88
89#ifdef HAVE_EBCDIC
90/* stdio replacements with ASCII/EBCDIC translation for OS/390.
91 * The OS/390 port depends on the CONVLIT compiler option being
92 * used to force character and string literals to be compiled in
93 * ISO8859-1, and the __LIBASCII cpp symbol to be defined to use the
94 * OS/390 ASCII-compatibility library. This library only supplies
95 * an ASCII version of sprintf, so other needed functions are
96 * provided here.
97 *
98 * All of the internal character manipulation is done in ASCII,
99 * but file I/O is EBCDIC, so we catch any stdio reading/writing
100 * of files here and do the translations.
101 */
102
103#undef fputs
104#undef fgets
105
106char *ber_pvt_fgets( char *s, int n, FILE *fp )
107{
108	s = (char *)fgets( s, n, fp );
109	if ( s ) __etoa( s );
110	return s;
111}
112
113int ber_pvt_fputs( const char *str, FILE *fp )
114{
115	char buf[8192];
116
117	strncpy( buf, str, sizeof(buf) );
118	__atoe( buf );
119	return fputs( buf, fp );
120}
121
122/* The __LIBASCII doesn't include a working vsprintf, so we make do
123 * using just sprintf. This is a very simplistic parser that looks for
124 * format strings and uses sprintf to process them one at a time.
125 * Literal text is just copied straight to the destination.
126 * The result is appended to the destination string. The parser
127 * recognizes field-width specifiers and the 'l' qualifier; it
128 * may need to be extended to recognize other qualifiers but so
129 * far this seems to be enough.
130 */
131int ber_pvt_vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
132{
133	char *ptr, *pct, *s2, *f2, *end;
134	char fm2[64];
135	int len, rem;
136
137	ptr = (char *)fmt;
138	s2 = str;
139	fm2[0] = '%';
140	if (n) {
141		end = str + n;
142	} else {
143		end = NULL;
144	}
145
146	for (pct = strchr(ptr, '%'); pct; pct = strchr(ptr, '%')) {
147		len = pct-ptr;
148		if (end) {
149			rem = end-s2;
150			if (rem < 1) return -1;
151			if (rem < len) len = rem;
152		}
153		s2 = lutil_strncopy( s2, ptr, len );
154		/* Did we cheat the length above? If so, bail out */
155		if (len < pct-ptr) return -1;
156		for (pct++, f2 = fm2+1; isdigit(*pct);) *f2++ = *pct++;
157		if (*pct == 'l') *f2++ = *pct++;
158		if (*pct == '%') {
159			*s2++ = '%';
160		} else {
161			*f2++ = *pct;
162			*f2 = '\0';
163			if (*pct == 's') {
164				char *ss = va_arg(ap, char *);
165				/* Attempt to limit sprintf output. This
166				 * may be thrown off if field widths were
167				 * specified for this string.
168				 *
169				 * If it looks like the string is too
170				 * long for the remaining buffer, bypass
171				 * sprintf and just copy what fits, then
172				 * quit.
173				 */
174				if (end && strlen(ss) > (rem=end-s2)) {
175					strncpy(s2, ss, rem);
176					return -1;
177				} else {
178					s2 += sprintf(s2, fm2, ss);
179				}
180			} else {
181				s2 += sprintf(s2, fm2, va_arg(ap, int));
182			}
183		}
184		ptr = pct + 1;
185	}
186	if (end) {
187		rem = end-s2;
188		if (rem > 0) {
189			len = strlen(ptr);
190			s2 = lutil_strncopy( s2, ptr, rem );
191			rem -= len;
192		}
193		if (rem < 0) return -1;
194	} else {
195		s2 = lutil_strcopy( s2, ptr );
196	}
197	return s2 - str;
198}
199
200int ber_pvt_vsprintf( char *str, const char *fmt, va_list ap )
201{
202	return vsnprintf( str, 0, fmt, ap );
203}
204
205/* The fixed buffer size here is a problem, we don't know how
206 * to flush the buffer and keep printing if the msg is too big.
207 * Hopefully we never try to write something bigger than this
208 * in a log msg...
209 */
210int ber_pvt_vfprintf( FILE *fp, const char *fmt, va_list ap )
211{
212	char buf[8192];
213	int res;
214
215	vsnprintf( buf, sizeof(buf), fmt, ap );
216	__atoe( buf );
217	res = fputs( buf, fp );
218	if (res == EOF) res = -1;
219	return res;
220}
221
222int ber_pvt_printf( const char *fmt, ... )
223{
224	va_list ap;
225	int res;
226
227	va_start( ap, fmt );
228	res = ber_pvt_vfprintf( stdout, fmt, ap );
229	va_end( ap );
230	return res;
231}
232
233int ber_pvt_fprintf( FILE *fp, const char *fmt, ... )
234{
235	va_list ap;
236	int res;
237
238	va_start( ap, fmt );
239	res = ber_pvt_vfprintf( fp, fmt, ap );
240	va_end( ap );
241	return res;
242}
243#endif
244