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
shell1:shell1.c
g++-g-Wallshell1.c../lib/libapue.a-I../include-oshell1
clean:
rmshell1
相关文章推荐
- UNIX环境高级编程 chat1 从标准输入中读取命令执行
- 从标准输入读入命令并执行【UNIX 环境高 3ff0 级编程】
- UNIX环境高级编程1-3用标准I/O将标准输入复制到标准输出
- UNIX环境高级编程程序1-2将标准输入复制到标准输出
- 从标准输入中读命令并执行
- unix环境高级编程-5.8-标准I/O的效率。
- UNIX环境高级编程-第5章- 标准 I/O 库
- APUE 1-10 从标准输入读命令并执行
- (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO
- unix环境高级编程1
- 1.5 从标准输入读命令并执行
- APUE 1-7 从标准输入读命令并执行
- 笔记1-3: 从标准输入读取命令并执行
- Linux/UNIX脚本编程学习笔记(0807)---输入、输出、命令执行
- 1.8 从标准输入读命令并执行2
- 笔记1-3: 从标准输入读取命令并执行
- UNIX环境高级编程一书中见到的命令
- unix 环境高级编程 chap5 - 标准I/O
- 从标准输入读命令并执行
- UNIX环境高级编程5.9二进制IO 输入输出流理解