工作中,许多时候需要查看所有系统环境变量,以确定某一个变量是否定义了,或者值是多少,写一个程序,编译出来之后放到可执行程序目录中即可,十分方便:
直接上代码:
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
void platform()
{
// 获取系统变量信息
#ifdef WIN32
#pragma warning(disable : 4101)
#endif
char *ret;
extern char **environ;
char **env = environ;
// 打印系统变量信息
while(*env)
{
puts(*env);
env++;
}
#ifdef WIN32
// for windows_nt
size_t nReturnValue;
char buffer[8192];
const char *varname = "OS";
getenv_s(&nReturnValue, buffer, 8192, varname);
if (nReturnValue > 0)
{
printf("%s\n", buffer);
}
#else
// for Linux
ret = getenv("OS");
if (ret != NULL)
{
ret = getenv("TERM");
printf("%s \n", ret);
}
#endif
}
int main(int argc, char *argv[])
{
printf("测试工具 环境变量枚举v1.0\n");
platform();
return 0;
}