X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblutil%2Futils.c;h=0fdd00027ca4ddae0a42d9f6ca1ce26203b11e1a;hb=9d2e03f0527b31ac2fc994fddb7d858d010d566d;hp=0be045a361276248bb832babcd8965785a438bfd;hpb=4af9eb971559e3a1f0432615e93ec870dc753ddb;p=openldap diff --git a/libraries/liblutil/utils.c b/libraries/liblutil/utils.c index 0be045a361..0fdd00027c 100644 --- a/libraries/liblutil/utils.c +++ b/libraries/liblutil/utils.c @@ -1,7 +1,7 @@ /* $OpenLDAP$ */ /* This work is part of OpenLDAP Software . * - * Copyright 1998-2009 The OpenLDAP Foundation. + * Copyright 1998-2010 The OpenLDAP Foundation. * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -282,128 +282,6 @@ int lutil_parsetime( char *atm, struct lutil_tm *tm ) return -1; } -/* return a broken out time, with microseconds - * Must be mutex-protected. - */ -#ifdef _WIN32 -/* Windows SYSTEMTIME only has 10 millisecond resolution, so we - * also need to use a high resolution timer to get microseconds. - * This is pretty clunky. - */ -void -lutil_gettime( struct lutil_tm *tm ) -{ - static LARGE_INTEGER cFreq; - static LARGE_INTEGER prevCount; - static int subs; - static int offset; - LARGE_INTEGER count; - SYSTEMTIME st; - - GetSystemTime( &st ); - QueryPerformanceCounter( &count ); - - /* It shouldn't ever go backwards, but multiple CPUs might - * be able to hit in the same tick. - */ - if ( count.QuadPart <= prevCount.QuadPart ) { - subs++; - } else { - subs = 0; - prevCount = count; - } - - /* We assume Windows has at least a vague idea of - * when a second begins. So we align our microsecond count - * with the Windows millisecond count using this offset. - * We retain the submillisecond portion of our own count. - * - * Note - this also assumes that the relationship between - * the PerformanceCouunter and SystemTime stays constant; - * that assumption breaks if the SystemTime is adjusted by - * an external action. - */ - if ( !cFreq.QuadPart ) { - long long t; - int usec; - QueryPerformanceFrequency( &cFreq ); - - /* just get sub-second portion of counter */ - t = count.QuadPart % cFreq.QuadPart; - - /* convert to microseconds */ - t *= 1000000; - usec = t / cFreq.QuadPart; - - offset = usec - st.wMilliseconds * 1000; - } - - tm->tm_usub = subs; - - /* convert to microseconds */ - count.QuadPart %= cFreq.QuadPart; - count.QuadPart *= 1000000; - count.QuadPart /= cFreq.QuadPart; - count.QuadPart -= offset; - - tm->tm_usec = count.QuadPart % 1000000; - if ( tm->tm_usec < 0 ) - tm->tm_usec += 1000000; - - /* any difference larger than microseconds is - * already reflected in st - */ - - tm->tm_sec = st.wSecond; - tm->tm_min = st.wMinute; - tm->tm_hour = st.wHour; - tm->tm_mday = st.wDay; - tm->tm_mon = st.wMonth - 1; - tm->tm_year = st.wYear - 1900; -} -#else -void -lutil_gettime( struct lutil_tm *ltm ) -{ - struct timeval tv; - static struct timeval prevTv; - static int subs; - -#ifdef HAVE_GMTIME_R - struct tm tm_buf; -#endif - struct tm *tm; - time_t t; - - gettimeofday( &tv, NULL ); - t = tv.tv_sec; - - if ( tv.tv_sec < prevTv.tv_sec - || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec == prevTv.tv_usec )) { - subs++; - } else { - subs = 0; - prevTv = tv; - } - - ltm->tm_usub = subs; - -#ifdef HAVE_GMTIME_R - tm = gmtime_r( &t, &tm_buf ); -#else - tm = gmtime( &t ); -#endif - - ltm->tm_sec = tm->tm_sec; - ltm->tm_min = tm->tm_min; - ltm->tm_hour = tm->tm_hour; - ltm->tm_mday = tm->tm_mday; - ltm->tm_mon = tm->tm_mon; - ltm->tm_year = tm->tm_year; - ltm->tm_usec = tv.tv_usec; -} -#endif - /* 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. @@ -466,6 +344,40 @@ int mkstemp( char * template ) #endif #ifdef _MSC_VER +/* Equivalent of MS CRT's _dosmaperr(). + * @param lastError[in] Result of GetLastError(). + */ +static errno_t win2errno(DWORD lastError) +{ + const struct { + DWORD windows_code; + errno_t errno_code; + } WIN2ERRNO_TABLE[] = { + { ERROR_SUCCESS, 0 }, + { ERROR_FILE_NOT_FOUND, ENOENT }, + { ERROR_PATH_NOT_FOUND, ENOENT }, + { ERROR_TOO_MANY_OPEN_FILES, EMFILE }, + { ERROR_ACCESS_DENIED, EACCES }, + { ERROR_INVALID_HANDLE, EBADF }, + { ERROR_NOT_ENOUGH_MEMORY, ENOMEM }, + { ERROR_LOCK_VIOLATION, EACCES }, + { ERROR_FILE_EXISTS, EEXIST }, + { ERROR_INVALID_PARAMETER, EINVAL }, + { ERROR_FILENAME_EXCED_RANGE, ENAMETOOLONG }, + }; + const unsigned int WIN2ERRNO_TABLE_SIZE = sizeof(WIN2ERRNO_TABLE) / +sizeof(WIN2ERRNO_TABLE[0]); + const errno_t DEFAULT_ERRNO_ERROR = -1; + unsigned int i; + + for (i = 0; i < WIN2ERRNO_TABLE_SIZE; ++i) { + if (WIN2ERRNO_TABLE[i].windows_code == lastError) { + return WIN2ERRNO_TABLE[i].errno_code; + } + } + return DEFAULT_ERRNO_ERROR; +} + struct dirent { char *d_name; }; @@ -483,8 +395,10 @@ DIR *opendir( char *path ) HANDLE h; WIN32_FIND_DATA data; - if (len+3 >= sizeof(tmp)) + if (len+3 >= sizeof(tmp)) { + errno = ENAMETOOLONG; return NULL; + } strcpy(tmp, path); tmp[len++] = '\\'; @@ -492,9 +406,11 @@ DIR *opendir( char *path ) tmp[len] = '\0'; h = FindFirstFile( tmp, &data ); - - if ( h == INVALID_HANDLE_VALUE ) + + if ( h == INVALID_HANDLE_VALUE ) { + errno = win2errno( GetLastError()); return NULL; + } d = ber_memalloc( sizeof(DIR) ); if ( !d ) @@ -518,7 +434,7 @@ struct dirent *readdir(DIR *dir) } return &dir->data; } -void closedir(DIR *dir) +int closedir(DIR *dir) { FindClose(dir->dir); ber_memfree(dir); @@ -529,7 +445,7 @@ void closedir(DIR *dir) * Memory Reverse Search */ void * -lutil_memrchr(const void *b, int c, size_t n) +(lutil_memrchr)(const void *b, int c, size_t n) { if (n != 0) { const unsigned char *s, *bb = b, cc = c;