2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
19 #include <ac/stdlib.h>
20 #include <ac/string.h>
22 #include <ac/unistd.h>
32 #include "ldap_defaults.h"
40 /* Some Windows versions accept both forward and backslashes in
41 * directory paths, but we always use backslashes when generating
44 void lutil_slashpath( char *path )
49 while (( c=strchr( p, '/' ))) {
56 char* lutil_progname( const char* name, int argc, char *argv[] )
67 for (i=0; i<argc; i++) __etoa(argv[i]);
71 LUTIL_SLASHPATH( argv[0] );
72 progname = strrchr ( argv[0], *LDAP_DIRSEP );
73 progname = progname ? &progname[1] : argv[0];
78 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
82 /* We've been compiling in ASCII so far, but we want EBCDIC now since
83 * strftime only understands EBCDIC input.
85 #pragma convlit(suspend)
87 ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
89 #pragma convlit(resume)
96 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
101 if ( smax < 16 ) { /* YYYYmmddHHMMSSZ */
106 /* We've been compiling in ASCII so far, but we want EBCDIC now since
107 * strftime only understands EBCDIC input.
109 #pragma convlit(suspend)
111 ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
113 #pragma convlit(resume)
116 if ( delta == 0 || ret == 0 ) {
120 if ( smax < 20 ) { /* YYYYmmddHHMMSS+HHMM */
134 snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
135 ( delta % 3600 ) / 60 );
140 int lutil_tm2time( struct lutil_tm *tm, struct lutil_timet *tt )
142 static int moffset[12] = {
148 tt->tt_usec = tm->tm_usec;
150 /* special case 0000/01/01+00:00:00 is returned as zero */
151 if ( tm->tm_year == -1900 && tm->tm_mon == 0 && tm->tm_mday == 1 &&
152 tm->tm_hour == 0 && tm->tm_min == 0 && tm->tm_sec == 0 ) {
158 /* tm->tm_year is years since 1900 */
159 /* calculate days from years since 1970 (epoch) */
160 tt->tt_sec = tm->tm_year - 70;
163 /* count leap days in preceding years */
164 tt->tt_sec += ((tm->tm_year -69) >> 2);
166 /* calculate days from months */
167 tt->tt_sec += moffset[tm->tm_mon];
169 /* add in this year's leap day, if any */
170 if (((tm->tm_year & 3) == 0) && (tm->tm_mon > 1)) {
174 /* add in days in this month */
175 tt->tt_sec += (tm->tm_mday - 1);
177 /* this function can handle a range of about 17408 years... */
178 /* 86400 seconds in a day, divided by 128 = 675 */
181 /* move high 7 bits into tt_gsec */
182 tt->tt_gsec = tt->tt_sec >> 25;
183 tt->tt_sec -= tt->tt_gsec << 25;
188 /* convert to minutes */
192 /* convert to seconds */
196 /* add remaining seconds */
204 int lutil_parsetime( char *atm, struct lutil_tm *tm )
210 /* Is the stamp reasonably long? */
211 for (i=0; isdigit((unsigned char) atm[i]); i++);
212 if (i < sizeof("00000101000000")-1)
216 * parse the time into a struct tm
218 /* 4 digit year to year - 1900 */
219 tm->tm_year = *ptr++ - '0';
220 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
221 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
222 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
224 /* month 01-12 to 0-11 */
225 tm->tm_mon = *ptr++ - '0';
226 tm->tm_mon *=10; tm->tm_mon += *ptr++ - '0';
227 if (tm->tm_mon < 1 || tm->tm_mon > 12) break;
230 /* day of month 01-31 */
231 tm->tm_mday = *ptr++ - '0';
232 tm->tm_mday *=10; tm->tm_mday += *ptr++ - '0';
233 if (tm->tm_mday < 1 || tm->tm_mday > 31) break;
236 tm->tm_hour = *ptr++ - '0';
237 tm->tm_hour *=10; tm->tm_hour += *ptr++ - '0';
238 if (tm->tm_hour < 0 || tm->tm_hour > 23) break;
241 tm->tm_min = *ptr++ - '0';
242 tm->tm_min *=10; tm->tm_min += *ptr++ - '0';
243 if (tm->tm_min < 0 || tm->tm_min > 59) break;
246 tm->tm_sec = *ptr++ - '0';
247 tm->tm_sec *=10; tm->tm_sec += *ptr++ - '0';
248 if (tm->tm_sec < 0 || tm->tm_sec > 61) break;
250 /* Fractions of seconds */
253 for (i = 0, fracs = 0; isdigit((unsigned char) *ptr); ) {
254 i*=10; i+= *ptr++ - '0';
259 for (i = fracs; i<6; i++)
265 if (*ptr != 'Z') break;
272 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
273 * the result string. This allows fast construction of catenated strings
274 * without the overhead of strlen/strcat.
285 while ((*a++ = *b++)) ;
289 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
290 * the result string. This allows fast construction of catenated strings
291 * without the overhead of strlen/strcat.
300 if (!a || !b || n == 0)
303 while ((*a++ = *b++) && n-- > 0) ;
308 int mkstemp( char * template )
311 return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
327 char buf[MAX_PATH+1];
329 DIR *opendir( char *path )
332 int len = strlen(path);
335 WIN32_FIND_DATA data;
337 if (len+3 >= sizeof(tmp))
345 h = FindFirstFile( tmp, &data );
347 if ( h == INVALID_HANDLE_VALUE )
350 d = ber_memalloc( sizeof(DIR) );
354 d->data.d_name = d->buf;
356 strcpy(d->data.d_name, data.cFileName);
359 struct dirent *readdir(DIR *dir)
361 WIN32_FIND_DATA data;
366 if (!FindNextFile(dir->dir, &data))
368 strcpy(dir->data.d_name, data.cFileName);
372 void closedir(DIR *dir)
380 * Memory Reverse Search
383 lutil_memrchr(const void *b, int c, size_t n)
386 const unsigned char *s, *bb = b, cc = c;
388 for ( s = bb + n; s > bb; ) {
399 lutil_atoix( int *v, const char *s, int x )
407 i = strtol( s, &next, x );
408 if ( next == s || next[ 0 ] != '\0' ) {
412 if ( (long)(int)i != i ) {
422 lutil_atoux( unsigned *v, const char *s, int x )
430 /* strtoul() has an odd interface */
431 if ( s[ 0 ] == '-' ) {
435 u = strtoul( s, &next, x );
436 if ( next == s || next[ 0 ] != '\0' ) {
440 if ( (unsigned long)(unsigned)u != u ) {
450 lutil_atolx( long *v, const char *s, int x )
458 l = strtol( s, &next, x );
459 if ( next == s || next[ 0 ] != '\0' ) {
469 lutil_atoulx( unsigned long *v, const char *s, int x )
477 /* strtoul() has an odd interface */
478 if ( s[ 0 ] == '-' ) {
482 ul = strtoul( s, &next, x );
483 if ( next == s || next[ 0 ] != '\0' ) {
492 static char time_unit[] = "dhms";
503 scale[] = { 86400, 3600, 60, 1 };
507 for ( s = (char *)in; s[ 0 ] != '\0'; ) {
511 /* strtoul() has an odd interface */
512 if ( s[ 0 ] == '-' ) {
516 u = strtoul( s, &next, 10 );
521 if ( next[ 0 ] == '\0' ) {
527 what = strchr( time_unit, next[ 0 ] );
528 if ( what == NULL ) {
532 if ( what - time_unit <= sofar ) {
536 sofar = what - time_unit;
537 t += u * scale[ sofar ];
553 unsigned long v[ 4 ];
557 v[ 1 ] = (t%86400)/3600;
558 v[ 2 ] = (t%3600)/60;
561 for ( i = 0; i < 4; i++ ) {
562 if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
563 len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
564 if ( len < 0 || (unsigned)len >= buflen ) {