From: Howard Chu Date: Sat, 27 Jul 2002 00:25:16 +0000 (+0000) Subject: Moved slap_strcopy, slap_strncopy to lutil_strcopy, lutil_strncopy X-Git-Tag: NO_SLAP_OP_BLOCKS~1320 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=155c16484bf7beca6ebfa97950093df6081d33fc;p=openldap Moved slap_strcopy, slap_strncopy to lutil_strcopy, lutil_strncopy Added lutil_gentime, moved lutil_vsnprintf to stdio.c --- diff --git a/libraries/liblutil/getopt.c b/libraries/liblutil/getopt.c index c6e8e914f7..137944ab6f 100644 --- a/libraries/liblutil/getopt.c +++ b/libraries/liblutil/getopt.c @@ -23,6 +23,8 @@ #include #endif +#include "lutil.h" + #ifndef STDERR_FILENO #define STDERR_FILENO 2 #endif @@ -32,20 +34,27 @@ int optind = 1; int optopt; char * optarg; +#ifdef HAVE_EBCDIC +extern int _trans_argv; +#endif + static void ERR (char * const argv[], const char * s, char c) { - char errbuf[2]; - #ifdef DF_TRACE_DEBUG printf("DF_TRACE_DEBUG: static void ERR () in getopt.c\n"); #endif if (opterr) { - errbuf[0] = c; - errbuf[1] = '\n'; - (void) write(STDERR_FILENO,argv[0],strlen(argv[0])); - (void) write(STDERR_FILENO,s,strlen(s)); - (void) write(STDERR_FILENO,errbuf,sizeof errbuf); + char *ptr, outbuf[4096]; + + ptr = lutil_strncopy(outbuf, argv[0], sizeof(outbuf) - 2); + ptr = lutil_strncopy(ptr, s, sizeof(outbuf)-2 -(ptr-outbuf)); + *ptr++ = c; + *ptr++ = '\n'; +#ifdef HAVE_EBCDIC + __atoe_l(outbuf, ptr - outbuf); +#endif + (void) write(STDERR_FILENO,outbuf,ptr - outbuf); } } @@ -57,6 +66,14 @@ int getopt (int argc, char * const argv [], const char * opts) #ifdef DF_TRACE_DEBUG printf("DF_TRACE_DEBUG: int getopt () in getopt.c\n"); +#endif + +#ifdef HAVE_EBCDIC + if (_trans_argv) { + int i; + for (i=0; i +#include +#include + +#include + +#ifndef HAVE_VSNPRINTF +/* 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 + */ +#ifndef HAVE_EBCDIC +/* 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 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 + +int 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 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 diff --git a/libraries/liblutil/utils.c b/libraries/liblutil/utils.c index 5024e907ff..973406f21e 100644 --- a/libraries/liblutil/utils.c +++ b/libraries/liblutil/utils.c @@ -9,6 +9,7 @@ #include #include #include +#include #ifdef HAVE_IO_H #include #endif @@ -16,102 +17,93 @@ #include #endif -#include #include #include +#ifdef HAVE_EBCDIC +int _trans_argv = 1; +#endif + char* lutil_progname( const char* name, int argc, char *argv[] ) { char *progname; if(argc == 0) { - return ber_strdup( name ); + return (char *)name; } +#ifdef HAVE_EBCDIC + if (_trans_argv) { + int i; + for (i=0; i -#include -#include - -/* 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. - * - * Note: glibc's setvbuf is broken, so this code fails on glibc. - * But that's no loss since glibc provides these functions itself. - * - * In practice, the main app will probably have ignored SIGPIPE - * already, so catching it here is redundant, but harmless. - * - * -- hyc, 2002-07-19 +/* strcopy is like strcpy except it returns a pointer to the trailing NUL of + * the result string. This allows fast construction of catenated strings + * without the overhead of strlen/strcat. */ -int vsnprintf( char *str, size_t n, const char *fmt, va_list ap ) +char * +lutil_strcopy( + char *a, + const char *b +) { - int fds[2], res; - FILE *f; -#ifdef SIGPIPE - RETSIGTYPE (*sig)(); -#endif - - if (pipe( fds )) return -1; - - f = fdopen( fds[1], "w" ); - if ( !f ) { - close( fds[1] ); - close( fds[0] ); - return -1; - } -#ifdef SIGPIPE - sig = SIGNAL( SIGPIPE, SIG_IGN ); -#endif - setvbuf( f, str, _IOFBF, n ); - close( fds[0] ); - - res = vfprintf( f, fmt, ap ); - fclose( f ); -#ifdef SIGPIPE - SIGNAL( SIGPIPE, sig ); -#endif - return res; + if (!a || !b) + return a; + + while ((*a++ = *b++)) ; + return a-1; } -int snprintf( char *str, size_t n, const char *fmt, ... ) +/* strncopy is like strcpy except it returns a pointer to the trailing NUL of + * the result string. This allows fast construction of catenated strings + * without the overhead of strlen/strcat. + */ +char * +lutil_strncopy( + char *a, + const char *b, + size_t n +) { - va_list ap; - int res; + if (!a || !b || n == 0) + return a; + + while ((*a++ = *b++) && n-- > 0) ; + return a-1; +} - va_start( ap, fmt ); - res = vsnprintf( str, n, fmt, ap ); - va_end( ap ); - return res; +#ifndef HAVE_MKSTEMP +int mkstemp( char * template ) +{ +#ifdef HAVE_MKTEMP + return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 ); +#else + return -1; +#endif } -#endif /* !HAVE_VSNPRINTF */ +#endif