資源簡(jiǎn)介
1、應(yīng)用UNIX的fork()等系統(tǒng)調(diào)用,編寫一個(gè)c程序具有以下功能: a) 實(shí)現(xiàn)Shell的基本功能,包括有:打印提示符; 接受和分析命令行(濾去無效的空格、tab符號(hào)以及換行符等);執(zhí)行命令(要有出錯(cuò)處理;輸入exit或者bye退出);返回父進(jìn)程;b) 處理后臺(tái)程序(不需要wait)c) 處理多行命令(分析命令行中的‘;’并處理之)<br>d)應(yīng)用 dup(), pipe()系統(tǒng)調(diào)用具有輸入輸出重定向以及管道功能;收縮
代碼片段和文件信息
/*************************************************
????目的:
1、應(yīng)用UNIX的fork()等系統(tǒng)調(diào)用,編寫一個(gè)c程序具有以下功能:
??a)?實(shí)現(xiàn)Shell的基本功能,包括有:打印提示符;
??????接受和分析命令行(濾去無效的空格、tab符號(hào)以及換行符等);
??????執(zhí)行命令(要有出錯(cuò)處理;輸入exit或者bye退出);返回父進(jìn)程;
??b)?處理后臺(tái)程序(不需要wait)
??c)?處理多行命令(分析命令行中的‘;’并處理之)
??d)應(yīng)用?dup()?pipe()系統(tǒng)調(diào)用具有輸入輸出重定向以及管道功能;
????文件名:my_shell.c
????作者:鹿珂珂?2005011534
????日期:2008年5月31日
***************************************************/
/************頭文件************/
#include?
#include?
#include?
#include?
#include?
#include?
#include?
?? /**************全局變量定義***************/
const?int?BUFFERSIZE=80;//接收命令的最大字符數(shù)
char?buffer[80];//緩存用戶輸入的命令
int?is_back=0;//是否是后臺(tái)進(jìn)程默認(rèn)不是后臺(tái)執(zhí)行的程序
int?status=0;//狀態(tài)變量
pid_t?pid;//進(jìn)程
??
/**************函數(shù)聲明***************/
char?*get_command(int?*input_len);//獲取輸入命令
void?dispose_command(char?*deal_inint?len);//解析處理命令
int?redirect(char?*?in?int?len);//實(shí)現(xiàn)輸入輸出重定向的函數(shù)
int?piple(char?*?in?int?li_inlen);//實(shí)現(xiàn)管道功能的函數(shù)
int?is_fileexist(char?*?comm);//用來查找命令是否存在
void?multi_command(char?*?m_in?int?m_len);//處理用分號(hào)區(qū)分的多個(gè)命令
int?main()//主函數(shù):調(diào)用get_command()和dispose_command()實(shí)現(xiàn)shell的基本功能
{
char?*?path;//當(dāng)前路徑
char?*user;//用戶名
char?*input=NULL;//用戶輸入命令
int?command_len=0;//輸入字符個(gè)數(shù)
while(1)?
{
command_len=0;
path?=?get_current_dir_name();//獲取當(dāng)前目錄
user=getlogin();//獲取當(dāng)前登錄用戶
printf(“%s@%s-desktop:~%s$“useruser?path);//用戶輸入提示符:用戶名@用戶名-desktop:~當(dāng)前目錄$
if(input)
free(input);//釋放上一次輸入命令內(nèi)存空間
input=get_command(&command_len);//獲取命令
if(input)
{
dispose_command(inputcommand_len);//處理執(zhí)行命令
}
}
}
char?*get_command(int?*?input_len)//獲取用戶輸入命令
{
char?lc_char;//輸入字符
char?*get_in;
(*input_len)=0;
/*?開始獲取輸入?*/
lc_char?=?getchar();
while(lc_char?!=?‘\n‘?&&?(*input_len)? {
???? buffer[(*input_len)?++]?=?lc_char;
??? ? lc_char?=?getchar();
}
/*?命令超長處理*/
if((*input_len)?>=?BUFFERSIZE)?{
?? ???printf(“Your?command?too?long?!?Please?reenter?your?command?!\n“);
?? ???(*input_len)?=?0;?????/*?Reset?*/
???gets(buffer);
???strcpy(buffer““);
???get_in=NULL;
???? ???return?NULL;
}
else??
??? buffer[(*input_len)]?=?‘\0‘;?/*加上串結(jié)束符,形成字符串*/
if((*input_len)==0)return?NULL;//處理直接敲入回車的情況
/*?將命令從緩存拷貝到in中*/
get_in?=?(char?*)malloc(sizeof(char)?*?((*input_len)?+?1));
strcpy(get_in?buffer);
strcpy(buffer““);
return?get_in;
}
void?dispose_command(char?*deal_inint?len)//調(diào)用multi_command()、piple()、redirect()分別實(shí)現(xiàn)多個(gè)命令、管道、重定向
{
char?*?arg[30];//存儲(chǔ)指令或參數(shù)
int?i=0?j=0?k=0;//計(jì)數(shù)變量
pid_t?pid;//進(jìn)程
/*?獲取命令和參數(shù)并保存在arg中*/
?for(?i=0;i<=len;i++)
?{
if(deal_in[i]==‘;‘)
{
multi_command(deal_inlen);
return;
}
?}
?for(?i?=?0?j=0k=0;?i<=len;?i?++)?{
/*管道和重定向單獨(dú)處理*/
if(deal_in[i]==‘<‘?||?deal_in[i]?==?‘>‘?||?deal_in[i]?==?‘|‘?||?deal_in[i]==‘;‘)?{
??if(deal_in[i]?==?‘|‘)
{
?????? ?????piple(deal_in?len);
?????return;
}
???else?if
評(píng)論
共有 條評(píng)論