Skip to content

Commit 7d2bfce

Browse files
authored
Merge pull request #1253 from ACSimon33/getenv_s
Improve getenv usage
2 parents 8a1ae32 + de2f9be commit 7d2bfce

2 files changed

Lines changed: 60 additions & 5 deletions

File tree

BLAS/SRC/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ if(BUILD_INDEX64_EXT_API)
129129
set_target_properties(${BLASLIB}_64_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)
130130
#Add _64 suffix to all Fortran functions via macros
131131
foreach(F IN LISTS SOURCES_64_F)
132-
if(CMAKE_Fortran_COMPILER_ID STREQUAL "NAG")
132+
if(CMAKE_Fortran_COMPILER_ID STREQUAL "NAG" OR CMAKE_Fortran_COMPILER_ID MATCHES "Intel")
133133
set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS "-fpp")
134134
else()
135135
set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS "-cpp")

LAPACKE/src/lapacke_nancheck.c

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,83 @@
3232

3333
#include "lapacke_utils.h"
3434

35+
#include <stdlib.h>
36+
#include <string.h>
37+
3538
static int nancheck_flag = -1;
3639

3740
void LAPACKE_set_nancheck( int flag )
3841
{
3942
nancheck_flag = ( flag ) ? 1 : 0;
4043
}
4144

45+
typedef struct {
46+
int found;
47+
char *value;
48+
} lapacke_env_var;
49+
50+
static inline lapacke_env_var LAPACKE_getenv(const char *var_name)
51+
{
52+
size_t var_length = 0;
53+
54+
/* Get the length of the environment variable value */
55+
#if defined(_WIN32)
56+
errno_t result = getenv_s( &var_length, NULL, 0, var_name );
57+
if ( result != 0 || var_length == 0 ) {
58+
return (lapacke_env_var){ 0, NULL };
59+
}
60+
#else
61+
const char *env = getenv( var_name );
62+
if ( env == NULL ) {
63+
return (lapacke_env_var){ 0, NULL };
64+
}
65+
var_length = strlen( env ) + 1;
66+
#endif
67+
68+
/* Allocate memory for the environment variable value */
69+
char *value = (char *)LAPACKE_malloc( var_length );
70+
if ( value == NULL ) {
71+
return (lapacke_env_var){ 0, NULL };
72+
}
73+
74+
/* Get the value of the environment variable */
75+
#if defined(_WIN32)
76+
result = getenv_s( &var_length, value, var_length, var_name );
77+
if ( result != 0 ) {
78+
LAPACKE_free( value );
79+
return (lapacke_env_var){ 0, NULL };
80+
}
81+
#else
82+
memcpy( value, env, var_length );
83+
#endif
84+
85+
return (lapacke_env_var){ 1, value };
86+
}
87+
88+
static inline void LAPACKE_freenv(lapacke_env_var *var)
89+
{
90+
if ( var->found && var->value != NULL ) {
91+
LAPACKE_free( var->value );
92+
}
93+
var->found = 0;
94+
var->value = NULL;
95+
}
96+
4297
int LAPACKE_get_nancheck( )
4398
{
44-
char* env;
4599
if ( nancheck_flag != -1 ) {
46100
return nancheck_flag;
47101
}
48102

49103
/* Check environment variable, once and only once */
50-
env = getenv( "LAPACKE_NANCHECK" );
51-
if ( !env ) {
104+
lapacke_env_var lapacke_nancheck = LAPACKE_getenv( "LAPACKE_NANCHECK" );
105+
if ( !lapacke_nancheck.found ) {
52106
/* By default, NaN checking is enabled */
53107
nancheck_flag = 1;
54108
} else {
55-
nancheck_flag = atoi( env ) ? 1 : 0;
109+
nancheck_flag = atoi( lapacke_nancheck.value ) ? 1 : 0;
56110
}
57111

112+
LAPACKE_freenv( &lapacke_nancheck );
58113
return nancheck_flag;
59114
}

0 commit comments

Comments
 (0)