用標準C實現shell功能(轉)
用標準C實現shell功能(轉)[@more@]#include #include #include #include #include #include #define SHELL_NAME "sh1"#define PROMPT_ENVIRONMENT_VARIABLE "PROMPT"char *prompt;int main(int argc, char **argv) {char cmd[80];int statval;/* Determine prompt value. */if ((prompt = getenv(PROMPT_ENVIRONMENT_VARIABLE)) == NULL)prompt = SHELL_NAME ":";/* Process commands until exit, or death by signal. */while (1) {/* Prompt and read a command. */printf(prompt);gets(cmd);/* Process built-in commands. */if(strcasecmp(cmd, "exit") == 0)break;/* Process non-built-in commands. */if(fork() == 0) {execlp(cmd, cmd, NULL);fprintf(stderr, "%s: Exec %s failed: %s
", argv[0], cmd, strerror(errno));exit(1);} wait(&statval);if(WIFEXITED(statval)) {if(WEXITSTATUS(statval)){fprintf(stderr,"%s: child exited with status %d.
",argv[0], WEXITSTATUS(statval));}} else {fprintf(stderr, "%s: child died unexpectedly.
", argv[0]);}}}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8225414/viewspace-944582/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 也用 C++ 實現 Property 功能 (轉)C++
- 淺談C#中標準Dispose模式的實現C#模式
- Shell標準輸出、標準錯誤 >/dev/null 2>&1devNull
- C# 實現將 PDF 轉文字的功能C#
- [Shell] AWK實現SQL的功能(1)SQL
- JavaScript實現標準快速排序JavaScript排序
- C 標準庫 -
- C++標準模板庫(STL)迭代器的原理與實現C++
- 【shell】實現tomcat日誌切割功能Tomcat
- 用C++實現下載檔案的功能C++
- 用VB編寫標準CGI程式 (轉)
- 用ASP實現論壇的UBB功能 (轉)
- C++標準庫C++
- Visual C++實現檔案間批次轉換功能 (轉)C++
- c/c++ 標準庫 vectorC++
- C++ articles:Guru of the Week #3:使用標準庫 (轉)C++
- 工程座標轉換方法C#程式碼實現C#
- Qt標準對話方塊實現QT
- JBI - Java實現SOA的標準方法Java
- 用VC++實現上網撥號功能 (轉)C++
- [Shell] awk 實現列轉行例子
- Visual C++ 6.0程式設計實現列印功能(轉)C++程式設計
- C標準庫學習
- 標準c語言03C語言
- - C語言標準庫C語言
- c#標準註釋C#
- 模組轉測標準
- 利用shell指令碼實現計劃任務功能指令碼
- 用CSS實現類似導航翻轉功能例子CSS
- 標準庫 http 包的簡單實用HTTP
- 用pandas實現SQL功能SQL
- 如何實現共享螢幕標註功能?
- 利用指標實現strncmp函式功能指標函式
- Unity標準開發例項——虛擬現實技術應用14Unity
- 什麼是 C 和 C ++ 標準庫?
- c/c++ 標準庫 map set 插入C++
- c/c++ 標準庫 迭代器(iterator)C++
- c/c++標準預定義巨集C++