#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include<fcntl.h>
|
|
|
|
static int num = 0;
|
|
static char storage[MAXLINE][MAXLINE];
|
|
|
|
void history(char *cmdline)
|
|
{
|
|
strcpy(storage[num], cmdline);
|
|
num++;
|
|
}
|
|
|
|
void print_history(char *argv)
|
|
{
|
|
int i = 0;
|
|
if(argv == NULL)
|
|
{
|
|
for(; i<num; i++)
|
|
{
|
|
fputs(storage[i], stdout);
|
|
}
|
|
return;
|
|
}
|
|
i = num - atoi(argv);
|
|
if(i < 0)
|
|
{
|
|
i = 0;
|
|
}
|
|
for(; i<num; i++)
|
|
{
|
|
fputs(storage[i], stdout);
|
|
}
|
|
}
|
|
|
|
|
|
void shell_pip(char **prog1, char **prog2)
|
|
{
|
|
int des_p[2];
|
|
if(pipe(des_p) == -1)
|
|
{
|
|
perror("Pipe failed");
|
|
exit(1);
|
|
}
|
|
if(fork() == 0)
|
|
{
|
|
close(1);
|
|
dup(des_p[1]);
|
|
close(des_p[0]);
|
|
close(des_p[1]);
|
|
execvp(prog1[0], prog1);
|
|
perror("execvp of prog1 failed");
|
|
exit(1);
|
|
}
|
|
if(fork() == 0)
|
|
{
|
|
close(0);
|
|
dup(des_p[0]);
|
|
close(des_p[1]);
|
|
close(des_p[0]);
|
|
execvp(prog2[0], prog2);
|
|
perror("execvp of prog2 failed");
|
|
exit(1);
|
|
}
|
|
close(des_p[0]);
|
|
close(des_p[1]);
|
|
wait(0);
|
|
wait(0);
|
|
}
|
|
|
|
int Is_pipe(char **argv)
|
|
{
|
|
int i = 0;
|
|
int Ispipe = 0;
|
|
char *cmd_0[MAXLINE];
|
|
char *cmd_1[MAXLINE];
|
|
while (argv[i]!=NULL)
|
|
{
|
|
if (argv[i][0] == '|')
|
|
{
|
|
Ispipe = 1;
|
|
}
|
|
i++;
|
|
}
|
|
if(Ispipe == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
i = 0;
|
|
while (argv[i][0] != '|')
|
|
{
|
|
cmd_0[i] = argv[i];
|
|
i++;
|
|
}
|
|
cmd_0[i] = NULL;
|
|
i++;
|
|
int j = 0;
|
|
while (argv[i]!=NULL)
|
|
{
|
|
cmd_1[j] = argv[i];
|
|
i++;
|
|
j++;
|
|
}
|
|
cmd_1[j] = NULL;
|
|
shell_pip(cmd_0, cmd_1);
|
|
return 1;
|
|
}
|
|
|
|
|
|
void shell_direct(char **prog1, char **prog2, char Isopen)
|
|
{
|
|
int fd;
|
|
if(Isopen == '>')
|
|
{
|
|
if(fork() == 0)
|
|
{
|
|
if( (fd=open(prog2[0],O_RDWR | O_CREAT | O_NOCTTY | O_NDELAY))<0 )
|
|
{
|
|
perror("open");
|
|
exit(0);
|
|
}
|
|
close(1);
|
|
dup(fd);
|
|
close(fd);
|
|
execvp(prog1[0], prog1);
|
|
remove(prog2[0]);
|
|
exit(0);
|
|
}
|
|
wait(0);
|
|
if(fork() == 0)
|
|
{
|
|
if( (fd=open(prog1[0],O_RDWR | O_NOCTTY | O_NDELAY))<0 )
|
|
{
|
|
exit(0);
|
|
}
|
|
close(0);
|
|
dup(fd);
|
|
close(fd);
|
|
execvp(prog2[0], prog2);
|
|
exit(0);
|
|
}
|
|
wait(0);
|
|
}
|
|
if(Isopen == '<')
|
|
{
|
|
if(fork() == 0)
|
|
{
|
|
if( (fd=open(prog2[0],O_RDWR | O_NOCTTY | O_NDELAY))<0 )
|
|
{
|
|
perror("open");
|
|
exit(0);
|
|
}
|
|
close(0);
|
|
dup(fd);
|
|
close(fd);
|
|
execvp(prog1[0], prog1);
|
|
exit(0);
|
|
}
|
|
wait(0);
|
|
}
|
|
return;
|
|
}
|
|
|
|
int Is_redirect(char **argv)
|
|
{
|
|
int i = 0;
|
|
char Isopen = '0';
|
|
char *cmd_0[MAXLINE];
|
|
char *cmd_1[MAXLINE];
|
|
while (argv[i]!=NULL)
|
|
{
|
|
if (argv[i][0] == '>')
|
|
{
|
|
Isopen = '>';
|
|
}
|
|
if (argv[i][0] == '<')
|
|
{
|
|
Isopen = '<';
|
|
}
|
|
i++;
|
|
}
|
|
if(Isopen == '0')
|
|
{
|
|
return 0;
|
|
}
|
|
i = 0;
|
|
while (argv[i][0] != '>' && argv[i][0] != '<')
|
|
{
|
|
cmd_0[i] = argv[i];
|
|
i++;
|
|
}
|
|
cmd_0[i] = NULL;
|
|
i++;
|
|
int j = 0;
|
|
while (argv[i]!=NULL)
|
|
{
|
|
cmd_1[j] = argv[i];
|
|
i++;
|
|
j++;
|
|
}
|
|
cmd_1[j] = NULL;
|
|
|
|
shell_direct(cmd_0, cmd_1, Isopen);
|
|
return 1;
|
|
}
|