隨筆-159  評論-114  文章-7  trackbacks-0

          環境變量,在程序中獲取。

          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**的指針代表的。

          Untitled-1.gif
          所以遍歷時,既要判斷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;

          在程序中添加和修改的環境變量只對本進程起作用。

          Untitled2.gif
          代碼棧,存儲著函數調用的入口地址,通過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 閱讀(406) 評論(0)  編輯  收藏 所屬分類: 達內學習總結
          主站蜘蛛池模板: 红原县| 岳阳县| 含山县| 建瓯市| 合山市| 麻阳| 金华市| 章丘市| 昭平县| 威海市| 胶南市| 汾阳市| 麦盖提县| 云阳县| 汉川市| 鄂尔多斯市| 德清县| 鄯善县| 铜鼓县| 囊谦县| 莒南县| 留坝县| 永修县| 麻城市| 新宾| 泰兴市| 电白县| 屏东市| 磴口县| 本溪市| 通化市| 岢岚县| 通辽市| 柯坪县| 旬阳县| 商河县| 晋州市| 县级市| 贵阳市| 明光市| 娱乐|