Beispiel #1
0
char *__kmp_env_get(char const *name) {

  char *result = NULL;

#if KMP_OS_UNIX
  char const *value = getenv(name);
  if (value != NULL) {
    size_t len = KMP_STRLEN(value) + 1;
    result = (char *)KMP_INTERNAL_MALLOC(len);
    if (result == NULL) {
      KMP_FATAL(MemoryAllocFailed);
    }
    KMP_STRNCPY_S(result, len, value, len);
  }
#elif KMP_OS_WINDOWS
  /* We use GetEnvironmentVariable for Windows* OS instead of getenv because the
     act of loading a DLL on Windows* OS makes any user-set environment
     variables (i.e. with putenv()) unavailable. getenv() apparently gets a
     clean copy of the env variables as they existed at the start of the run.
     JH 12/23/2002 */
  DWORD rc;
  rc = GetEnvironmentVariable(name, NULL, 0);
  if (!rc) {
    DWORD error = GetLastError();
    if (error != ERROR_ENVVAR_NOT_FOUND) {
      __kmp_fatal(KMP_MSG(CantGetEnvVar, name), KMP_ERR(error), __kmp_msg_null);
    }
    // Variable is not found, it's ok, just continue.
  } else {
    DWORD len = rc;
    result = (char *)KMP_INTERNAL_MALLOC(len);
    if (result == NULL) {
      KMP_FATAL(MemoryAllocFailed);
    }
    rc = GetEnvironmentVariable(name, result, len);
    if (!rc) {
      // GetEnvironmentVariable() may return 0 if variable is empty.
      // In such a case GetLastError() returns ERROR_SUCCESS.
      DWORD error = GetLastError();
      if (error != ERROR_SUCCESS) {
        // Unexpected error. The variable should be in the environment,
        // and buffer should be large enough.
        __kmp_fatal(KMP_MSG(CantGetEnvVar, name), KMP_ERR(error),
                    __kmp_msg_null);
        KMP_INTERNAL_FREE((void *)result);
        result = NULL;
      }
    }
  }
#else
#error Unknown or unsupported OS.
#endif

  return result;

} // func __kmp_env_get
void
__kmp_env_set( char const * name, char const * value, int overwrite ) {

    #if KMP_OS_UNIX
        int rc = setenv( name, value, overwrite );
        if ( rc != 0 ) {
            // Dead code. I tried to put too many variables into Linux* OS
            // environment on IA-32 architecture. When application consumes
            // more than ~2.5 GB of memory, entire system feels bad. Sometimes
            // application is killed (by OS?), sometimes system stops 
            // responding... But this error message never appears. --ln
            __kmp_msg(
                kmp_ms_fatal,
                KMP_MSG( CantSetEnvVar, name ),
                KMP_HNT( NotEnoughMemory ),
                __kmp_msg_null
            );
        }; // if
    #elif KMP_OS_WINDOWS
        BOOL rc;
        if ( ! overwrite ) {
            rc = GetEnvironmentVariable( name, NULL, 0 );
            if ( rc ) {
                // Variable exists, do not overwrite.
                return;
            }; // if
            DWORD error = GetLastError();
            if ( error != ERROR_ENVVAR_NOT_FOUND ) {
                __kmp_msg(
                    kmp_ms_fatal,
                    KMP_MSG( CantGetEnvVar, name ),
                    KMP_ERR( error ),
                    __kmp_msg_null
                );
            }; // if
        }; // if
        rc = SetEnvironmentVariable( name, value );
        if ( ! rc ) {
            DWORD error = GetLastError();
            __kmp_msg(
                kmp_ms_fatal,
                KMP_MSG( CantSetEnvVar, name ),
                KMP_ERR( error ),
                __kmp_msg_null
            );
        }; // if
    #else
        #error Unknown or unsupported OS.
    #endif

} // func __kmp_env_set
Beispiel #3
0
void __kmp_env_blk_init(kmp_env_blk_t *block, // M: Block to initialize.
                        char const *bulk // I: Initialization string, or NULL.
                        ) {

  if (bulk != NULL) {
    ___kmp_env_blk_parse_string(block, bulk);
  } else {
#if KMP_OS_UNIX
    ___kmp_env_blk_parse_unix(block, environ);
#elif KMP_OS_WINDOWS
    {
      char *mem = GetEnvironmentStrings();
      if (mem == NULL) {
        DWORD error = GetLastError();
        __kmp_fatal(KMP_MSG(CantGetEnvironment), KMP_ERR(error),
                    __kmp_msg_null);
      }
      ___kmp_env_blk_parse_windows(block, mem);
      FreeEnvironmentStrings(mem);
    }
#else
#error Unknown or unsupported OS.
#endif
  }

} // __kmp_env_blk_init
int
__kmp_env_exists( char const * name ) {

    #if KMP_OS_UNIX
        char const * value = getenv( name );
        return ( ( value == NULL ) ? ( 0 ) : ( 1 ) );
    #elif KMP_OS_WINDOWS
        DWORD rc;
        rc = GetEnvironmentVariable( name, NULL, 0 );
        if ( rc == 0 ) {
            DWORD error = GetLastError();
            if ( error != ERROR_ENVVAR_NOT_FOUND ) {
                __kmp_msg(
                    kmp_ms_fatal,
                    KMP_MSG( CantGetEnvVar, name ),
                    KMP_ERR( error ),
                    __kmp_msg_null
                );
            }; // if
            return 0;
        }; // if
        return 1;
    #else
        #error Unknown or unsupported OS.
    #endif

} // func __kmp_env_exists
Beispiel #5
0
void __kmp_env_unset(char const *name) {

#if KMP_OS_UNIX
  unsetenv(name);
#elif KMP_OS_WINDOWS
  BOOL rc = SetEnvironmentVariable(name, NULL);
  if (!rc) {
    DWORD error = GetLastError();
    __kmp_fatal(KMP_MSG(CantSetEnvVar, name), KMP_ERR(error), __kmp_msg_null);
  }
#else
#error Unknown or unsupported OS.
#endif

} // func __kmp_env_unset
Beispiel #6
0
void
__kmp_i18n_do_catopen(
) {
    int    english = 0;
    char * lang    = __kmp_env_get( "LANG" );
    // TODO: What about LC_ALL or LC_MESSAGES?

    KMP_DEBUG_ASSERT( status == KMP_I18N_CLOSED );
    KMP_DEBUG_ASSERT( cat    == KMP_I18N_NULLCAT );

    english =
	lang == NULL                       ||  // In all these cases English language is used.
	strcmp( lang, "" )            == 0 ||
        strcmp( lang, " " )           == 0 ||
              // Workaround for Fortran RTL bug DPD200137873 "Fortran runtime resets LANG env var
              // to space if it is not set".
	strcmp( lang, "C" )           == 0 ||
	strcmp( lang, "POSIX" )       == 0;

    if ( ! english ) {  // English language is not yet detected, let us continue.
        // Format of LANG is: [language[_territory][.codeset][@modifier]]
        // Strip all parts except language.
        char * tail = NULL;
        __kmp_str_split( lang, '@', & lang, & tail );
        __kmp_str_split( lang, '.', & lang, & tail );
        __kmp_str_split( lang, '_', & lang, & tail );
        english = ( strcmp( lang, "en" ) == 0 );
    }; // if

    KMP_INTERNAL_FREE( lang );

    // Do not try to open English catalog because internal messages are
    // exact copy of messages in English catalog.
    if ( english ) {
	status = KMP_I18N_ABSENT;  // mark catalog as absent so it will not be re-opened.
	return;
    }

    cat = catopen( name, 0 );
    // TODO: Why do we pass 0 in flags?
    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
        int    error   = errno; // Save errno immediately.
	char * nlspath = __kmp_env_get( "NLSPATH" );
        char * lang    = __kmp_env_get( "LANG" );

	// 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.
	__kmp_msg(
	    kmp_ms_warning,
	    KMP_MSG( CantOpenMessageCatalog, name ),
	    KMP_ERR( error ),
	    KMP_HNT( CheckEnvVar, "NLSPATH", nlspath ),
            KMP_HNT( CheckEnvVar, "LANG", lang ),
	    __kmp_msg_null
	);
	KMP_INFORM( WillUseDefaultMessages );
        KMP_INTERNAL_FREE( nlspath );
        KMP_INTERNAL_FREE( lang );
      }
    } 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 ];
            // Expected version of the catalog.
        kmp_str_buf_t version;   // Actual version of the catalog.
        __kmp_str_buf_init( & version );
        __kmp_str_buf_print( & version, "%s", catgets( cat, section, number, NULL ) );

            // String returned by catgets is invalid after closing the catalog, so copy it.
        if ( strcmp( version.str, expected ) != 0 ) {
            __kmp_i18n_catclose();     // Close bad catalog.
            status = KMP_I18N_ABSENT;  // And mark it as absent.
            if (__kmp_generate_warnings > kmp_warnings_low) { // AC: only issue warning in case explicitly asked to
                // And now print a warning using default messages.
                char const * name    = "NLSPATH";
                char const * nlspath = __kmp_env_get( name );
                __kmp_msg(
                    kmp_ms_warning,
                    KMP_MSG( WrongMessageCatalog, name, version.str, expected ),
                    KMP_HNT( CheckEnvVar, name, nlspath ),
                    __kmp_msg_null
                );
                KMP_INFORM( WillUseDefaultMessages );
                KMP_INTERNAL_FREE( (void *) nlspath );
            } // __kmp_generate_warnings
        }; // if
        __kmp_str_buf_free( & version );

    }; // if

} // func __kmp_i18n_do_catopen