#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <signal.h>
|
|
#include<fcntl.h>
|
|
|
|
#include "mytop.c"
|
|
//#include "fun.h"
|
|
|
|
|
|
void eval(char *cmdline)
|
|
{
|
|
char *argv[MAXLINE];
|
|
char buf[MAXLINE];
|
|
int isBg;
|
|
int pid;
|
|
strcpy(buf,cmdline);
|
|
isBg = parseline(cmdline,argv);
|
|
if(argv[0] == NULL)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if(!builtin_cmd(argv))
|
|
{
|
|
if((pid=fork())==0)
|
|
{
|
|
if(isBg)
|
|
{
|
|
int fd = open( "/dev/null", O_RDWR );
|
|
dup2( fd, 0 );
|
|
dup2( fd, 1 );
|
|
signal(SIGCHLD, SIG_IGN);
|
|
}
|
|
if(execvp(argv[0],argv)<0)
|
|
{
|
|
printf("%s:Command not found.\n",argv[0]);
|
|
exit(0);
|
|
}
|
|
}
|
|
if(!isBg)
|
|
{
|
|
wait(0);
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
signal(SIGCHLD, SIG_IGN);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
int parseline(const char *cmdline, char **argv)
|
|
{
|
|
static char array[MAXLINE];
|
|
char *buf = array;
|
|
char *delim;
|
|
int argc;
|
|
int bg;
|
|
|
|
strcpy(buf, cmdline);
|
|
buf[strlen(buf)-1] = ' ';
|
|
while (*buf && (*buf == ' '))
|
|
buf++;
|
|
|
|
argc = 0;
|
|
if (*buf == '\'')
|
|
{
|
|
buf++;
|
|
delim = strchr(buf, '\'');
|
|
}
|
|
else
|
|
{
|
|
delim = strchr(buf, ' ');
|
|
}
|
|
|
|
while (delim)
|
|
{
|
|
argv[argc++] = buf;
|
|
*delim = '\0';
|
|
buf = delim + 1;
|
|
while (*buf && (*buf == ' '))
|
|
{
|
|
buf++;
|
|
}
|
|
|
|
if (*buf == '\'')
|
|
{
|
|
buf++;
|
|
delim = strchr(buf, '\'');
|
|
}
|
|
else
|
|
{
|
|
delim = strchr(buf, ' ');
|
|
}
|
|
}
|
|
argv[argc] = NULL;
|
|
|
|
if (argc == 0)
|
|
return 1;
|
|
|
|
if ((bg = (*argv[argc-1] == '&')) != 0)
|
|
{
|
|
argv[--argc] = NULL;
|
|
}
|
|
return bg;
|
|
}
|
|
|
|
int builtin_cmd(char **argv)
|
|
{
|
|
if(Is_pipe(argv)==1)
|
|
{
|
|
return 1;
|
|
}
|
|
if(Is_redirect(argv)==1)
|
|
{
|
|
return 1;
|
|
}
|
|
if(!strcmp(argv[0],"quit")||!strcmp(argv[0],"exit"))
|
|
{
|
|
exit(0);
|
|
}
|
|
if(!strcmp(argv[0],"history"))
|
|
{
|
|
print_history(argv[1]);
|
|
return 1;
|
|
}
|
|
if(!strcmp(argv[0],"cd"))
|
|
{
|
|
if (argv[1] == NULL)
|
|
{
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
if (chdir(argv[1]) != 0)
|
|
{
|
|
perror("Error: ");
|
|
}
|
|
return 1;
|
|
}
|
|
}
|
|
if(!strcmp(argv[0],"mytop"))
|
|
{
|
|
mytop();
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|