3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
10 #include <ac/string.h>
11 #include <ac/unistd.h>
21 #include <ldap_defaults.h>
23 char* lutil_progname( const char* name, int argc, char *argv[] )
28 return ber_strdup( name );
31 progname = strrchr ( argv[0], *LDAP_DIRSEP );
32 progname = ber_strdup( progname ? &progname[1] : argv[0] );
38 int mkstemp( char * template )
41 return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
48 #ifndef HAVE_VSNPRINTF
49 #include <ac/stdarg.h>
50 #include <ac/signal.h>
53 /* Write at most n characters to the buffer in str, return the
54 * number of chars written or -1 if the buffer would have been
57 * This is portable to any POSIX-compliant system. We use pipe()
58 * to create a valid file descriptor, and then fdopen() it to get
59 * a valid FILE pointer. The user's buffer and size are assigned
60 * to the FILE pointer using setvbuf. Then we close the read side
61 * of the pipe to invalidate the descriptor.
63 * If the write arguments all fit into size n, the write will
64 * return successfully. If the write is too large, the stdio
65 * buffer will need to be flushed to the underlying file descriptor.
66 * The flush will fail because it is attempting to write to a
67 * broken pipe, and the write will be terminated.
69 * Note: glibc's setvbuf is broken, so this code fails on glibc.
70 * But that's no loss since glibc provides these functions itself.
72 * In practice, the main app will probably have ignored SIGPIPE
73 * already, so catching it here is redundant, but harmless.
77 int vsnprintf( char *str, size_t n, const char *fmt, va_list ap )
85 if (pipe( fds )) return -1;
87 f = fdopen( fds[1], "w" );
94 sig = SIGNAL( SIGPIPE, SIG_IGN );
96 setvbuf( f, str, _IOFBF, n );
99 res = vfprintf( f, fmt, ap );
102 SIGNAL( SIGPIPE, sig );
107 int snprintf( char *str, size_t n, const char *fmt, ... )
113 res = vsnprintf( str, n, fmt, ap );
117 #endif /* !HAVE_VSNPRINTF */