操作系统实验报告 shell程序Word文档格式.docx
《操作系统实验报告 shell程序Word文档格式.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告 shell程序Word文档格式.docx(20页珍藏版)》请在冰豆网上搜索。
注#include<
signal.h>
释)#include<
stdlib.h>
#include"
ysh.h"
#defineNO_PIPE-1
#defineFD_READ0
#defineFD_WRITE1
intis_founded(char*cmd)
{
intk=0;
while(envpath[k]!
=NULL){strcpy(buf,envpath[k]);
strcat(buf,cmd);
if(access(buf,F_OK)==0){return1;
}k++;
}
return0;
voidgetenviron(intn,char*s){inti=0,j=0,k=0;
charc;
charbuff[80];
char*p;
while((c=s[i])!
='
='
){buff[i++]=c;
}buff[i++]='
\0'
;
if(strcmp(buff,"
PATH"
)==0){
while(s[i]!
){if(s[i]=='
:
'
){
buff[j++]='
/'
buff[j]='
p=(char*)malloc(strlen(buff)+1);
strcpy(p,buff);
envpath[k++]=p;
envpath[k]=NULL;
j=0;
i++;
}else{
buff[j]=s[i];
j++;
}else
fprintf(stderr,"
Nomatch"
);
}
intgetline(intfd,char*buf)
{inti=0;
while(read(fd,&
c,1)){
buf[i++]=c;
if(c=='
\n'
buf[i-1]='
returni;
}returni;
voidinit_environ()
intfd,n;
charbuf[80];
if((fd=open("
ysh_profile"
O_RDONLY,660))==-1){
printf("
initenvironvariableerror!
\n"
exit
(1);
while((n=getline(fd,buf))!
=0){
getenviron(n,buf);
envhis.start=0;
envhis.end=0;
head=end=NULL;
intpipel(char*input,intlen)
{char*argv[10][30];
char*filename[0];
inti,j,k,is_bg=0;
intli_cmd=0;
intfd[10][1],pipe_in=-1;
intpipe_out=-1,flag=0;
pid_tpid;
for(i=0,j=0,k=0;
i<
=len;
i++){
if((input[i]=='
'
)||(input[i]=='
\t'
)||
(input[i]=='
|'
>
)){
if(input[i]=='
flag=1;
if(j>
0){
buf[j++]='
argv[li_cmd][k]=(char*)malloc(sizeof(char)*j);
strcpy(argv[li_cmd][k],buf);
k++;
argv[li_cmd][k]=(char*)0;
li_cmd++;
k=0;
if(j==0){
continue;
if(flag==0){
}else{
filename[0]=(char*)malloc(sizeof(char)*j);
strcpy(filename[0],buf);
}else{
&
)&
(input[i]=='
)){
is_bg=1;
buf[j++]=input[i];
argv[li_cmd][k++]=NULL;
for(i=0;
=10;
fd[i][FD_READ]=NO_PIPE;
fd[i][FD_WRITE]=NO_PIPE;
li_cmd;
if(pipe(fd[i])==-1){
Cannotopenpipe!
if(is_founded(argv[i][0])==0){
Cannotfoundcommand!
break;
if(i!
pipe_in=fd[i-1][FD_READ];
pipe_in=NO_PIPE;
=li_cmd){
pipe_out=fd[i][FD_WRITE];
{if(flag==1){
if((pipe_out=open(filename[0],O_WRONLY|O_CREAT|O_TRUNC,
S_IRUSR|S_IWUSR))==-1){
Cannotopen%s\n"
filename[0]);
pipe_out=NO_PIPE;
if((pid=fork())<
0){
Forkfailed!
if(pid==0){
if(pipe_in==NO_PIPE){
close(pipe_in);
if(pipe_out==NO_PIPE){
close(pipe_out);
if(pipe_out!
=NO_PIPE){
dup2(pipe_out,1);
if(pipe_in!
dup2(pipe_in,0);
execv(buf,argv[i]);
if(is_bg==0){
waitpid(pid,NULL,0);
voidadd_history(char*inputcmd){
envhis.end=(envhis.end+1)%HISNUM;
if(envhis.end==envhis.start){
envhis.start=(envhis.start+1)%HISNUM;
strcpy(envhis.his_cmd[envhis.end],inputcmd);
voidhistory_cmd()
inti,j=0;
if(envhis.start==envhis.end){
return;
elseif(envhis.start<
envhis.end){
for(i=envhis.start+1;
=envhis.end;
%d\t%s\n"
j,envhis.his_cmd[i]);
HISNUM;
voidcd_cmd(char*route)
if(route!
=NULL){
if(chdir(route)<
0)
cd;
%sErrorfileordirectory!
route);
voidjobs_cmd()
structNODE*p;
inti=1;
p=head;
if(head!
do
%d%d%s\t%s\n"
i,p->
pid,p->
state,p->
cmd);
p=p->
link;
}while(p!
=NULL);
else
Nojobs!
voidadd_node(char*input_cmd,intnode_pid){
p=(structNODE*)malloc(sizeof(structNODE));
p->
pid=node_pid;
strcpy(p->
state,input_cmd);
state,"
running"
link=NULL;
if(head==NULL){
head=p;
end=p;
end->
link=p;
voiddel_node(intsig,siginfo_t*sip)
structNODE*q;
intid;
if(sig_z==1){
sig_z=0;
gotoout;
id=sip->
si_pid;
p=q=head;
if(head==NULL)
while(p->
pid!
=id&
p->
link!
if(p->
=id)
if(p==head)
head=head->
else{
while(q->
=p){
q=q->
if(p==end){
end=q;
q->
link=p->
free(p);
out:
voidsetflag(){
sig_flag=1;
voidctrl_z()
if(pid1==0){
while((p->
=pid1)&
(p->
=NULL)){
pid==pid1){
stopped"
add_node(input,pid1);
strcpy(end->
sig_z=1;
kill(pid1,SIGSTOP);
for(p=head;
=pid1;
link)
[%d]\t%s\t%s\n"
i,end->
state,end->
pid1=0;
voidbg_cmd(intjob_num){
inti=0;
for(i=1;
job_num;
i++)
kill(p->
pid,SIGCONT);
voidfg_cmd(intjob_num){
strcpy(input,p->
pid1=p->
pid;
signal(SIGTSTP,ctrl_z);
waitpid(p->
pid,NULL,0);
intmain()
init_environ();
while
(1)
char*arg[20];
inti=0,j=0,k=0;
intis_pr=0,is_bg=0;
intinput_len=0,path;
intpid=0,status=0;
structsigactionaction;
action.sa_sigaction=del_node;
sigfillset(&
action.sa_mask);
action.sa_flags=SA_SIGINFO;
sigaction(SIGCHLD,&
action,NULL);
path=get_current_dir_name();
ysh@%s"
path);
while(((c=getchar())=='
)||(c=='
)||(c==EOF))
)
while(c!
buf[input_len++]=c;
c=getchar();
buf[input_len]='
input=(char*)malloc(sizeof(char)*(input_len+1));
strcpy(input,buf);
=input_len;
<
))
pipel(input,input_len);
add_history(input);
free(input);
redirect(input,input_len);
is_pr=1;
if(is_pr==1)
if(j==0)
arg[k]=(char*)malloc(sizeof(char)*j);
strcpy(arg[k++],buf);
(input[i+1]=='
if(strcmp(arg[0],"
exit"
)==0)
Byebye!
history"
history_cmd();
cd"
for(i=3,j=0;
buf[j]='
arg[1]=(char*)malloc(sizeof(char)*j);
strcpy(arg[1],buf);
cd_cmd(arg[1]);
jobs"
jobs_cmd();
bg"
%'
for(;
bg_cmd(atoi(arg[1]));
fg"
){
fg_cmd(atoi(arg[1]));
if(is_pr==0)
arg[k]=(char*)malloc(sizeof(char));
arg[k]=NULL;
if(is_founded(arg[0])==0)
Thiscommandisnotfouned!
=k;
free(arg[i]);