Skip to content

Commit

Permalink
fpm_os -> fpm_environment to enable single-file build
Browse files Browse the repository at this point in the history
  • Loading branch information
perazz committed Jun 24, 2024
1 parent 9e2dcc1 commit ac15668
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 35 deletions.
38 changes: 38 additions & 0 deletions src/fpm_environment.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdlib.h>
#include <stdio.h>

/// @brief Set environment variable using the C standard library
/// @param envname: points to a string containing the name of an environment variable to be added or altered.
/// @param envval: points to the value the environment variable is set to
/// @param overwrite: flag to determine whether an old value should be overwritten
/// @return success flag, 0 on successful execution
int c_setenv(const char *envname, const char *envval, int overwrite) {
#ifndef _WIN32
return setenv(envname, envval, overwrite);
#else
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, envname);
if (errcode || envsize) return errcode;
}
return _putenv_s(envname, envval);
#endif
}

/// @brief Delete environment variable using the C standard library
/// @param envname: points to a string containing the name of an environment variable.
/// @return success flag, 0 on successful execution
int c_unsetenv(const char *envname) {
#ifndef _WIN32
return unsetenv(envname);
#else
char* str = malloc(64*sizeof(char));
*str = '\0';
int errcode = _putenv_s(envname,str);
// Windows returns a non-0 code when setting empty variable
if (errcode==-1) errcode=0;
free(str);
return errcode;
#endif
}
35 changes: 0 additions & 35 deletions src/fpm_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,5 @@ char* c_realpath(char* path, char* resolved_path, int maxLength) {
#endif
}

/// @brief Set environment variable using the C standard library
/// @param envname: points to a string containing the name of an environment variable to be added or altered.
/// @param envval: points to the value the environment variable is set to
/// @param overwrite: flag to determine whether an old value should be overwritten
/// @return success flag, 0 on successful execution
int c_setenv(const char *envname, const char *envval, int overwrite) {
#ifndef _WIN32
return setenv(envname, envval, overwrite);
#else
int errcode = 0;
if(!overwrite) {
size_t envsize = 0;
errcode = getenv_s(&envsize, NULL, 0, envname);
if (errcode || envsize) return errcode;
}
return _putenv_s(envname, envval);
#endif
}

/// @brief Delete environment variable using the C standard library
/// @param envname: points to a string containing the name of an environment variable.
/// @return success flag, 0 on successful execution
int c_unsetenv(const char *envname) {
#ifndef _WIN32
return unsetenv(envname);
#else
char* str = malloc(64*sizeof(char));
*str = '\0';
int errcode = _putenv_s(envname,str);
// Windows returns a non-0 code when setting empty variable
if (errcode==-1) errcode=0;
free(str);
return errcode;
#endif
}


0 comments on commit ac15668

Please sign in to comment.