Skip to content

Commit ac15668

Browse files
committed
fpm_os -> fpm_environment to enable single-file build
1 parent 9e2dcc1 commit ac15668

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/fpm_environment.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
4+
/// @brief Set environment variable using the C standard library
5+
/// @param envname: points to a string containing the name of an environment variable to be added or altered.
6+
/// @param envval: points to the value the environment variable is set to
7+
/// @param overwrite: flag to determine whether an old value should be overwritten
8+
/// @return success flag, 0 on successful execution
9+
int c_setenv(const char *envname, const char *envval, int overwrite) {
10+
#ifndef _WIN32
11+
return setenv(envname, envval, overwrite);
12+
#else
13+
int errcode = 0;
14+
if(!overwrite) {
15+
size_t envsize = 0;
16+
errcode = getenv_s(&envsize, NULL, 0, envname);
17+
if (errcode || envsize) return errcode;
18+
}
19+
return _putenv_s(envname, envval);
20+
#endif
21+
}
22+
23+
/// @brief Delete environment variable using the C standard library
24+
/// @param envname: points to a string containing the name of an environment variable.
25+
/// @return success flag, 0 on successful execution
26+
int c_unsetenv(const char *envname) {
27+
#ifndef _WIN32
28+
return unsetenv(envname);
29+
#else
30+
char* str = malloc(64*sizeof(char));
31+
*str = '\0';
32+
int errcode = _putenv_s(envname,str);
33+
// Windows returns a non-0 code when setting empty variable
34+
if (errcode==-1) errcode=0;
35+
free(str);
36+
return errcode;
37+
#endif
38+
}

src/fpm_os.c

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,5 @@ char* c_realpath(char* path, char* resolved_path, int maxLength) {
1616
#endif
1717
}
1818

19-
/// @brief Set environment variable using the C standard library
20-
/// @param envname: points to a string containing the name of an environment variable to be added or altered.
21-
/// @param envval: points to the value the environment variable is set to
22-
/// @param overwrite: flag to determine whether an old value should be overwritten
23-
/// @return success flag, 0 on successful execution
24-
int c_setenv(const char *envname, const char *envval, int overwrite) {
25-
#ifndef _WIN32
26-
return setenv(envname, envval, overwrite);
27-
#else
28-
int errcode = 0;
29-
if(!overwrite) {
30-
size_t envsize = 0;
31-
errcode = getenv_s(&envsize, NULL, 0, envname);
32-
if (errcode || envsize) return errcode;
33-
}
34-
return _putenv_s(envname, envval);
35-
#endif
36-
}
37-
38-
/// @brief Delete environment variable using the C standard library
39-
/// @param envname: points to a string containing the name of an environment variable.
40-
/// @return success flag, 0 on successful execution
41-
int c_unsetenv(const char *envname) {
42-
#ifndef _WIN32
43-
return unsetenv(envname);
44-
#else
45-
char* str = malloc(64*sizeof(char));
46-
*str = '\0';
47-
int errcode = _putenv_s(envname,str);
48-
// Windows returns a non-0 code when setting empty variable
49-
if (errcode==-1) errcode=0;
50-
free(str);
51-
return errcode;
52-
#endif
53-
}
5419

5520

0 commit comments

Comments
 (0)