mirror of
https://git.openldap.org/openldap/openldap.git
synced 2025-12-27 01:59:38 -05:00
Moved slap_strcopy, slap_strncopy to lutil_strcopy, lutil_strncopy
Added lutil_gentime, moved lutil_vsnprintf to stdio.c
This commit is contained in:
parent
5a01db28e3
commit
155c16484b
3 changed files with 318 additions and 81 deletions
|
|
@ -23,6 +23,8 @@
|
|||
#include <io.h>
|
||||
#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<argc; i++) __etoa(argv[i]);
|
||||
_trans_argv = 0;
|
||||
}
|
||||
#endif
|
||||
if (sp == 1)
|
||||
{
|
||||
|
|
|
|||
228
libraries/liblutil/stdio.c
Normal file
228
libraries/liblutil/stdio.c
Normal file
|
|
@ -0,0 +1,228 @@
|
|||
/* $OpenLDAP$ */
|
||||
/*
|
||||
* Copyright 2002 The OpenLDAP Foundation, All Rights Reserved.
|
||||
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
||||
*/
|
||||
|
||||
#include "portable.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ac/stdarg.h>
|
||||
#include <ac/string.h>
|
||||
|
||||
#include <lutil.h>
|
||||
|
||||
#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 <ac/signal.h>
|
||||
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
|
||||
|
|
@ -9,6 +9,7 @@
|
|||
#include <ac/stdlib.h>
|
||||
#include <ac/string.h>
|
||||
#include <ac/unistd.h>
|
||||
#include <ac/time.h>
|
||||
#ifdef HAVE_IO_H
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
|
@ -16,24 +17,86 @@
|
|||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <lber.h>
|
||||
#include <lutil.h>
|
||||
#include <ldap_defaults.h>
|
||||
|
||||
#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<argc; i++) __etoa(argv[i]);
|
||||
_trans_argv = 0;
|
||||
}
|
||||
#endif
|
||||
progname = strrchr ( argv[0], *LDAP_DIRSEP );
|
||||
progname = ber_strdup( progname ? &progname[1] : argv[0] );
|
||||
progname = progname ? &progname[1] : argv[0];
|
||||
|
||||
return progname;
|
||||
}
|
||||
|
||||
size_t lutil_gentime( char *s, size_t max, const struct tm *tm )
|
||||
{
|
||||
size_t ret;
|
||||
#ifdef HAVE_EBCDIC
|
||||
/* We've been compiling in ASCII so far, but we want EBCDIC now since
|
||||
* strftime only understands EBCDIC input.
|
||||
*/
|
||||
#pragma convlit(suspend)
|
||||
#endif
|
||||
ret = strftime( s, max, "%Y%m%d%H%M%SZ", tm );
|
||||
#ifdef HAVE_EBCDIC
|
||||
#pragma convlit(resume)
|
||||
__etoa( s );
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
char *
|
||||
lutil_strcopy(
|
||||
char *a,
|
||||
const char *b
|
||||
)
|
||||
{
|
||||
if (!a || !b)
|
||||
return a;
|
||||
|
||||
while ((*a++ = *b++)) ;
|
||||
return a-1;
|
||||
}
|
||||
|
||||
/* 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
|
||||
)
|
||||
{
|
||||
if (!a || !b || n == 0)
|
||||
return a;
|
||||
|
||||
while ((*a++ = *b++) && n-- > 0) ;
|
||||
return a-1;
|
||||
}
|
||||
|
||||
#ifndef HAVE_MKSTEMP
|
||||
int mkstemp( char * template )
|
||||
{
|
||||
|
|
@ -44,74 +107,3 @@ int mkstemp( char * template )
|
|||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef HAVE_VSNPRINTF
|
||||
#include <ac/stdarg.h>
|
||||
#include <ac/signal.h>
|
||||
#include <stdio.h>
|
||||
|
||||
/* 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
|
||||
*/
|
||||
int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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 */
|
||||
|
|
|
|||
Loading…
Reference in a new issue