Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

198 linhas
3.4 KiB

#include <string.h>
#include<fcntl.h>
//#include "fun.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++)
puts(storage[i]);
return;
}
i = num - atoi(argv);
if(i < 0)
{
i = 0;
}
for(; i<num; i++)
puts(storage[i]);
}
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)
{
int fd;
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);
}
int Is_redirect(char **argv)
{
int i = 0;
int Isopen = 0;
char *cmd_0[MAXLINE];
char *cmd_1[MAXLINE];
while (argv[i]!=NULL)
{
if (argv[i][0] == '>')
{
Isopen = 1;
}
i++;
}
if(Isopen == 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_direct(cmd_0, cmd_1);
return 1;
}
/*
void mytop()
{
FILE *fp;
unsigned int pagesize;
unsigned long total, free, largest, cached;
if ((fp = fopen("/proc/meminfo", "r")) == NULL)
return 0;
if (fscanf(fp, "%u %lu %lu %lu %lu", &pagesize, &total, &free, &largest, &cached) != 5)
{
fclose(fp);
return 0;
}
fclose(fp);
printf("main memory: %ldK total, %ldK free, %ldK contig free, %ldK cached\n",
(pagesize * total)/1024, (pagesize * free)/1024,
(pagesize * largest)/1024, (pagesize * cached)/1024);
return 1;
}
*/