static void ___kmp_env_blk_parse_unix( kmp_env_blk_t * block, // M: Env block to fill. char * * env // I: Unix environment to parse. ) { char * bulk = NULL; kmp_env_var_t * vars = NULL; int count = 0; int size = 0; // Size of bulk. // Count number of variables and length of required bulk. { count = 0; size = 0; while ( env[ count ] != NULL ) { size += KMP_STRLEN( env[ count ] ) + 1; ++ count; }; // while } // Allocate memory. bulk = (char *) allocate( size ); vars = (kmp_env_var_t *) allocate( count * sizeof( kmp_env_var_t ) ); // Loop thru all the vars. { char * var; // Pointer to beginning of var. char * name; // Pointer to name of variable. char * value; // Pointer to value. int len; // Length of variable. int i; var = bulk; for ( i = 0; i < count; ++ i ) { // Copy variable to bulk. len = KMP_STRLEN( env[ i ] ); KMP_MEMCPY_S( var, size, env[ i ], len + 1 ); // Save found variable in vars array. __kmp_str_split( var, '=', & name, & value ); vars[ i ].name = name; vars[ i ].value = value; // Move pointer. var += len + 1; }; // for } // Fill out result. block->bulk = bulk; block->vars = vars; block->count = count; }; // ___kmp_env_blk_parse_unix
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
kmp_msg_t __kmp_msg_error_mesg( char const * mesg ) { kmp_msg_t msg; msg.type = kmp_mt_syserr; msg.num = 0; msg.str = __kmp_str_format( "%s", mesg ); msg.len = KMP_STRLEN( msg.str ); return msg; } // __kmp_msg_error_mesg
kmp_msg_t __kmp_msg_error_code( int code ) { kmp_msg_t msg; msg.type = kmp_mt_syserr; msg.num = code; msg.str = sys_error( code ); msg.len = KMP_STRLEN( msg.str ); return msg; } // __kmp_msg_error_code
static void ___kmp_env_blk_parse_windows( kmp_env_blk_t *block, // M: Env block to fill. char const *env // I: Pointer to Windows* OS (DOS) environment block. ) { char *bulk = NULL; kmp_env_var_t *vars = NULL; int count = 0; // Number of used elements in vars array. int size = 0; // Size of bulk. char *name; // Pointer to name of variable. char *value; // Pointer to value. if (env != NULL) { // Loop thru all the vars in environment block. Count variables, find size // of block. { char const *var; // Pointer to beginning of var. int len; // Length of variable. count = 0; var = env; // The first variable starts and beginning of environment block. len = KMP_STRLEN(var); while (len != 0) { ++count; size = size + len + 1; var = var + len + 1; // Move pointer to the beginning of the next variable. len = KMP_STRLEN(var); } size = size + 1; // Total size of env block, including terminating zero byte. } // Copy original block to bulk, we will modify bulk, not original block. bulk = (char *)allocate(size); KMP_MEMCPY_S(bulk, size, env, size); // Allocate vars array. vars = (kmp_env_var_t *)allocate(count * sizeof(kmp_env_var_t)); // Loop thru all the vars, now in bulk. { char *var; // Pointer to beginning of var. int len; // Length of variable. count = 0; var = bulk; len = KMP_STRLEN(var); while (len != 0) { // Save variable in vars array. __kmp_str_split(var, '=', &name, &value); vars[count].name = name; vars[count].value = value; ++count; // Get the next var. var = var + len + 1; len = KMP_STRLEN(var); } } } // Fill out result. block->bulk = bulk; block->vars = vars; block->count = count; }