Skip to content
Snippets Groups Projects
Commit 59c1515a authored by hwi's avatar hwi
Browse files

201620914

parent 98fbc382
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,11 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include "signal_handlers.h"
#include "commands.h"
#include "built_in.h"
......@@ -30,6 +34,13 @@ static int is_built_in_command(const char* command_name)
*/
int evaluate_command(int n_commands, struct single_command (*commands)[512])
{
signal(SIGINT,catch_sigint);
signal(SIGTSTP,catch_sigtstp);
char *ls[] = {"ls",NULL};
char *cat[]={"cat","/etc/hosts",NULL};
char *vim[]={"/usr/bin/vim",NULL};
char *path[]={"Path=/usr/local/bin:usr/bin:/bin:.usr/sbin:/sbin",NULL};
if (n_commands > 0) {
struct single_command* com = (*commands);
......@@ -45,11 +56,34 @@ int evaluate_command(int n_commands, struct single_command (*commands)[512])
fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]);
return -1;
}
} else if (strcmp(com->argv[0], "") == 0) {
}
else if(strcmp(com->argv[0],"")==0){
return 0;
} else if (strcmp(com->argv[0], "exit") == 0) {
}
else if(strcmp(com->argv[0],"exit")==0){
return 1;
} else {
}
else if(built_in_pos == -1){
pid_t pid = fork();
if(pid<0){
perror("fork error");
}
if(pid == 0){//child
if(strcmp(com->argv[0],"ls")==0)
execve("/bin/ls",ls,path);
else if(strcmp(com->argv[0],"cat")==0)
execve("/bin/cat",cat,path);
else if(strcmp(com->argv[0],"vim")==0)
execve("/usr/bin/vim",vim,path);
else execv(com->argv[0],com->argv);
}
else{//parent
wait(0);
}
}
else {
fprintf(stderr, "%s: command not found\n", com->argv[0]);
return -1;
}
......
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "signal_handlers.h"
void catch_sigint(int signalNo)
{
// TODO: File this!
{//ctrl+c
signal(SIGINT,SIG_IGN);
signal(SIGINT,catch_sigint);
}
void catch_sigtstp(int signalNo)
{
// TODO: File this!
{//ctrl+z
signal(SIGTSTP,SIG_IGN);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment