1
/* $OpenLDAP: pkg/ldap/libraries/liblutil/utils.c,v 1.33.2.17 2008/02/11 23:26:42 kurt Exp $ */
1
/* $OpenLDAP: pkg/ldap/libraries/liblutil/utils.c,v 1.33.2.23 2009/01/22 00:00:58 kurt Exp $ */
2
2
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
* Copyright 1998-2008 The OpenLDAP Foundation.
4
* Copyright 1998-2009 The OpenLDAP Foundation.
5
5
* All rights reserved.
7
7
* Redistribution and use in source and binary forms, with or without
77
77
LUTIL_SLASHPATH( argv[0] );
78
78
progname = strrchr ( argv[0], *LDAP_DIRSEP );
79
79
progname = progname ? &progname[1] : argv[0];
82
size_t len = strlen( progname );
83
if ( len > 4 && strcasecmp( &progname[len - 4], ".exe" ) == 0 )
84
progname[len - 4] = '\0';
296
303
GetSystemTime( &st );
297
304
QueryPerformanceCounter( &count );
306
/* It shouldn't ever go backwards, but multiple CPUs might
307
* be able to hit in the same tick.
309
if ( count.QuadPart <= prevCount.QuadPart ) {
299
316
/* We assume Windows has at least a vague idea of
300
317
* when a second begins. So we align our microsecond count
301
318
* with the Windows millisecond count using this offset.
302
319
* We retain the submillisecond portion of our own count.
321
* Note - this also assumes that the relationship between
322
* the PerformanceCouunter and SystemTime stays constant;
323
* that assumption breaks if the SystemTime is adjusted by
324
* an external action.
304
326
if ( !cFreq.QuadPart ) {
307
329
QueryPerformanceFrequency( &cFreq );
309
t = count.QuadPart * 1000000;
313
offset = ( usec - st.wMilliseconds ) * 1000;
316
/* It shouldn't ever go backwards, but multiple CPUs might
317
* be able to hit in the same tick.
319
if ( count.QuadPart <= prevCount.QuadPart ) {
331
/* just get sub-second portion of counter */
332
t = count.QuadPart % cFreq.QuadPart;
334
/* convert to microseconds */
336
usec = t / cFreq.QuadPart;
338
offset = usec - st.wMilliseconds * 1000;
326
341
tm->tm_usub = subs;
328
343
/* convert to microseconds */
344
count.QuadPart %= cFreq.QuadPart;
329
345
count.QuadPart *= 1000000;
330
346
count.QuadPart /= cFreq.QuadPart;
331
347
count.QuadPart -= offset;
333
349
tm->tm_usec = count.QuadPart % 1000000;
350
if ( tm->tm_usec < 0 )
351
tm->tm_usec += 1000000;
335
353
/* any difference larger than microseconds is
336
354
* already reflected in st
442
/* memcopy is like memcpy except it returns a pointer to the byte past
443
* the end of the result buffer, set to NULL. This allows fast construction
444
* of catenated buffers. Provided for API consistency with lutil_str*copy().
424
457
#ifndef HAVE_MKSTEMP
425
458
int mkstemp( char * template )
702
734
#define HEXMAX (2 * sizeof(long))
703
736
/* Convert a longword at a time, but handle leading
704
737
* odd bytes first
706
chunk = len & (HEXMAX-1);
739
chunk = len % HEXMAX;
711
745
ctmp = pin[chunk];
712
746
pin[chunk] = '\0';
714
l = strtol( pin, &end, 16 );
748
l = strtoul( pin, &end, 16 );
715
749
pin[chunk] = ctmp;
720
for ( i = chunk; i>=0; i-- ) {
752
ochunk = (chunk + 1)/2;
753
for ( i = ochunk - 1; i >= 0; i-- ) {
721
754
pout[i] = l & 0xff;
725
pout += sizeof(long);
729
out->bv_len = pout + len - out->bv_val;
762
out->bv_len = pout - out->bv_val;
732
765
char tmpbuf[64], *tmp;
733
766
lutil_int_decnum num;
736
770
len = in->bv_len;
737
771
pin = in->bv_val;