Skip to content

Commit 749a33f

Browse files
authored
Merge pull request #353 from lplewa/getenv
Simplify getenv implementation
2 parents fad47c3 + 113431e commit 749a33f

File tree

6 files changed

+12
-77
lines changed

6 files changed

+12
-77
lines changed

src/utils/utils_common.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ void util_align_ptr_size(void **ptr, size_t *size, size_t alignment) {
3131
*size = s;
3232
}
3333

34+
int util_env_var_has_str(const char *envvar, const char *str) {
35+
char *value = getenv(envvar);
36+
if (value && strstr(value, str)) {
37+
return 1;
38+
}
39+
40+
return 0;
41+
}
42+
3443
// check if we are running in the proxy library
3544
int util_is_running_in_proxy_lib(void) {
3645
return util_env_var_has_str("LD_PRELOAD", "libumf_proxy.so");

src/utils/utils_common.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,6 @@ extern "C" {
4343

4444
#endif /* _WIN32 */
4545

46-
// utils_env_var - populate the given buffer with the value
47-
// of the given environment variable
48-
// Return value
49-
// If the function succeeds, the return value is the number of characters
50-
// stored in the buffer pointed to by buffer, not including
51-
// the terminating null character.
52-
//
53-
// If the buffer is not large enough to hold the data, then:
54-
// 1) the return value equals (-1) * the buffer size (in characters)
55-
// required to hold the string and its terminating null character,
56-
// 2) the content of the buffer is undefined.
57-
//
58-
// If the function fails, the return value is zero.
59-
int util_env_var(const char *envvar, char *buffer, size_t buffer_size);
60-
6146
// Check if the environment variable contains the given string.
6247
int util_env_var_has_str(const char *envvar, const char *str);
6348

src/utils/utils_log.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ void util_log(util_log_level_t level, const char *format, ...) {
133133
static const char *bool_to_str(int b) { return b ? "yes" : "no"; }
134134

135135
void util_log_init(void) {
136-
char envVar[MAX_ENV_LEN];
136+
const char *envVar = getenv("UMF_LOG");
137137

138-
if (util_env_var("UMF_LOG", envVar, sizeof(envVar)) <= 0) {
138+
if (!envVar) {
139139
return;
140140
}
141141

src/utils/utils_posix_common.c

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@
1717
static UTIL_ONCE_FLAG Page_size_is_initialized = UTIL_ONCE_FLAG_INIT;
1818
static size_t Page_size;
1919

20-
int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
21-
char *value = getenv(envvar);
22-
if (!value) {
23-
return 0;
24-
}
25-
26-
size_t len = strlen(value) + 1;
27-
if (len > buffer_size) {
28-
return -len;
29-
}
30-
31-
strncpy(buffer, value, buffer_size - 1);
32-
// make sure the string is NULL-terminated
33-
buffer[buffer_size - 1] = 0;
34-
35-
return (len - 1);
36-
}
37-
38-
int util_env_var_has_str(const char *envvar, const char *str) {
39-
char *value = getenv(envvar);
40-
if (value && strstr(value, str)) {
41-
return 1;
42-
}
43-
44-
return 0;
45-
}
46-
4720
static void _util_get_page_size(void) { Page_size = sysconf(_SC_PAGE_SIZE); }
4821

4922
size_t util_get_page_size(void) {

src/utils/utils_windows_common.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,6 @@
1818
static UTIL_ONCE_FLAG Page_size_is_initialized = UTIL_ONCE_FLAG_INIT;
1919
static size_t Page_size;
2020

21-
int util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
22-
int ret = GetEnvironmentVariableA(envvar, buffer, (DWORD)buffer_size);
23-
if (ret >= buffer_size) {
24-
return -ret;
25-
}
26-
27-
return ret;
28-
}
29-
30-
int util_env_var_has_str(const char *envvar, const char *str) {
31-
char buffer[BUFFER_SIZE];
32-
if (util_env_var(envvar, buffer, BUFFER_SIZE) > 0) {
33-
return (strstr(buffer, str) != NULL);
34-
}
35-
36-
return 0;
37-
}
38-
3921
static void _util_get_page_size(void) {
4022
SYSTEM_INFO SystemInfo;
4123
GetSystemInfo(&SystemInfo);

test/utils/utils_log.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,8 @@ int mock_fflush(FILE *stream) {
4747
}
4848

4949
extern "C" {
50-
const char *env_variable = "";
51-
52-
int mock_util_env_var(const char *envvar, char *buffer, size_t buffer_size) {
53-
(void)envvar;
54-
if (!env_variable) {
55-
return 0;
56-
}
57-
58-
size_t len = strlen(env_variable) + 1;
59-
len = buffer_size < len ? buffer_size : len;
60-
61-
memcpy(buffer, env_variable, len);
62-
63-
return (int)len;
64-
}
6550

51+
const char *env_variable = "";
6652
#define fopen(A, B) mock_fopen(A, B)
6753
#define fopen_s(A, B, C) mock_fopen_s(A, B, C)
6854
#define fputs(A, B) mock_fputs(A, B)

0 commit comments

Comments
 (0)