csetenv

Setenv in c programming usage?


#include <stdio h>
#include <stdlib.h>

int main(int argc, char *argv[], char *envp[])
{
    int i = 0;
    while (envp[i] != NULL)
    {
        if (strstr(envp[i], "SHLVL") != NULL)
            printf("%s\n", envp[i]);
        i++;
    }
  
    setenv("SHLVL", "stackoverflow", 2);
    i = 0;
    while (envp[i] != NULL)
    {
        if (strstr(envp[i], "SHLVL") != NULL)
            printf("%s\n", envp[i]);
        i++;
    }
    return 0;
}

In this code SHLVL is an existing environmental variable. When I try to change the value of that variable using setenv it gives expected output.

SHLVL=1
SHLVL=stackoverflow

In another case: The same code with non-existent environmental variable

int main(int argc, char *argv[], char *envp[])
{
    int i = 0;
    while (envp[i] != NULL)
    {
        if (strstr(envp[i], "SHLVL") != NULL)
            printf("%s\n", envp[i]);
        i++;
    }

    setenv("arr", "33", 2); // non-exist environmental variable 
    setenv("SHLVL", "stackoverflow", 2);
    i = 0;
    while (envp[i] != NULL)
    {
        if (strstr(envp[i], "SHLVL") != NULL)
            printf("%s\n", envp[i]);
        i++;
    }
    return 0;
}

Here the arr is an non-exist environmental variable. This code give output as

SHLVL=1
SHLVL=1

My question is how the non-existing environmental variable change the output here? Here I am using gcc compiler.


Solution

  • The envp argument in main points to the original array with environment variable definitions. When you set en environment variable using setenv():

    This behavior is very tricky and has far reaching consequences for the memory management of definition strings. Passing the envp to main is not standard and only available on unix systems, but even on these systems, a preferred way of enumerating the environment definitions is to use the global variable environ that is updated by setenv() and putenv():

    extern char **environ;