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