Skip to content

Commit 11a7590

Browse files
committed
Use GetEnvironmentVariableA() instead of _dupenv_s()
Add util_env_var() and util_env_var_has_str() to utils_common.h. Use GetEnvironmentVariableA() instead of _dupenv_s(). Signed-off-by: Lukasz Dorau <[email protected]>
1 parent f78d5a1 commit 11a7590

File tree

4 files changed

+91
-27
lines changed

4 files changed

+91
-27
lines changed

src/utils/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ include(${UMF_CMAKE_SOURCE_DIR}/cmake/helpers.cmake)
66
include(FindThreads)
77

88
set(UMF_UTILS_SOURCES_POSIX
9+
utils_posix_common.c
910
utils_posix_concurrency.c
1011
utils_posix_math.c
1112
)
1213

1314
set(UMF_UTILS_SOURCES_WINDOWS
15+
utils_windows_common.c
1416
utils_windows_concurrency.c
1517
utils_windows_math.c
1618
)

src/utils/utils_common.h

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
#include <stdint.h>
1414
#include <stdio.h>
1515
#include <stdlib.h>
16-
#include <string.h>
1716

1817
#ifndef _WIN32
1918
#include <sys/syscall.h>
@@ -36,45 +35,23 @@ extern "C" {
3635

3736
#define __TLS __declspec(thread)
3837

39-
static inline char *util_getenv(const char *name) {
40-
char *buffer;
41-
size_t numberOfElements;
42-
errno_t err = _dupenv_s(&buffer, &numberOfElements, name);
43-
if (err) {
44-
return NULL;
45-
}
46-
47-
return buffer;
48-
}
49-
50-
static inline void util_free_getenv(char *val) { free(val); }
51-
5238
// TODO: implement util_get_page_size() for Windows
5339
static inline size_t util_get_page_size(void) { return 4096; }
5440

5541
#else /* Linux */
5642

5743
#define __TLS __thread
5844

59-
static inline char *util_getenv(const char *name) { return getenv(name); }
60-
static inline void util_free_getenv(const char *val) {
61-
(void)val; // unused
62-
}
63-
6445
static inline size_t util_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }
6546

6647
#endif /* _WIN32 */
6748

49+
int util_env_var(const char *envvar, char *buffer, size_t buffer_size);
50+
int util_env_var_has_str(const char *envvar, const char *str);
51+
6852
// check if we are running in the proxy library
6953
static inline int is_running_in_proxy_lib(void) {
70-
int is_in_proxy_lib_val = 0;
71-
char *ld_preload = util_getenv("LD_PRELOAD");
72-
if (ld_preload && strstr(ld_preload, "libumf_proxy.so")) {
73-
is_in_proxy_lib_val = 1;
74-
}
75-
76-
util_free_getenv(ld_preload);
77-
return is_in_proxy_lib_val;
54+
return util_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");
7855
}
7956

8057
#define NOFUNCTION \

src/utils/utils_posix_common.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#include <stdlib.h>
11+
#include <string.h>
12+
13+
// Return value
14+
// If the function succeeds, the return value is the number of characters
15+
// stored in the buffer pointed to by buffer, not including
16+
// the terminating null character.
17+
//
18+
// If buffer is not large enough to hold the data, the return value
19+
// is the buffer size, in characters, required to hold the string
20+
// and its terminating null character and the contents of buffer are undefined.
21+
//
22+
// If the function fails, the return value is zero.
23+
int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
24+
char *value = getenv(envvar);
25+
if (!value) {
26+
return 0;
27+
}
28+
29+
size_t len = strlen(value) + 1;
30+
if (len > buffer_size) {
31+
return len;
32+
}
33+
34+
strcpy(buffer, value);
35+
36+
return (len - 1);
37+
}
38+
39+
// Check if the environment variable contains the given string.
40+
int util_env_var_has_str(const char *envvar, const char *str) {
41+
char *value = getenv(envvar);
42+
if (value && strstr(value, str)) {
43+
return 1;
44+
}
45+
46+
return 0;
47+
}

src/utils/utils_windows_common.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* Copyright (C) 2024 Intel Corporation
4+
*
5+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
6+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
*
8+
*/
9+
10+
#include <Windows.h>
11+
#include <processenv.h>
12+
13+
#define BUFFER_SIZE 1024
14+
15+
// Return value
16+
// If the function succeeds, the return value is the number of characters
17+
// stored in the buffer pointed to by buffer, not including
18+
// the terminating null character.
19+
//
20+
// If buffer is not large enough to hold the data, the return value
21+
// is the buffer size, in characters, required to hold the string
22+
// and its terminating null character and the contents of buffer are undefined.
23+
//
24+
// If the function fails, the return value is zero.
25+
int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
26+
return GetEnvironmentVariableA(envvar, buffer, (DWORD)buffer_size);
27+
}
28+
29+
// Check if the environment variable contains the given string.
30+
int util_env_var_has_str(const char *envvar, const char *str) {
31+
char buffer[BUFFER_SIZE];
32+
int rc = util_env_var(envvar, buffer, BUFFER_SIZE);
33+
if (0 < rc && rc < BUFFER_SIZE) {
34+
return strstr(buffer, str) ? 1 : 0;
35+
}
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)