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>
35 #include "ldap_defaults.h"
43 /* Some Windows versions accept both forward and backslashes in
44 * directory paths, but we always use backslashes when generating
47 void lutil_slashpath( char *path )
52 while (( c=strchr( p, '/' ))) {
59 char* lutil_progname( const char* name, int argc, char *argv[] )
70 for (i=0; i<argc; i++) __etoa(argv[i]);
74 LUTIL_SLASHPATH( argv[0] );
75 progname = strrchr ( argv[0], *LDAP_DIRSEP );
76 progname = progname ? &progname[1] : argv[0];
81 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
85 /* We've been compiling in ASCII so far, but we want EBCDIC now since
86 * strftime only understands EBCDIC input.
88 #pragma convlit(suspend)
90 ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
92 #pragma convlit(resume)
99 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
104 if ( smax < 16 ) { /* YYYYmmddHHMMSSZ */
109 /* We've been compiling in ASCII so far, but we want EBCDIC now since
110 * strftime only understands EBCDIC input.
112 #pragma convlit(suspend)
114 ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
116 #pragma convlit(resume)
119 if ( delta == 0 || ret == 0 ) {
123 if ( smax < 20 ) { /* YYYYmmddHHMMSS+HHMM */
137 snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
138 ( delta % 3600 ) / 60 );
143 int lutil_tm2time( struct lutil_tm *tm, struct lutil_timet *tt )
145 static int moffset[12] = {
151 tt->tt_usec = tm->tm_usec;
153 /* special case 0000/01/01+00:00:00 is returned as zero */
154 if ( tm->tm_year == -1900 && tm->tm_mon == 0 && tm->tm_mday == 1 &&
155 tm->tm_hour == 0 && tm->tm_min == 0 && tm->tm_sec == 0 ) {
161 /* tm->tm_year is years since 1900 */
162 /* calculate days from years since 1970 (epoch) */
163 tt->tt_sec = tm->tm_year - 70;
166 /* count leap days in preceding years */
167 tt->tt_sec += ((tm->tm_year -69) >> 2);
169 /* calculate days from months */
170 tt->tt_sec += moffset[tm->tm_mon];
172 /* add in this year's leap day, if any */
173 if (((tm->tm_year & 3) == 0) && (tm->tm_mon > 1)) {
177 /* add in days in this month */
178 tt->tt_sec += (tm->tm_mday - 1);
180 /* this function can handle a range of about 17408 years... */
181 /* 86400 seconds in a day, divided by 128 = 675 */
184 /* move high 7 bits into tt_gsec */
185 tt->tt_gsec = tt->tt_sec >> 25;
186 tt->tt_sec -= tt->tt_gsec << 25;
191 /* convert to minutes */
195 /* convert to seconds */
199 /* add remaining seconds */
207 int lutil_parsetime( char *atm, struct lutil_tm *tm )
213 /* Is the stamp reasonably long? */
214 for (i=0; isdigit((unsigned char) atm[i]); i++);
215 if (i < sizeof("00000101000000")-1)
219 * parse the time into a struct tm
221 /* 4 digit year to year - 1900 */
222 tm->tm_year = *ptr++ - '0';
223 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
224 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
225 tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
227 /* month 01-12 to 0-11 */
228 tm->tm_mon = *ptr++ - '0';
229 tm->tm_mon *=10; tm->tm_mon += *ptr++ - '0';
230 if (tm->tm_mon < 1 || tm->tm_mon > 12) break;
233 /* day of month 01-31 */
234 tm->tm_mday = *ptr++ - '0';
235 tm->tm_mday *=10; tm->tm_mday += *ptr++ - '0';
236 if (tm->tm_mday < 1 || tm->tm_mday > 31) break;
239 tm->tm_hour = *ptr++ - '0';
240 tm->tm_hour *=10; tm->tm_hour += *ptr++ - '0';
241 if (tm->tm_hour < 0 || tm->tm_hour > 23) break;
244 tm->tm_min = *ptr++ - '0';
245 tm->tm_min *=10; tm->tm_min += *ptr++ - '0';
246 if (tm->tm_min < 0 || tm->tm_min > 59) break;
249 tm->tm_sec = *ptr++ - '0';
250 tm->tm_sec *=10; tm->tm_sec += *ptr++ - '0';
251 if (tm->tm_sec < 0 || tm->tm_sec > 61) break;
253 /* Fractions of seconds */
256 for (i = 0, fracs = 0; isdigit((unsigned char) *ptr); ) {
257 i*=10; i+= *ptr++ - '0';
262 for (i = fracs; i<6; i++)
268 if (*ptr != 'Z') break;
275 /* return a broken out time, with microseconds
276 * Must be mutex-protected.
279 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
280 * also need to use a high resolution timer to get microseconds.
281 * This is pretty clunky.
284 lutil_gettime( struct lutil_tm *tm )
286 static LARGE_INTEGER cFreq;
287 static LARGE_INTEGER prevCount;
293 GetSystemTime( &st );
294 QueryPerformanceCounter( &count );
296 /* We assume Windows has at least a vague idea of
297 * when a second begins. So we align our microsecond count
298 * with the Windows millisecond count using this offset.
299 * We retain the submillisecond portion of our own count.
301 if ( !cFreq.QuadPart ) {
304 QueryPerformanceFrequency( &cFreq );
306 t = count.QuadPart * 1000000;
310 offset = ( usec - st.wMilliseconds ) * 1000;
313 /* It shouldn't ever go backwards, but multiple CPUs might
314 * be able to hit in the same tick.
316 if ( count.QuadPart <= prevCount.QuadPart ) {
325 /* convert to microseconds */
326 count.QuadPart *= 1000000;
327 count.QuadPart /= cFreq.QuadPart;
328 count.QuadPart -= offset;
330 tm->tm_usec = count.QuadPart % 1000000;
332 /* any difference larger than microseconds is
333 * already reflected in st
336 tm->tm_sec = st.wSecond;
337 tm->tm_min = st.wMinute;
338 tm->tm_hour = st.wHour;
339 tm->tm_mday = st.wDay;
340 tm->tm_mon = st.wMonth - 1;
341 tm->tm_year = st.wYear - 1900;
345 lutil_gettime( struct lutil_tm *ltm )
348 static struct timeval prevTv;
357 gettimeofday( &tv, NULL );
360 if ( tv.tv_sec < prevTv.tv_sec
361 || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec == prevTv.tv_usec )) {
371 tm = gmtime_r( &t, &tm_buf );
376 ltm->tm_sec = tm->tm_sec;
377 ltm->tm_min = tm->tm_min;
378 ltm->tm_hour = tm->tm_hour;
379 ltm->tm_mday = tm->tm_mday;
380 ltm->tm_mon = tm->tm_mon;
381 ltm->tm_year = tm->tm_year;
382 ltm->tm_usec = tv.tv_usec;
386 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
387 * the result string. This allows fast construction of catenated strings
388 * without the overhead of strlen/strcat.
399 while ((*a++ = *b++)) ;
403 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
404 * the result string. This allows fast construction of catenated strings
405 * without the overhead of strlen/strcat.
414 if (!a || !b || n == 0)
417 while ((*a++ = *b++) && n-- > 0) ;
422 int mkstemp( char * template )
425 return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
440 char buf[MAX_PATH+1];
442 DIR *opendir( char *path )
445 int len = strlen(path);
448 WIN32_FIND_DATA data;
450 if (len+3 >= sizeof(tmp))
458 h = FindFirstFile( tmp, &data );
460 if ( h == INVALID_HANDLE_VALUE )
463 d = ber_memalloc( sizeof(DIR) );
467 d->data.d_name = d->buf;
469 strcpy(d->data.d_name, data.cFileName);
472 struct dirent *readdir(DIR *dir)
474 WIN32_FIND_DATA data;
479 if (!FindNextFile(dir->dir, &data))
481 strcpy(dir->data.d_name, data.cFileName);
485 void closedir(DIR *dir)
493 * Memory Reverse Search
496 lutil_memrchr(const void *b, int c, size_t n)
499 const unsigned char *s, *bb = b, cc = c;
501 for ( s = bb + n; s > bb; ) {
512 lutil_atoix( int *v, const char *s, int x )
520 i = strtol( s, &next, x );
521 if ( next == s || next[ 0 ] != '\0' ) {
525 if ( (long)(int)i != i ) {
535 lutil_atoux( unsigned *v, const char *s, int x )
543 /* strtoul() has an odd interface */
544 if ( s[ 0 ] == '-' ) {
548 u = strtoul( s, &next, x );
549 if ( next == s || next[ 0 ] != '\0' ) {
553 if ( (unsigned long)(unsigned)u != u ) {
563 lutil_atolx( long *v, const char *s, int x )
571 l = strtol( s, &next, x );
572 if ( next == s || next[ 0 ] != '\0' ) {
582 lutil_atoulx( unsigned long *v, const char *s, int x )
590 /* strtoul() has an odd interface */
591 if ( s[ 0 ] == '-' ) {
595 ul = strtoul( s, &next, x );
596 if ( next == s || next[ 0 ] != '\0' ) {
605 static char time_unit[] = "dhms";
607 /* Used to parse and unparse time intervals, not timestamps */
617 scale[] = { 86400, 3600, 60, 1 };
621 for ( s = (char *)in; s[ 0 ] != '\0'; ) {
625 /* strtoul() has an odd interface */
626 if ( s[ 0 ] == '-' ) {
630 u = strtoul( s, &next, 10 );
635 if ( next[ 0 ] == '\0' ) {
641 what = strchr( time_unit, next[ 0 ] );
642 if ( what == NULL ) {
646 if ( what - time_unit <= sofar ) {
650 sofar = what - time_unit;
651 t += u * scale[ sofar ];
667 unsigned long v[ 4 ];
671 v[ 1 ] = (t%86400)/3600;
672 v[ 2 ] = (t%3600)/60;
675 for ( i = 0; i < 4; i++ ) {
676 if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
677 len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
678 if ( len < 0 || (unsigned)len >= buflen ) {