2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2008 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
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>.
19 #include <ac/stdarg.h>
20 #include <ac/string.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
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.
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 )
53 if (pipe( fds )) return -1;
55 f = fdopen( fds[1], "w" );
61 setvbuf( f, str, _IOFBF, n );
62 sig = signal( SIGPIPE, SIG_IGN );
65 res = vfprintf( f, fmt, ap );
68 signal( SIGPIPE, sig );
69 if ( res > 0 && res < n ) {
70 res = vsprintf( str, fmt, ap );
77 int ber_pvt_snprintf( char *str, size_t n, const char *fmt, ... )
83 res = vsnprintf( str, n, fmt, ap );
87 #endif /* !HAVE_SNPRINTF */
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
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.
106 char *ber_pvt_fgets( char *s, int n, FILE *fp )
108 s = (char *)fgets( s, n, fp );
109 if ( s ) __etoa( s );
113 int ber_pvt_fputs( const char *str, FILE *fp )
117 strncpy( buf, str, sizeof(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;
146 for (pct = strchr(ptr, '%'); pct; pct = strchr(ptr, '%')) {
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++;
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
174 if (end && strlen(ss) > (rem=end-s2)) {
175 strncpy(s2, ss, rem);
178 s2 += sprintf(s2, fm2, ss);
181 s2 += sprintf(s2, fm2, va_arg(ap, int));
190 s2 = lutil_strncopy( s2, ptr, rem );
193 if (rem < 0) return -1;
195 s2 = lutil_strcopy( s2, ptr );
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
210 int ber_pvt_vfprintf( FILE *fp, const char *fmt, va_list ap )
215 vsnprintf( buf, sizeof(buf), fmt, ap );
217 res = fputs( buf, fp );
218 if (res == EOF) res = -1;
222 int ber_pvt_printf( const char *fmt, ... )
228 res = ber_pvt_vfprintf( stdout, fmt, ap );
233 int ber_pvt_fprintf( FILE *fp, const char *fmt, ... )
239 res = ber_pvt_vfprintf( fp, fmt, ap );