您的位置:首页 > 编程语言

UNIX环境高级编程1-5从标准输入读命令并执行

2015-01-29 16:39 295 查看
从标准输入读命令并执行

隐藏行号复制代码?

//program1-5proc/shell1.c


//proc/shell1.c

#include"apue.h"


#include<sys/wait.h>


intmain(void)


{


charbuf[MAXLINE];/*fromapue.h*/


pid_tpid;


intstatus;


printf("%%");/*printprompt(printfrequires%%toprint%)*/


//WeusethestandardI/Ofunctionfgetstoreadonelineatatimefromthe


//standardinput.Whenwetypetheend-of-filecharacter(whichisoften


//Control-D)asthefirstcharacterofaline,fgetsreturnsanullpointer,theloop


//stops,andtheprocessterminates.InChapter18,wedescribeallthespecial


//terminalcharacters--endoffile,backspaceonecharacter,eraseentireline,and


//soon--andhowtochangethem.


//Becauseeachlinereturnedbyfgetsisterminatedwithanewlinecharacter,


//followedbyanullbyte,weusethestandardCfunctionstrlentocalculatethe


//lengthofthestring,andthenreplacethenewlinewithanullbyte.Wedothis


//becausetheexeclpfunctionwantsanull-terminatedargument,nota


//newline-terminatedargument.


//Wecallforktocreateanewprocess,whichisacopyofthecaller.Wesaythat


//thecalleristheparentandthatthenewlycreatedprocessisthechild.Then


//forkreturnsthenon-negativeprocessIDofthenewchildprocesstotheparent,


//andreturns0tothechild.Becauseforkcreatesanewprocess,wesaythatitis


//calledonce--bytheparent--butreturnstwice--intheparentandinthechild.


//Inthechild,wecallexeclptoexecutethecommandthatwasreadfromthe


//standardinput.Thisreplacesthechildprocesswiththenewprogramfile.The


//combinationofforkfollowedbyexeciscalledspawninganewprocesson


//someoperatingsystems.IntheUNIXSystem,thetwopartsareseparatedinto


//individualfunctions.We'llsayalotmoreaboutthesefunctionsinChapter8.


//Becausethechildcallsexeclptoexecutethenewprogramfile,theparent


//wantstowaitforthechildtoterminate.Thisisdonebycallingwaitpid,


//specifyingwhichprocesstowaitfor:thepidargument,whichistheprocessID


//ofthechild.Thewaitpidfunctionalsoreturnstheterminationstatusofthe


//child--thestatusvariable--butinthissimpleprogram,wedon'tdoanything


//withthisvalue.Wecouldexamineittodeterminehowthechildterminated


//Themostfundamentallimitationofthisprogramisthatwecan'tpass


//argumentstothecommandweexecute.Wecan't,forexample,specifythename


//ofadirectorytolist.Wecanexecutelsonlyontheworkingdirectory.Toallow


//argumentswouldrequirethatweparsetheinputline,separatingthearguments


//bysomeconvention,probablyspacesortabs,andthenpasseachargumentasa


//separateparametertotheexeclpfunction.Nevertheless,thisprogramisstilla


//usefuldemonstrationoftheUNIXSystem'sprocesscontrolfunctions.


while(fgets(buf,MAXLINE,stdin)!=NULL)


{


if(buf[strlen(buf)-1]=='\n')


{


buf[strlen(buf)-1]=0;/*replacenewlinewithnull*/


}


if((pid=fork())<0)


{


err_sys("forkerror");


}


elseif(pid==0)


{/*child*/


//iftheexeclpexecutesuccessfully,execlpwillnotreturn


//ifitexecutefailed,return-1,andtheerrornumberstoreinerrnovariable


execlp(buf,buf,(char*)0);


err_ret("couldn'texecute:%s",buf);


exit(127);


}


/*parent*/


if((pid=waitpid(pid,&status,0))<0)


{


err_sys("waitpiderror");


}


printf("%%");


}


return0;


}




//functionCopyCode(key){varcodeElement=null;vartrElements=document.all.tags("ol");vari;for(i=0;i
all:shell1
shell1:shell1.c
g++-g-Wallshell1.c../lib/libapue.a-I../include-oshell1
clean:
rmshell1












内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: