1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2004-01-02 03:15:16 +08:00
|
|
|
* Copyright 1998-2004 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-08-18 03:00:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
2003-05-15 09:03:38 +08:00
|
|
|
#include <stdio.h>
|
1999-08-18 03:00:59 +08:00
|
|
|
#include <ac/stdlib.h>
|
|
|
|
#include <ac/string.h>
|
2001-12-18 10:54:49 +08:00
|
|
|
#include <ac/unistd.h>
|
2002-07-27 08:25:16 +08:00
|
|
|
#include <ac/time.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
|
|
|
|
|
|
|
#include <lutil.h>
|
|
|
|
#include <ldap_defaults.h>
|
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
int _trans_argv = 1;
|
|
|
|
#endif
|
|
|
|
|
2004-03-19 16:06:42 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Some Windows versions accept both forward and backslashes in
|
|
|
|
* directory paths, but we always use backslashes when generating
|
|
|
|
* and parsing...
|
|
|
|
*/
|
|
|
|
void lutil_slashpath( char *path )
|
|
|
|
{
|
|
|
|
char *c, *p;
|
|
|
|
|
|
|
|
p = path;
|
|
|
|
while (( c=strchr( p, '/' ))) {
|
|
|
|
*c++ = '\\';
|
|
|
|
p = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
char* lutil_progname( const char* name, int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
char *progname;
|
|
|
|
|
|
|
|
if(argc == 0) {
|
2002-07-27 08:25:16 +08:00
|
|
|
return (char *)name;
|
1999-08-18 03:00:59 +08:00
|
|
|
}
|
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
if (_trans_argv) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<argc; i++) __etoa(argv[i]);
|
|
|
|
_trans_argv = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2004-03-19 16:06:42 +08:00
|
|
|
LUTIL_SLASHPATH( argv[0] );
|
1999-08-18 03:00:59 +08:00
|
|
|
progname = strrchr ( argv[0], *LDAP_DIRSEP );
|
2002-07-27 08:25:16 +08:00
|
|
|
progname = progname ? &progname[1] : argv[0];
|
1999-08-18 03:00:59 +08:00
|
|
|
return progname;
|
|
|
|
}
|
2001-06-15 12:16:55 +08:00
|
|
|
|
2003-05-15 09:18:45 +08:00
|
|
|
#if 0
|
2003-05-15 09:03:38 +08:00
|
|
|
size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
|
2001-06-15 12:16:55 +08:00
|
|
|
{
|
2002-07-27 08:25:16 +08:00
|
|
|
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)
|
2001-12-18 10:54:49 +08:00
|
|
|
#endif
|
2003-05-15 09:03:38 +08:00
|
|
|
ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
#pragma convlit(resume)
|
|
|
|
__etoa( s );
|
2001-10-11 12:01:45 +08:00
|
|
|
#endif
|
2002-07-27 08:25:16 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2003-05-15 09:18:45 +08:00
|
|
|
#endif
|
2002-07-20 01:33:14 +08:00
|
|
|
|
2003-05-15 09:03:38 +08:00
|
|
|
size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
char *p;
|
|
|
|
|
2003-05-15 09:18:45 +08:00
|
|
|
if ( smax < 16 ) { /* YYYYmmddHHMMSSZ */
|
|
|
|
return 0;
|
2003-05-15 09:03:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#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, smax, "%Y%m%d%H%M%SZ", tm );
|
2003-05-29 14:15:03 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
#pragma convlit(resume)
|
|
|
|
__etoa( s );
|
|
|
|
#endif
|
2003-05-15 09:18:45 +08:00
|
|
|
if ( delta == 0 || ret == 0 ) {
|
2003-05-15 09:03:38 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-05-15 09:18:45 +08:00
|
|
|
if ( smax < 20 ) { /* YYYYmmddHHMMSS+HHMM */
|
|
|
|
return 0;
|
2003-05-15 09:03:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
/* 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.
|
2002-07-20 01:33:14 +08:00
|
|
|
*/
|
2002-07-27 08:25:16 +08:00
|
|
|
char *
|
|
|
|
lutil_strcopy(
|
|
|
|
char *a,
|
|
|
|
const char *b
|
|
|
|
)
|
2002-07-20 01:33:14 +08:00
|
|
|
{
|
2002-07-27 08:25:16 +08:00
|
|
|
if (!a || !b)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
while ((*a++ = *b++)) ;
|
|
|
|
return a-1;
|
2002-07-20 01:33:14 +08:00
|
|
|
}
|
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
/* 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
|
|
|
|
)
|
2002-07-20 01:33:14 +08:00
|
|
|
{
|
2002-07-27 08:25:16 +08:00
|
|
|
if (!a || !b || n == 0)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
while ((*a++ = *b++) && n-- > 0) ;
|
|
|
|
return a-1;
|
|
|
|
}
|
2002-07-20 01:33:14 +08:00
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
#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
|
2002-07-20 01:33:14 +08:00
|
|
|
}
|
2002-07-27 08:25:16 +08:00
|
|
|
#endif
|