/*name: panruoying*/
/*number: 5060309237*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#define MAXSIZE 99
int main(void)
{
char *readCommand(char *buffer); /*get command from keyboard*/
int isPipe(const char *input); /*get the number of pipes*/
int pipel(char * input); /*the function of pipe*/
int redirect(char *input); /*redirect of I/O*/
char *path,*buffer;
char *thecommand;
int i,pipe,k;
buffer=(char *)malloc(MAXSIZE*(sizeof(char)));
while(1)
{
path=getcwd(NULL,0); /*get the present path*/
printf("%%pry Shell%%");
thecommand=readCommand(buffer); /*read the command*/
if(thecommand==NULL)
continue;
if(strlen(thecommand)!=0)
{
k=isPipe(thecommand); /*whether there is pipe*/
if(k!=0)
pipel(thecommand);
else
redirect(thecommand); /*redirect I/O*/
}
/*release the memory space*/
free(thecommand);
free(path);
}
}
char *readCommand(char *buffer) /*get the command from keyboard*/
{
char temp_char;
char *input;
int k=0;
temp_char = getchar();
while(temp_char != '\n' && k < MAXSIZE)
{
buffer[k] = temp_char;
temp_char = getchar();
k++;
}
if(k >= MAXSIZE) /*error when the command is overlong*/
{
fprintf(stderr,"Opps! Your command is too long ! -_-b\n");
k = 0;
return NULL;
}
else
buffer[k] = '\0'; /*add the character of EOF to form a string*/
input=(char *)malloc(sizeof(char)*(k+1));
strcpy(input, buffer);
return input;
}
int isPipe(const char *input) /*get the number of pipes*/
{
int i;
for (i=0;i<strlen(input);i++)
if(input[i]=='|')
return 1;
return 0;
}
int pipel(char * input) /*the function of pipe*/
{
int redirect(char *input);
int isDaemon(char *command); /*whether the process is daemon and delete the "&"*/
int isPipe(const char *input); /*get the number of pipes*/
int status,i,j,k,*fd,back=0,len;
char **command;
int *child;
back=isDaemon(input);
len=strlen(input);
command=(char **)malloc(2*sizeof(char *));
for(i=0;i<2;i++)
command[i]=(char *)malloc((len+1)*sizeof(char));
child=(int *)malloc(2*sizeof(char *));
fd=(int *)malloc(2*sizeof(int));
/*get the two commands of the pipe*/
k=0;
j=0;
for (i=0;i<=len;i++)
{
if (input[i]!='|')
{
command[k][j]=input[i];
j++;
}
else
{
command[k][j]='\0';
k++;
j=0;
}
}
/*create the pipe*/
pipe(fd);
/*create the first child*/
if((child[0]=fork())==0)
{
close(fd[0]);
if(fd[1] != STDOUT_FILENO)
{
/*redirect the STD_OUT to the write_end of pipe,*/
/*so that the output of the subprocess is writtern into pipe*/
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
}
redirect(command[0]);
exit(1);
}
else
{
/*wait for child*/
waitpid(child[0],&status,0);
close(fd[1]);
}
/*create the second child*/
if((child[1] = fork()) == 0)
{
close(fd[1]);
if(fd[0] != STDIN_FILENO)
{
/*redirect the STD_IN to the read_end of pipe*/
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
}
redirect(command[1]);
exit(1);
}
else if(back==0)
{
waitpid(child[1], NULL, 0);
close(fd[1]);
}
/*release the memory space*/
free(fd);
for(i=0;i<2;i++)
free(command[i]);
free(command);
free(child);
return 1;
}
int redirect(char *input) /*function of redirection of I/O*/
{
char **analize(const char *input);
/*analyse the commands, get them and their arguments and store them in args*/
char *isFileExist(const char *command); /*judge if the command exit or not*/
void doCd(char *argv[]); /*the command that used particularly for "cd"*/
int number(const char *input);
/*analyse the command, get the number of its arguments, and devide the string*/
int isDaemon(char *command); /*whether the process is daemon and delete the "&"*/
char *command_path,*real_command;
char *out_filename,*in_filename;
char **analized_command;
int len,status,i,j,k,back=0,fd_out,fd_in,flag_out=0,flag_in=0;
pid_t pid;
back=isDaemon(input);
len=strlen(input);
out_filename=(char *)malloc((len+1)*(sizeof(char)));
in_filename=(char *)malloc((len+1)*(sizeof(char)));
real_command=(char *)malloc((len+1)*(sizeof(char)));
/*read the command from the string, and store it in real_command */
for(i=0;i<len;i++)
{
if (input[i]!='>'&&input[i]!='<')
real_command[i]=input[i];
else
{
if (input[i]=='>')
flag_out=1;
if (input[i]=='<')
flag_in=1;
break;
}
}
real_command[i]='\0';
i++;
/*read the whitespace in front*/
while ((input[i]==' '||input[i]==' ')&&i<len)
i++;
j=0;
out_filename[0]='\0';
in_filename[0]='\0';
/*read the direct-in or direct-out file*/
if(flag_out>0)
{
while (i<=len)
{
if(input[i]=='<')
{
out_filename[j]='\0';
break;
}
out_filename[j]=input[i];
i++;
j++;
}
}
if(flag_in>0)
while (i<=len)
{
if (input[i]=='>')
{
in_filename[j]='\0';
break;
}
in_filename[j]=input[i];
i++;
j++;
}
/*there are both redirect of in and redirect of out*/
if (i<len)
{
j=0;
if (flag_out>0&&input[i]=='<')
{
i++;
flag_in=1;
/*read the whitespace in front*/
while ((input[i]==' '||input[i]==' ')&&i<len)
i++;
while (i<=len)
{
in_filename[j]=input[i];
i++;
j++;
}
}
else if (flag_in>0&&input[i]=='>')
{
i++;
flag_out=1;
/*read the whitespace in front*/
while ((input[i]==' '||input[i]==' ')&&i<len)
i++;
while (i<=len)
{
out_filename[j]=input[i];
i++;
j++;
}
}
else
{
fprintf(stderr,"Oops!can't find the file! -_-b\n");
return -1;
}
}
k=number(real_command);
analized_command=analize(real_command);
/*the command has been stored in *analized_command*/
if(strcmp(analized_command[0], "leave") == 0) /*the command of exit*/
{
printf("THX for using~ wave~~~\n");
/*release the memory space*/
for(i=0;i<k;i++)
free(analized_command[i]);
free(analized_command);
free(real_command);
exit(1);
return 1;
}
/*if the command is cd*/
if (strcmp(analized_command[0],"cd")==0)
{
doCd(analized_command);
/*release the memory space*/
for(i=0;i<k;i++)
free(analized_command[i]);
free(analized_command);
free(real_command);
return 1;
}
command_path=isFileExist(analized_command[0]);
if(command_path==NULL) /*can't find the command*/
{
fprintf(stderr,"Oops! This command is not founded...... -_-b\n");
/*release the memory space*/
for(i=0;i<k;i++)
free(analized_command[i]);
free(analized_command);
free(real_command);
return -1;
}
pid = fork();
if(pid == 0) /*child process*/
{
/*there is the redirect of I/O*/
if(flag_out==1)
fd_out = open(out_filename,O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR );
if(flag_in==1)
fd_in = open(in_filename, O_RDONLY, S_IRUSR|S_IWUSR );
if(fd_out==-1)
{
printf("Oops! Open out %s error -_-b\n", out_filename);
return -1;
}
if(fd_in==-1)
{
fprintf(stderr,"Oops! Open in %s error -_-b\n", in_filename);
return -1;
}
/*redirect the STD_OUT to the fd_out by dup2*/
if(flag_out>0)
dup2(fd_out, STDOUT_FILENO);
/*redirect the STD_IN to the fd_in by dup2*/
if(flag_in>0)
dup2(fd_in,STDIN_FILENO);
execv(command_path,analized_command);
exit(1);
}
else /*parent process */
if(back==0) /*it is not the daemon process*/
pid=waitpid(pid, &status, 0);
/*release the memory space*/
free(out_filename);
free(in_filename);
free(command_path);
for(i=0;i<k;i++)
free(analized_command[i]);
free(analized_command);
return 1;
}
int isDaem
Minix系统shell命令解释器
4星 · 超过85%的资源 需积分: 10 169 浏览量
2011-04-07
21:13:55
上传
评论
收藏 248KB RAR 举报
Stellayqm
- 粉丝: 0
- 资源: 2
最新资源
- python-leetcode面试题解之第157题用Read4读取N个字符-题解.zip
- python-leetcode面试题解之第156题上下翻转二叉树-题解.zip
- python-leetcode面试题解之第155题最小栈-题解.zip
- python-leetcode面试题解之第153题寻找旋转排序数组中的最小值-题解.zip
- python-leetcode面试题解之第152题乘积最大子数组-题解.zip
- python-leetcode面试题解之第151题反转字符串中的单词-题解.zip
- python-leetcode面试题解之第150题逆波兰表达式求值-题解.zip
- python-leetcode面试题解之第149题直线上最多的点数-题解.zip
- python-leetcode面试题解之第148题排序链表-题解.zip
- python-leetcode面试题解之第147题对链表进行插入排序-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页