#include "portable.h"
#include <stdio.h>
#include <ac/stdlib.h>
#include <ac/string.h>
#include <ac/unistd.h>
#include <ac/time.h>
#ifdef HAVE_IO_H
#include <io.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <lutil.h>
#include <ldap_defaults.h>
#ifdef HAVE_EBCDIC
int _trans_argv = 1;
#endif
#ifdef _WIN32
void lutil_slashpath( char *path )
{
char *c, *p;
p = path;
while (( c=strchr( p, '/' ))) {
*c++ = '\\';
p = c;
}
}
#endif
char* lutil_progname( const char* name, int argc, char *argv[] )
{
char *progname;
if(argc == 0) {
return (char *)name;
}
#ifdef HAVE_EBCDIC
if (_trans_argv) {
int i;
for (i=0; i<argc; i++) __etoa(argv[i]);
_trans_argv = 0;
}
#endif
LUTIL_SLASHPATH( argv[0] );
progname = strrchr ( argv[0], *LDAP_DIRSEP );
progname = progname ? &progname[1] : argv[0];
return progname;
}
#if 0
size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
{
size_t ret;
#ifdef HAVE_EBCDIC
#pragma convlit(suspend)
#endif
ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
#ifdef HAVE_EBCDIC
#pragma convlit(resume)
__etoa( s );
#endif
return ret;
}
#endif
size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
{
size_t ret;
char *p;
if ( smax < 16 ) {
return 0;
}
#ifdef HAVE_EBCDIC
#pragma convlit(suspend)
#endif
ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
#ifdef HAVE_EBCDIC
#pragma convlit(resume)
__etoa( s );
#endif
if ( delta == 0 || ret == 0 ) {
return ret;
}
if ( smax < 20 ) {
return 0;
}
p = s + 14;
if ( delta < 0 ) {
p[ 0 ] = '-';
delta = -delta;
} else {
p[ 0 ] = '+';
}
p++;
snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
( delta % 3600 ) / 60 );
return ret + 5;
}
char *
lutil_strcopy(
char *a,
const char *b
)
{
if (!a || !b)
return a;
while ((*a++ = *b++)) ;
return a-1;
}
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 )
{
#ifdef HAVE_MKTEMP
return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
#else
return -1;
#endif
}
#endif