aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/openldap/libraries/liblber/stdio.c
blob: 3346b6f826b91bb0cc4657b41c15d191e0972888 (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
 *
 * Copyright 1998-2024 The OpenLDAP Foundation.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted only as authorized by the OpenLDAP
 * Public License.
 *
 * A copy of this license is available in the file LICENSE in the
 * top-level directory of the distribution or, alternatively, at
 * <http://www.OpenLDAP.org/license.html>.
 */

#include "portable.h"

#include <stdio.h>
#include <ac/stdarg.h>
#include <ac/string.h>
#include <ac/ctype.h>
#include <lutil.h>
#include <unistd.h>

#if !defined(HAVE_VSNPRINTF) && !defined(HAVE_EBCDIC)
/* Write at most n characters to the buffer in str, return the
 * number of chars written or -1 if the buffer would have been
 * overflowed.
 *
 * This is portable to any POSIX-compliant system. We use pipe()
 * to create a valid file descriptor, and then fdopen() it to get
 * a valid FILE pointer. The user's buffer and size are assigned
 * to the FILE pointer using setvbuf. Then we close the read side
 * of the pipe to invalidate the descriptor.
 *
 * If the write arguments all fit into size n, the write will
 * return successfully. If the write is too large, the stdio
 * buffer will need to be flushed to the underlying file descriptor.
 * The flush will fail because it is attempting to write to a
 * broken pipe, and the write will be terminated.
 * -- hyc, 2002-07-19
 */
/* This emulation uses vfprintf; on OS/390 we're also emulating
 * that function so it's more efficient just to have a separate
 * version of vsnprintf there.
 */
#include <ac/signal.h>
int ber_pvt_vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
{
	int fds[2], res;
	FILE *f;
	RETSIGTYPE (*sig)();

	if (pipe( fds )) return -1;

	f = fdopen( fds[1], "w" );
	if ( !f ) {
		close( fds[1] );
		close( fds[0] );
		return -1;
	}
	setvbuf( f, str, _IOFBF, n );
	sig = signal( SIGPIPE, SIG_IGN );
	close( fds[0] );

	res = vfprintf( f, fmt, ap );

	fclose( f );
	signal( SIGPIPE, sig );
	if ( res > 0 && res < n ) {
		res = vsprintf( str, fmt, ap );
	}
	return res;
}
#endif

#ifndef HAVE_SNPRINTF
int ber_pvt_snprintf( char *str, size_t n, const char *fmt, ... )
{
	va_list ap;
	int res;

	va_start( ap, fmt );
	res = vsnprintf( str, n, fmt, ap );
	va_end( ap );
	return res;
}
#endif /* !HAVE_SNPRINTF */

#ifdef HAVE_EBCDIC
/* stdio replacements with ASCII/EBCDIC translation for OS/390.
 * The OS/390 port depends on the CONVLIT compiler option being
 * used to force character and string literals to be compiled in
 * ISO8859-1, and the __LIBASCII cpp symbol to be defined to use the
 * OS/390 ASCII-compatibility library. This library only supplies
 * an ASCII version of sprintf, so other needed functions are
 * provided here.
 *
 * All of the internal character manipulation is done in ASCII,
 * but file I/O is EBCDIC, so we catch any stdio reading/writing
 * of files here and do the translations.
 */

#undef fputs
#undef fgets

char *ber_pvt_fgets( char *s, int n, FILE *fp )
{
	s = (char *)fgets( s, n, fp );
	if ( s ) __etoa( s );
	return s;
}

int ber_pvt_fputs( const char *str, FILE *fp )
{
	char buf[8192];

	strncpy( buf, str, sizeof(buf) );
	__atoe( buf );
	return fputs( buf, fp );
}

/* The __LIBASCII doesn't include a working vsprintf, so we make do
 * using just sprintf. This is a very simplistic parser that looks for
 * format strings and uses sprintf to process them one at a time.
 * Literal text is just copied straight to the destination.
 * The result is appended to the destination string. The parser
 * recognizes field-width specifiers and the 'l' qualifier; it
 * may need to be extended to recognize other qualifiers but so
 * far this seems to be enough.
 */
int ber_pvt_vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
{
	char *ptr, *pct, *s2, *f2, *end;
	char fm2[64];
	int len, rem;

	ptr = (char *)fmt;
	s2 = str;
	fm2[0] = '%';
	if (n) {
		end = str + n;
	} else {
		end = NULL;
	}

	for (pct = strchr(ptr, '%'); pct; pct = strchr(ptr, '%')) {
		len = pct-ptr;
		if (end) {
			rem = end-s2;
			if (rem < 1) return -1;
			if (rem < len) len = rem;
		}
		s2 = lutil_strncopy( s2, ptr, len );
		/* Did we cheat the length above? If so, bail out */
		if (len < pct-ptr) return -1;
		for (pct++, f2 = fm2+1; isdigit(*pct);) *f2++ = *pct++;
		if (*pct == 'l') *f2++ = *pct++;
		if (*pct == '%') {
			*s2++ = '%';
		} else {
			*f2++ = *pct;
			*f2 = '\0';
			if (*pct == 's') {
				char *ss = va_arg(ap, char *);
				/* Attempt to limit sprintf output. This
				 * may be thrown off if field widths were
				 * specified for this string.
				 *
				 * If it looks like the string is too
				 * long for the remaining buffer, bypass
				 * sprintf and just copy what fits, then
				 * quit.
				 */
				if (end && strlen(ss) > (rem=end-s2)) {
					strncpy(s2, ss, rem);
					return -1;
				} else {
					s2 += sprintf(s2, fm2, ss);
				}
			} else {
				s2 += sprintf(s2, fm2, va_arg(ap, int));
			}
		}
		ptr = pct + 1;
	}
	if (end) {
		rem = end-s2;
		if (rem > 0) {
			len = strlen(ptr);
			s2 = lutil_strncopy( s2, ptr, rem );
			rem -= len;
		}
		if (rem < 0) return -1;
	} else {
		s2 = lutil_strcopy( s2, ptr );
	}
	return s2 - str;
}

int ber_pvt_vsprintf( char *str, const char *fmt, va_list ap )
{
	return vsnprintf( str, 0, fmt, ap );
}

/* The fixed buffer size here is a problem, we don't know how
 * to flush the buffer and keep printing if the msg is too big. 
 * Hopefully we never try to write something bigger than this
 * in a log msg...
 */
int ber_pvt_vfprintf( FILE *fp, const char *fmt, va_list ap )
{
	char buf[8192];
	int res;

	vsnprintf( buf, sizeof(buf), fmt, ap );
	__atoe( buf );
	res = fputs( buf, fp );
	if (res == EOF) res = -1;
	return res;
}

int ber_pvt_printf( const char *fmt, ... )
{
	va_list ap;
	int res;

	va_start( ap, fmt );
	res = ber_pvt_vfprintf( stdout, fmt, ap );
	va_end( ap );
	return res;
}

int ber_pvt_fprintf( FILE *fp, const char *fmt, ... )
{
	va_list ap;
	int res;

	va_start( ap, fmt );
	res = ber_pvt_vfprintf( fp, fmt, ap );
	va_end( ap );
	return res;
}
#endif