No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / openldap / dist / libraries / liblber / stdio.c
blob7be692c50948ca591f02757cd34269a4d80f64a5
1 /* $OpenLDAP: pkg/ldap/libraries/liblber/stdio.c,v 1.11.2.3 2008/02/11 23:26:41 kurt Exp $ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
5 * All rights reserved.
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.
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>.
16 #include "portable.h"
18 #include <stdio.h>
19 #include <ac/stdarg.h>
20 #include <ac/string.h>
21 #include <ac/ctype.h>
22 #include <lutil.h>
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.
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.
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
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.
46 #include <ac/signal.h>
47 int ber_pvt_vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
49 int fds[2], res;
50 FILE *f;
51 RETSIGTYPE (*sig)();
53 if (pipe( fds )) return -1;
55 f = fdopen( fds[1], "w" );
56 if ( !f ) {
57 close( fds[1] );
58 close( fds[0] );
59 return -1;
61 setvbuf( f, str, _IOFBF, n );
62 sig = signal( SIGPIPE, SIG_IGN );
63 close( fds[0] );
65 res = vfprintf( f, fmt, ap );
67 fclose( f );
68 signal( SIGPIPE, sig );
69 if ( res > 0 && res < n ) {
70 res = vsprintf( str, fmt, ap );
72 return res;
74 #endif
76 #ifndef HAVE_SNPRINTF
77 int ber_pvt_snprintf( char *str, size_t n, const char *fmt, ... )
79 va_list ap;
80 int res;
82 va_start( ap, fmt );
83 res = vsnprintf( str, n, fmt, ap );
84 va_end( ap );
85 return res;
87 #endif /* !HAVE_SNPRINTF */
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.
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.
103 #undef fputs
104 #undef fgets
106 char *ber_pvt_fgets( char *s, int n, FILE *fp )
108 s = (char *)fgets( s, n, fp );
109 if ( s ) __etoa( s );
110 return s;
113 int ber_pvt_fputs( const char *str, FILE *fp )
115 char buf[8192];
117 strncpy( buf, str, sizeof(buf) );
118 __atoe( buf );
119 return fputs( buf, fp );
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.
131 int ber_pvt_vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
133 char *ptr, *pct, *s2, *f2, *end;
134 char fm2[64];
135 int len, rem;
137 ptr = (char *)fmt;
138 s2 = str;
139 fm2[0] = '%';
140 if (n) {
141 end = str + n;
142 } else {
143 end = NULL;
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;
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.
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.
174 if (end && strlen(ss) > (rem=end-s2)) {
175 strncpy(s2, ss, rem);
176 return -1;
177 } else {
178 s2 += sprintf(s2, fm2, ss);
180 } else {
181 s2 += sprintf(s2, fm2, va_arg(ap, int));
184 ptr = pct + 1;
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;
193 if (rem < 0) return -1;
194 } else {
195 s2 = lutil_strcopy( s2, ptr );
197 return s2 - str;
200 int ber_pvt_vsprintf( char *str, const char *fmt, va_list ap )
202 return vsnprintf( str, 0, fmt, ap );
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...
210 int ber_pvt_vfprintf( FILE *fp, const char *fmt, va_list ap )
212 char buf[8192];
213 int res;
215 vsnprintf( buf, sizeof(buf), fmt, ap );
216 __atoe( buf );
217 res = fputs( buf, fp );
218 if (res == EOF) res = -1;
219 return res;
222 int ber_pvt_printf( const char *fmt, ... )
224 va_list ap;
225 int res;
227 va_start( ap, fmt );
228 res = ber_pvt_vfprintf( stdout, fmt, ap );
229 va_end( ap );
230 return res;
233 int ber_pvt_fprintf( FILE *fp, const char *fmt, ... )
235 va_list ap;
236 int res;
238 va_start( ap, fmt );
239 res = ber_pvt_vfprintf( fp, fmt, ap );
240 va_end( ap );
241 return res;
243 #endif