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 */
277 /* Windows SYSTEMTIME only has 10 millisecond resolution, so we
278 * also need to use a high resolution timer to get microseconds.
279 * This is pretty clunky.
282 lutil_gettime( struct lutil_tm *tm )
284 static LARGE_INTEGER cFreq;
289 GetSystemTime( &st );
290 QueryPerformanceCounter( &count );
292 /* We assume Windows has at least a vague idea of
293 * when a second begins. So we align our microsecond count
294 * with the Windows millisecond count using this offset.
295 * We retain the submillisecond portion of our own count.
297 if ( !cFreq.QuadPart ) {
300 QueryPerformanceFrequency( &cFreq );
302 t = count.QuadPart * 1000000;
306 offset = ( usec - st.wMilliseconds ) * 1000;
309 /* convert to microseconds */
310 count.QuadPart *= 1000000;
311 count.QuadPart /= cFreq.QuadPart;
312 count.QuadPart -= offset;
314 tm->tm_usec = count.QuadPart % 1000000;
316 /* any difference larger than microseconds is
317 * already reflected in st
320 tm->tm_sec = st.wSecond;
321 tm->tm_min = st.wMinute;
322 tm->tm_hour = st.wHour;
323 tm->tm_mday = st.wDay;
324 tm->tm_mon = st.wMonth - 1;
325 tm->tm_year = st.wYear - 1900;
329 lutil_gettime( struct lutil_tm *ltm )
338 gettimeofday( &tv, NULL );
342 tm = gmtime_r( &t, &tm_buf );
347 ltm->tm_sec = tm->tm_sec;
348 ltm->tm_min = tm->tm_min;
349 ltm->tm_hour = tm->tm_hour;
350 ltm->tm_mday = tm->tm_mday;
351 ltm->tm_mon = tm->tm_mon;
352 ltm->tm_year = tm->tm_year;
353 ltm->tm_usec = tv.tv_usec;
357 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
358 * the result string. This allows fast construction of catenated strings
359 * without the overhead of strlen/strcat.
370 while ((*a++ = *b++)) ;
374 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
375 * the result string. This allows fast construction of catenated strings
376 * without the overhead of strlen/strcat.
385 if (!a || !b || n == 0)
388 while ((*a++ = *b++) && n-- > 0) ;
393 int mkstemp( char * template )
396 return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
411 char buf[MAX_PATH+1];
413 DIR *opendir( char *path )
416 int len = strlen(path);
419 WIN32_FIND_DATA data;
421 if (len+3 >= sizeof(tmp))
429 h = FindFirstFile( tmp, &data );
431 if ( h == INVALID_HANDLE_VALUE )
434 d = ber_memalloc( sizeof(DIR) );
438 d->data.d_name = d->buf;
440 strcpy(d->data.d_name, data.cFileName);
443 struct dirent *readdir(DIR *dir)
445 WIN32_FIND_DATA data;
450 if (!FindNextFile(dir->dir, &data))
452 strcpy(dir->data.d_name, data.cFileName);
456 void closedir(DIR *dir)
464 * Memory Reverse Search
467 lutil_memrchr(const void *b, int c, size_t n)
470 const unsigned char *s, *bb = b, cc = c;
472 for ( s = bb + n; s > bb; ) {
483 lutil_atoix( int *v, const char *s, int x )
491 i = strtol( s, &next, x );
492 if ( next == s || next[ 0 ] != '\0' ) {
496 if ( (long)(int)i != i ) {
506 lutil_atoux( unsigned *v, const char *s, int x )
514 /* strtoul() has an odd interface */
515 if ( s[ 0 ] == '-' ) {
519 u = strtoul( s, &next, x );
520 if ( next == s || next[ 0 ] != '\0' ) {
524 if ( (unsigned long)(unsigned)u != u ) {
534 lutil_atolx( long *v, const char *s, int x )
542 l = strtol( s, &next, x );
543 if ( next == s || next[ 0 ] != '\0' ) {
553 lutil_atoulx( unsigned long *v, const char *s, int x )
561 /* strtoul() has an odd interface */
562 if ( s[ 0 ] == '-' ) {
566 ul = strtoul( s, &next, x );
567 if ( next == s || next[ 0 ] != '\0' ) {
576 static char time_unit[] = "dhms";
578 /* Used to parse and unparse time intervals, not timestamps */
588 scale[] = { 86400, 3600, 60, 1 };
592 for ( s = (char *)in; s[ 0 ] != '\0'; ) {
596 /* strtoul() has an odd interface */
597 if ( s[ 0 ] == '-' ) {
601 u = strtoul( s, &next, 10 );
606 if ( next[ 0 ] == '\0' ) {
612 what = strchr( time_unit, next[ 0 ] );
613 if ( what == NULL ) {
617 if ( what - time_unit <= sofar ) {
621 sofar = what - time_unit;
622 t += u * scale[ sofar ];
638 unsigned long v[ 4 ];
642 v[ 1 ] = (t%86400)/3600;
643 v[ 2 ] = (t%3600)/60;
646 for ( i = 0; i < 4; i++ ) {
647 if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
648 len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
649 if ( len < 0 || (unsigned)len >= buflen ) {