]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/utils.c
Forced commit, undo previous accidental checkin.
[openldap] / libraries / liblutil / utils.c
index 5024e907ffe2d584f4be959ea7bb1b0068d2da3f..973406f21ebde25448f840517aba226129c7b79b 100644 (file)
@@ -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
 #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;
 }
 
-#ifndef HAVE_MKSTEMP
-int mkstemp( char * template )
+size_t lutil_gentime( char *s, size_t max, const struct tm *tm )
 {
-#ifdef HAVE_MKTEMP
-       return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
-#else
-       return -1;
+       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;
+}
 
-#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
+/* 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