環境變量,在程序中獲取。
shell命令,env 命令 (whereis ps which ps)
env|grep JAVA
程序中獲取環境變量
#include <iostream>
using namespace std;

int main(int argc,char** argv, char** env)


{

while(env&&*env)
{
cout << *env << endl;
++env;
}
return 0;
}
c++,一個字符串數組,以char**的指針代表的。

所以遍歷時,既要判斷env指向的指針數組是否為空,也要判斷*env是不是到末端。
=======================================
Unix 系統調用函數,getenv。
man getenv (或者man -k getenv man -a getenv)
SYNOPSIS
#include <stdlib.h>
char *getenv(const char *name);
得到函數原型和需要包含的頭文件。
RETURN VALUES
If successful, getenv() returns a pointer to the value in
the current environment; otherwise, it returns a null
pointer.
#include <stdlib.h>
#include <iostream>

using namespace std;

int main()


{
char javahome[200];
strcpy(javahome,getenv("JAVA_HOME"));
cout << javahome << endl;
return 0;
}int putenv(const char* str); 成功返回0;
在程序中添加和修改的環境變量只對本進程起作用。

代碼棧,存儲著函數調用的入口地址,通過pstack命令可以查看相應函數調用關系,也就是代碼棧。
openlab.tarena.ca% pstack 3212
3212: ljlserver
ff198958 accept (0, ffbefad8, ffbefad4, 1)
00010e74 main (1, ffbefb84, ffbefb8c, 216c4, 0, 0) + 348
000109f4 _start (0, 0, 0, 0, 0, 0) + 5c
Unix每個進程的啟動都回調用_start,然后向main傳入參數,在main調用前,env環境變量就已經拷貝到進程空間中了。_start負責提供前提條件。
posted on 2006-02-05 21:41
北國狼人的BloG 閱讀(404)
評論(0) 編輯 收藏 所屬分類:
達內學習總結