add a set_environment_variable procedure to stdlib_system
Motivation
While iso_fortran_env provides get_environment_variable to query existing env variables, there doesn't seem to be a "set" counterpart availble in the Fortran standard or other standard modules.
A (yet to be) set_environment_variable could be added to stdlib by wrapping the C procedures
- Windows :
_putenv_s - Unix:
setenv
Prior Art
No response
Additional Information
No response
Something like:
subroutine set_environment_variable(name, value, overwrite, status)
character(len=*), intent(in) :: name
character(len=*), intent(in) :: value
logical, intent(in), optional :: overwrite
integer, intent(out), optional :: status
logical :: overwrite_
integer(c_int) :: c_status
character(kind=c_char, len=:), allocatable :: c_name, c_value
interface
#ifdef _WIN32
function c_putenv_s(name, value) bind(C, name="_putenv_s")
import :: c_char, c_int
integer(c_int) :: c_putenv_s
character(c_char) :: name(*)
character(c_char) :: value(*)
end function
#else
function c_setenv(name, value, overwrite) bind(C, name="setenv")
import :: c_char, c_int
integer(c_int) :: c_setenv
character(c_char) :: name(*)
character(c_char) :: value(*)
integer(c_int), value :: overwrite
end function
#endif
end interface
overwrite_= .true.
if (present(overwrite)) overwrite_= overwrite
c_name = trim(name) // c_null_char
c_value = trim(value) // c_null_char
#ifdef _WIN32
c_status = c_putenv_s(c_name, c_value)
#else
c_status = c_setenv(c_name, c_value, merge(1_c_int, 0_c_int, overwrite_) )
#endif
if (present(status)) status = int(c_status)
end subroutine set_environment_variable
@jalvesz I would like to take this one, if nobody else is on it.
I have read through stdlib_system and your sketch, and I think the shape is clear. Before writing it, three questions where guessing would cost a review round:
1. Interface block or C shim? Your sketch puts the interface inside the procedure with #ifdef _WIN32 selecting _putenv_s or setenv. is_directory instead declares a single bind(c) interface and puts the platform branching in src/system/stdlib_system.c. The second keeps the preprocessor out of the Fortran and matches what is already there — but yours is fewer moving parts. Which would you prefer?
2. overwrite on Windows. setenv takes an overwrite flag; _putenv_s does not, and always overwrites. To honour overwrite=.false. on Windows the shim has to getenv first and return early if the name exists — which is not atomic, though for environment variables that is unlikely to matter. Is emulating it the behaviour you want, or should the argument be documented as POSIX-only?
3. Deleting a variable. setenv(name, "", 1) sets an empty value rather than unsetting; unsetenv is the real counterpart. Should an empty value mean "set to empty", or should this be paired with a delete_environment_variable? I would rather not have set_environment_variable(name, "") quietly mean two different things on two platforms.
Assuming the usual shape, I would put it in src/system/stdlib_system.F90 plus stdlib_system.c, with a spec entry in doc/specs/stdlib_system.md, an example under example/system/, and a test in test/system/.
Happy to follow whatever you prefer on all three — I just did not want to write it twice.
Disclosure: I use Claude Code as part of my workflow.
Hi @kunalKumar-13 thanks for your interest in helping stdlib,
Regarding point 1: a single C shim that handles the C-preprocessing is the ideal entre point. The example I wrote was just as a starting point. Indeed, one Fortran procedure handling preprocessing inside looks attractive w.r.t less moving parts but when C preprocessing is required then it is better to have a C shim.
Regarding point 2: my point of view is that stdlib has to be as platform agnostic as possible. Allow user to have a similar experience regardless of the platform, while not always possible, here for instance it can be easily made such that the overwrite optional boolean argument simply has no effect under windows but it does under POSIX style OS. E.g.: the Fortran signature has an optional argument which value is ignored under windows. Having that documented is enough.
Regarding point 3: it might be better to have the companion procedure delete_environment_variable in this case. The intent would be much more explicit.
closed by #1208