How to get environment variables in C in cross-platform mode

I use the following code on OSX and on Windows Vista:

#include <stdio.h>
#include <string.h>

extern char **environ;

int
main(int argc, char **argv)
{
    int i;
    for (i = 0; environ[i] != NULL; i++)
    {
        printf("var = %s - %d\n", environ[i], (int)strlen(environ[i]));
    }

    return 0;
}

On OSX, compile it with cc, on Windows with 'cl' from the Windows SDK 7.1 (with redistributable packages: Microsoft Visual C ++ 2010 in it).

I define the same ENV variables in both OSs in Russian: MY_ENV = 'Russian letters go here'. And they got a difference of about 2 times the length of the lines.

This is the length that I plan to use in malloc, and I want them to be the same. How can I get the same lengths correctly on CRT?

+3
source share
1 answer

getenv {http://man7.org/linux/man-pages/man3/getenv.3.html} GNU/Linux, Mac OS X Microsoft Windows. setenv {http://man7.org/linux/man-pages/man3/setenv.3.html} GNU/Linux Mac OS X, Microsoft Windows. putenv {http://man7.org/linux/man-pages/man3/putenv.3.html}, getenv, GNU/Linux, Mac OS X Microsoft Windows.

, getenv putenv - -.

+3

All Articles