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