每个程序都有一个环境表,它是一个字符指针数组,其中每个指针包含一个以NULL结尾的C字符串的地址。全局变量environ则包含了该指针数组的地址:
简单打印环境变量表:
#include<stdio.h>
extern char **environ;
int main()
{
int i;
for(i = 0; *environ[i]; i++)
printf("*environ[%d]: %s\n", i, environ[i]);
return 0;
}
|
结果:
h180 c # ./a.out
*environ[0]: ORBIT_SOCKETDIR=/tmp/orbit-root
*environ[1]: SSH_AGENT_PID=3471
*environ[2]: GPG_AGENT_INFO=/tmp/seahorse-I1EsFP/S.gpg-agent:3493:1
*environ[3]: TERM=xterm
*environ[4]: SHELL=/bin/bash
*environ[5]: XDG_SESSION_COOKIE=4f027cabadbf93d36492b7424b4a1694-1264793154.856694-819590398
*environ[6]: GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
*environ[7]: WINDOWID=65074553
*environ[8]: GTK_MODULES=canberra-gtk-module
*environ[9]: USER=root
*environ[10]: GNOME_KEYRING_SOCKET=/tmp/keyring-NBsJ8W/socket
*environ[11]: SSH_AUTH_SOCK=/tmp/ssh-cvNttB3314/agent.3314
*environ[12]: SESSION_MANAGER=local/h180:/tmp/.ICE-unix/3314
*environ[13]: USERNAME=root
*environ[14]: DESKTOP_SESSION=default
*environ[15]: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/pgsql/bin
*environ[16]: QT_IM_MODULE=ibus
*environ[17]: GDM_XSERVER_LOCATION=local
*environ[18]: PWD=/root/c
*environ[19]: XMODIFIERS=@im=ibus
*environ[20]: LANG=en_US.UTF-8
*environ[21]: GNOME_KEYRING_PID=3301
*environ[22]: GDM_LANG=en_US.UTF-8
*environ[23]: GDMSESSION=default
*environ[24]: XMODIFIER=@im=ibus
*environ[25]: SHLVL=1
*environ[26]: HOME=/root
*environ[27]: GNOME_DESKTOP_SESSION_ID=this-is-deprecated
*environ[28]: LOGNAME=root
*environ[29]: XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/usr/share/gdm/
*environ[30]: DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-Lp35w5OCn7,guid=59991196229b67f06e7bd0314b633646
*environ[31]: WINDOWPATH=7
*environ[32]: DISPLAY=:0.0
*environ[33]: GTK_IM_MODULE=ibus
*environ[34]: XAUTHORITY=/root/.Xauthority
*environ[35]: COLORTERM=gnome-terminal
*environ[36]: OLDPWD=/usr/include
*environ[37]: _=./a.out
Segmentation fault (core dumped)
|
阅读(2108) | 评论(0) | 转发(0) |