55

I need to know a way for use environment variables in the C programming language. How can I use and read them?

For example, read an environment variable or take the value of an environment variable and load it in another variable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex
  • 559
  • 1
  • 4
  • 6

5 Answers5

48

You can use following functions -

char * getenv (const char *name)-returns a string that is the value of the environment variable name.

char * secure_getenv (const char *name)

Read about some more functions here -http://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • Thanks for the useful link! But could you, please, clarify (or give a link to some kind of article clarifying) why is environment untrusted when the executable has a SUID/SGID bit set? – Kolay.Ne Jan 30 '21 at 12:52
  • @Kolay.Ne With the effective uid/gid, the program might gain some additional permissions like that of the root. If the environment variables are set to some malicious value it can be harmful. Also, such programs can give users running it the same permissions and kind of like a backdoor entry. – ameyCU Sep 25 '21 at 15:17
15

Use the getenv function from stdlib.h. That's it!

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

int main()
{
    printf("test\n");

    const char* s = getenv("PATH");

    // If the environment variable doesn't exist, it returns NULL
    printf("PATH :%s\n", (s != NULL) ? s : "getenv returned NULL");

    printf("end test\n");
}
Taureon
  • 75
  • 1
  • 9
user3334059
  • 437
  • 3
  • 5
  • 1
    The link here is now broken. It's generally best to include more context in the answer in case an external links breaks years after it's posted. – lucas Aug 03 '19 at 19:52
12

getenv:

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

int main(int argc, char *argv[])
{
    char* my_env_var = getenv("MY_ENV_VAR");

    if(my_env_var)
        printf("Var found: %s", my_env_var );
    else
        printf("Var not found.");                

    return 0;
}
Danijel
  • 8,198
  • 18
  • 69
  • 133
1

On windows, you would use GetEnvironmentVariable.

#include <stdio.h>
#include <winbase.h>
int main(int argc, char *argv[])
{
    TCHAR buff[100] = T("");
    DWORD resultLengthInCharacters = GetEnvironmentVariable(T("USERDOMAIN"), buff, 100);

    if (resultLengthInCharacters > 0 && resultLengthInCharacters < 100) {
        _tprintf(T("USERDOMAIN: %s\n"), buff);
    } else if ( resultLengthInCharacters > 100) {
        _tprintf(T("USERDOMAIN too long to store in buffer of length 100, try again with buffer length %lu\n"), resultLengthInCharacters);
    } else {
        // Error handling incomplete, should use GetLastError(),
        // but typically:
        _tprintf(T("USERDOMAIN is empty or not set in the Environment\n"));
    }
    return 0;
}

But if you are trying to get a standard path variable, you should use the SHGetFolderPath function with the right CSIDL variable (like from this question: How do I get the application data path in Windows using C++?)

Andrew Domaszek
  • 651
  • 6
  • 18
-2

Another way could be to use the global variable environ.

#include <stdio.h>

extern char** environ;
void main(int argc, char* argv[])
{
    int i=0;
    while(environ[i]!=NULL){
        printf("%s\n",environ[i++]);
    }
}
  • Definitely not the better way. You're loading variables from the global variable, so why are you including envp char pointer array in your main function arguments? – Timotej Leginus Dec 06 '22 at 16:43
  • Aaaand undeclared variable`I`. Love it when people don't even check if their snippet compiles. – Nidrax Dec 07 '22 at 12:30