void __kmp_str_buf_cat( kmp_str_buf_t * buffer, char const * str, int len ) { KMP_STR_BUF_INVARIANT( buffer ); KMP_DEBUG_ASSERT( str != NULL ); KMP_DEBUG_ASSERT( len >= 0 ); __kmp_str_buf_reserve( buffer, buffer->used + len + 1 ); memcpy( buffer->str + buffer->used, str, len ); buffer->str[ buffer->used + len ] = 0; buffer->used += len; KMP_STR_BUF_INVARIANT( buffer ); } // __kmp_str_buf_cat
void __kmp_str_buf_vprint( kmp_str_buf_t * buffer, char const * format, va_list args ) { KMP_STR_BUF_INVARIANT( buffer ); for ( ; ; ) { int const free = buffer->size - buffer->used; int rc; int size; // Try to format string. { /* On Linux* OS Intel(R) 64, vsnprintf() modifies args argument, so vsnprintf() crashes if it is called for the second time with the same args. To prevent the crash, we have to pass a fresh intact copy of args to vsnprintf() on each iteration. Unfortunately, standard va_copy() macro is not available on Windows* OS. However, it seems vsnprintf() does not modify args argument on Windows* OS. */ #if ! KMP_OS_WINDOWS va_list _args; __va_copy( _args, args ); // Make copy of args. #define args _args // Substitute args with its copy, _args. #endif // KMP_OS_WINDOWS rc = vsnprintf( buffer->str + buffer->used, free, format, args ); #if ! KMP_OS_WINDOWS #undef args // Remove substitution. va_end( _args ); #endif // KMP_OS_WINDOWS } // No errors, string has been formatted. if ( rc >= 0 && rc < free ) { buffer->used += rc; break; }; // if // Error occured, buffer is too small. if ( rc >= 0 ) { // C99-conforming implementation of vsnprintf returns required buffer size. size = buffer->used + rc + 1; } else { // Older implementations just return -1. Double buffer size. size = buffer->size * 2; }; // if // Enlarge buffer. __kmp_str_buf_reserve( buffer, size ); // And try again. }; // forever KMP_DEBUG_ASSERT( buffer->size > 0 ); KMP_STR_BUF_INVARIANT( buffer ); } // __kmp_str_buf_vprint
void __kmp_i18n_do_catopen( ) { LCID locale_id = GetThreadLocale(); WORD lang_id = LANGIDFROMLCID( locale_id ); WORD primary_lang_id = PRIMARYLANGID( lang_id ); kmp_str_buf_t path; KMP_DEBUG_ASSERT( status == KMP_I18N_CLOSED ); KMP_DEBUG_ASSERT( cat == KMP_I18N_NULLCAT ); __kmp_str_buf_init( & path ); // Do not try to open English catalog because internal messages are // exact copy of messages in English catalog. if ( primary_lang_id == LANG_ENGLISH ) { status = KMP_I18N_ABSENT; // mark catalog as absent so it will not be re-opened. goto end; }; // if // Construct resource DLL name. /* Simple LoadLibrary( name ) is not suitable due to security issue (see http://www.microsoft.com/technet/security/advisory/2269637.mspx). We have to specify full path to the message catalog. */ { // Get handle of our DLL first. HMODULE handle; BOOL brc = GetModuleHandleEx( GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast< LPCSTR >( & __kmp_i18n_do_catopen ), & handle ); if ( ! brc ) { // Error occurred. status = KMP_I18N_ABSENT; // mark catalog as absent so it will not be re-opened. goto end; // TODO: Enable multiple messages (KMP_MSG) to be passed to __kmp_msg; and print // a proper warning. }; // if // Now get path to the our DLL. for ( ; ; ) { DWORD drc = GetModuleFileName( handle, path.str, path.size ); if ( drc == 0 ) { // Error occurred. status = KMP_I18N_ABSENT; goto end; }; // if if ( drc < path.size ) { path.used = drc; break; }; // if __kmp_str_buf_reserve( & path, path.size * 2 ); }; // forever // Now construct the name of message catalog. kmp_str_fname fname; __kmp_str_fname_init( & fname, path.str ); __kmp_str_buf_clear( & path ); __kmp_str_buf_print( & path, "%s%lu/%s", fname.dir, (unsigned long)( locale_id ), name ); __kmp_str_fname_free( & fname ); } // For security reasons, use LoadLibraryEx() and load message catalog as a data file. cat = LoadLibraryEx( path.str, NULL, LOAD_LIBRARY_AS_DATAFILE ); status = ( cat == KMP_I18N_NULLCAT ? KMP_I18N_ABSENT : KMP_I18N_OPENED ); if ( status == KMP_I18N_ABSENT ) { if (__kmp_generate_warnings > kmp_warnings_low) { // AC: only issue warning in case explicitly asked to DWORD error = GetLastError(); // Infinite recursion will not occur -- status is KMP_I18N_ABSENT now, so // __kmp_i18n_catgets() will not try to open catalog but will return default message. /* If message catalog for another architecture found (e.g. OpenMP RTL for IA-32 architecture opens libompui.dll for Intel(R) 64) Windows* OS returns error 193 (ERROR_BAD_EXE_FORMAT). However, FormatMessage fails to return a message for this error, so user will see: OMP: Warning #2: Cannot open message catalog "1041\libompui.dll": OMP: System error #193: (No system error message available) OMP: Info #3: Default messages will be used. Issue a hint in this case to let cause of trouble more understandable. */ __kmp_msg( kmp_ms_warning, KMP_MSG( CantOpenMessageCatalog, path.str ), KMP_SYSERRCODE( error ), ( error == ERROR_BAD_EXE_FORMAT ? KMP_HNT( BadExeFormat, path.str, KMP_ARCH_STR ) : __kmp_msg_null ), __kmp_msg_null ); KMP_INFORM( WillUseDefaultMessages ); } } else { // status == KMP_I18N_OPENED int section = get_section( kmp_i18n_prp_Version ); int number = get_number( kmp_i18n_prp_Version ); char const * expected = __kmp_i18n_default_table.sect[ section ].str[ number ]; kmp_str_buf_t version; // Actual version of the catalog. __kmp_str_buf_init( & version ); __kmp_str_buf_print( & version, "%s", ___catgets( kmp_i18n_prp_Version ) ); // String returned by catgets is invalid after closing the catalog, so copy it. if ( strcmp( version.str, expected ) != 0 ) { // Close bad catalog. __kmp_i18n_catclose(); status = KMP_I18N_ABSENT; // And mark it as absent. if (__kmp_generate_warnings > kmp_warnings_low) { // And now print a warning using default messages. __kmp_msg( kmp_ms_warning, KMP_MSG( WrongMessageCatalog, path.str, version.str, expected ), __kmp_msg_null ); KMP_INFORM( WillUseDefaultMessages ); } // __kmp_generate_warnings }; // if __kmp_str_buf_free( & version ); }; // if code_page = get_code_page(); end: __kmp_str_buf_free( & path ); return; } // func __kmp_i18n_do_catopen