1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
1999-08-18 03:00:59 +08:00
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
1999-08-18 03:00:59 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/string.h>
|
2001-12-18 10:54:49 +08:00
|
|
|
#include <ac/unistd.h>
|
2001-12-18 12:52:55 +08:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2001-12-18 10:48:20 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
1999-08-18 03:00:59 +08:00
|
|
|
|
2000-06-08 02:49:36 +08:00
|
|
|
#include <lber.h>
|
1999-08-18 03:00:59 +08:00
|
|
|
#include <lutil.h>
|
|
|
|
#include <ldap_defaults.h>
|
|
|
|
|
|
|
|
char* lutil_progname( const char* name, int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
char *progname;
|
|
|
|
|
|
|
|
if(argc == 0) {
|
2000-06-07 14:33:59 +08:00
|
|
|
return ber_strdup( name );
|
1999-08-18 03:00:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
progname = strrchr ( argv[0], *LDAP_DIRSEP );
|
2000-06-07 14:33:59 +08:00
|
|
|
progname = ber_strdup( progname ? &progname[1] : argv[0] );
|
1999-08-18 03:00:59 +08:00
|
|
|
|
|
|
|
return progname;
|
|
|
|
}
|
2001-06-15 12:16:55 +08:00
|
|
|
|
|
|
|
#ifndef HAVE_MKSTEMP
|
|
|
|
int mkstemp( char * template )
|
|
|
|
{
|
2001-12-18 10:54:49 +08:00
|
|
|
#ifdef HAVE_MKTEMP
|
2001-12-18 10:48:20 +08:00
|
|
|
return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
|
2001-12-18 10:54:49 +08:00
|
|
|
#else
|
2001-12-18 10:55:38 +08:00
|
|
|
return -1;
|
2001-12-18 10:54:49 +08:00
|
|
|
#endif
|
2001-06-15 12:16:55 +08:00
|
|
|
}
|
2001-10-11 12:01:45 +08:00
|
|
|
#endif
|
2002-07-20 01:33:14 +08:00
|
|
|
|
|
|
|
#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 */
|