From: Howard Chu Date: Tue, 10 Sep 2002 00:47:32 +0000 (+0000) Subject: Moved stdio replacements from liblutil lutil_* to liblber ber_pvt_*. X-Git-Tag: NO_SLAP_OP_BLOCKS~995 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=890d4b9449f9f022505ece7811dde7c36fcc4c68;p=openldap Moved stdio replacements from liblutil lutil_* to liblber ber_pvt_*. --- diff --git a/acconfig.h b/acconfig.h index cdd82b766f..37ec724e6d 100644 --- a/acconfig.h +++ b/acconfig.h @@ -100,12 +100,12 @@ * vsnprintf and snprintf are used too, but they are already * checked by the configure script */ -#define fputs lutil_fputs -#define fgets lutil_fgets -#define printf lutil_printf -#define fprintf lutil_fprintf -#define vfprintf lutil_vfprintf -#define vsprintf lutil_vsprintf +#define fputs ber_pvt_fputs +#define fgets ber_pvt_fgets +#define printf ber_pvt_printf +#define fprintf ber_pvt_fprintf +#define vfprintf ber_pvt_vfprintf +#define vsprintf ber_pvt_vsprintf #endif #include "ldap_cdefs.h" diff --git a/configure b/configure index 7aec0fede0..8b2d460d8b 100755 --- a/configure +++ b/configure @@ -22256,13 +22256,13 @@ if test "$ac_cv_func_snprintf" != yes -o "$ac_cv_func_vsnprintf" != yes; then LIBOBJS="$LIBOBJS stdio.o" if test "$ac_cv_func_snprintf" != yes; then cat >> confdefs.h <<\EOF -#define snprintf lutil_snprintf +#define snprintf ber_pvt_snprintf EOF fi if test "$ac_cv_func_vsnprintf" != yes; then cat >> confdefs.h <<\EOF -#define vsnprintf lutil_vsnprintf +#define vsnprintf ber_pvt_vsnprintf EOF fi diff --git a/configure.in b/configure.in index 899cd51b84..6be54b88d3 100644 --- a/configure.in +++ b/configure.in @@ -2491,10 +2491,10 @@ if test "$ac_cv_func_snprintf" != yes -o "$ac_cv_func_vsnprintf" != yes; then LIBSRCS="$LIBSRCS stdio.c" LIBOBJS="$LIBOBJS stdio.o" if test "$ac_cv_func_snprintf" != yes; then - AC_DEFINE(snprintf, lutil_snprintf, [define to snprintf routine]) + AC_DEFINE(snprintf, ber_pvt_snprintf, [define to snprintf routine]) fi if test "$ac_cv_func_vsnprintf" != yes; then - AC_DEFINE(vsnprintf, lutil_vsnprintf, [define to snprintf routine]) + AC_DEFINE(vsnprintf, ber_pvt_vsnprintf, [define to snprintf routine]) fi fi diff --git a/include/portable.h.in b/include/portable.h.in index 8846242686..b4d2b7ee11 100644 --- a/include/portable.h.in +++ b/include/portable.h.in @@ -1030,12 +1030,12 @@ * vsnprintf and snprintf are used too, but they are already * checked by the configure script */ -#define fputs lutil_fputs -#define fgets lutil_fgets -#define printf lutil_printf -#define fprintf lutil_fprintf -#define vfprintf lutil_vfprintf -#define vsprintf lutil_vsprintf +#define fputs ber_pvt_fputs +#define fgets ber_pvt_fgets +#define printf ber_pvt_printf +#define fprintf ber_pvt_fprintf +#define vfprintf ber_pvt_vfprintf +#define vsprintf ber_pvt_vsprintf #endif #include "ldap_cdefs.h" diff --git a/libraries/liblber/Makefile.in b/libraries/liblber/Makefile.in index 87da85611a..cae30fbc54 100644 --- a/libraries/liblber/Makefile.in +++ b/libraries/liblber/Makefile.in @@ -10,6 +10,9 @@ LIBRARY = liblber.la NT_SRCS = nt_err.c NT_OBJS = nt_err.lo +UNIX_SRCS = stdio.c +UNIX_OBJS = stdio.lo + LIB_DEFS = -DLBER_LIBRARY SRCS= assert.c decode.c encode.c io.c bprint.c debug.c \ diff --git a/libraries/liblber/stdio.c b/libraries/liblber/stdio.c new file mode 100644 index 0000000000..0c5205ac80 --- /dev/null +++ b/libraries/liblber/stdio.c @@ -0,0 +1,226 @@ +/* $OpenLDAP$ */ +/* + * Copyright 2002 The OpenLDAP Foundation, All Rights Reserved. + * COPYING RESTRICTIONS APPLY, see COPYRIGHT file + */ + +#include "portable.h" + +#include +#include +#include + +#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 +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 ); + 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_VSNPRINTF */ + +#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 = ber_pvt_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 = ber_pvt_strncopy( s2, ptr, rem ); + rem -= len; + } + if (rem < 0) return -1; + } else { + s2 = ber_pvt_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 diff --git a/libraries/liblutil/stdio.c b/libraries/liblutil/stdio.c deleted file mode 100644 index 39d8a37541..0000000000 --- a/libraries/liblutil/stdio.c +++ /dev/null @@ -1,228 +0,0 @@ -/* $OpenLDAP$ */ -/* - * Copyright 2002 The OpenLDAP Foundation, All Rights Reserved. - * COPYING RESTRICTIONS APPLY, see COPYRIGHT file - */ - -#include "portable.h" - -#include -#include -#include - -#include - -#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 -int lutil_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 ); - return res; -} -#endif - -#ifndef HAVE_SNPRINTF -int lutil_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_VSNPRINTF */ - -#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 *lutil_fgets( char *s, int n, FILE *fp ) -{ - s = (char *)fgets( s, n, fp ); - if ( s ) __etoa( s ); - return s; -} - -int lutil_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 lutil_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 lutil_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 lutil_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 lutil_printf( const char *fmt, ... ) -{ - va_list ap; - int res; - - va_start( ap, fmt ); - res = lutil_vfprintf( stdout, fmt, ap ); - va_end( ap ); - return res; -} - -int lutil_fprintf( FILE *fp, const char *fmt, ... ) -{ - va_list ap; - int res; - - va_start( ap, fmt ); - res = lutil_vfprintf( fp, fmt, ap ); - va_end( ap ); - return res; -} -#endif