diff --git a/include/built_in.h b/include/built_in.h index b2df0aa845f50b0ba45508d0418a3fbe9643579c..c211d42a89d2bd7c31bbd653d4ee0afff3e68d03 100644 --- a/include/built_in.h +++ b/include/built_in.h @@ -22,6 +22,8 @@ struct built_in_command If success, return 0. Else if arguments are not valid, return -1. */ + +int do_sleep(int argc,char** argv); int do_cd(int argc, char** argv); /** @@ -57,5 +59,5 @@ int validate_cd_argv(int argc, char** argv); int validate_pwd_argv(int argc, char** argv); int validate_fg_argv(int argc, char** argv); - +int validate_sleep_argv(int argc,char** argv); #endif // BUILT_IN_H_ diff --git a/include/commands.h b/include/commands.h index c0b66fecb66fd5863c4e9b9ab363c3a053eb37bc..c66386b321f797be467bec765f7b80a7342b1890 100644 --- a/include/commands.h +++ b/include/commands.h @@ -10,5 +10,5 @@ struct single_command int evaluate_command(int n_commands, struct single_command (*commands)[512]); void free_commands(int n_commands, struct single_command (*commands)[512]); - +int background_st; #endif // MYSH_COMMANDS_H_ diff --git a/src/built_in.c b/src/built_in.c index 9a466e91501d5877107fb67e308275c4847bcb38..a8be026499dbbeb7144ede9bffa01ba23d80c162 100644 --- a/src/built_in.c +++ b/src/built_in.c @@ -1,6 +1,6 @@ #include <stdio.h> #include <string.h> - +#include "commands.h" #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> @@ -8,6 +8,11 @@ #include "built_in.h" +int do_sleep(int argc, char** argv){ + +sleep(100); + +} int do_cd(int argc, char** argv) { if (!validate_cd_argv(argc, argv)) return -1; @@ -33,13 +38,18 @@ int do_pwd(int argc, char** argv) { } int do_fg(int argc, char** argv) { - if (!validate_fg_argv(argc, argv)) - return -1; - // TODO: Fill this. + if (!validate_fg_argv(argc, argv)) + return -1; + else{ + if(background_st!=0){printf("%d running",background_st);} + //TODO: Fill this. return 0; + } } +int validate_sleep_argv(int argc,char **argv){ +return 1;} int validate_cd_argv(int argc, char** argv) { if (argc != 2) return 0; diff --git a/src/commands.c b/src/commands.c index 13e9c330aedcb9d3c1c0979a5b22fd7844516ada..21d00dfc1698ac76313be220357c4b5fbad76a0e 100644 --- a/src/commands.c +++ b/src/commands.c @@ -2,14 +2,19 @@ #include <stdlib.h> #include <string.h> #include <assert.h> - +#include <error.h> #include "commands.h" #include "built_in.h" - +#include <unistd.h> +#include <sys/wait.h> +#include <sys/types.h> +#include "signal_handlers.h" +extern int background_st; static struct built_in_command built_in_commands[] = { { "cd", do_cd, validate_cd_argv }, { "pwd", do_pwd, validate_pwd_argv }, - { "fg", do_fg, validate_fg_argv } + { "fg", do_fg, validate_fg_argv }, + {"slp",do_sleep,validate_sleep_argv} }; static int is_built_in_command(const char* command_name) @@ -30,34 +35,97 @@ static int is_built_in_command(const char* command_name) */ int evaluate_command(int n_commands, struct single_command (*commands)[512]) { - if (n_commands > 0) { - struct single_command* com = (*commands); + + char mk_absolute_path[55][55]={{"/usr/local/bin/"},{"/bin/"},{"/usr/bin/"},{"/usr/sbin/"},{"/sbin/"}}; //make absolute path + char tmp_absolute_path[100]={'\0',}; + char absolute_path[100]={'\0',}; + - assert(com->argc != 0); + int background=0; + if (n_commands >0) { + + + + struct single_command* com = (*commands); + + if(*(com->argv[(com->argc)-1])=='&'){ + + background=1; + (com+0)->argc--; + com->argv[(com->argc)]=NULL; + } + assert(com->argc != 0); int built_in_pos = is_built_in_command(com->argv[0]); - if (built_in_pos != -1) { - if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv)) { - if (built_in_commands[built_in_pos].command_do(com->argc, com->argv) != 0) { - fprintf(stderr, "%s: Error occurs\n", com->argv[0]); - } - } else { - fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]); + + if (built_in_pos != -1) + { + if (built_in_commands[built_in_pos].command_validate(com->argc, com->argv)) + { + if (built_in_commands[built_in_pos].command_do(com->argc, com->argv) != 0) { + fprintf(stderr, "%s: Error occurs\n", com->argv[0]); + } + } + else + { + fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]); return -1; - } - } else if (strcmp(com->argv[0], "") == 0) { - return 0; - } else if (strcmp(com->argv[0], "exit") == 0) { - return 1; - } else { - fprintf(stderr, "%s: command not found\n", com->argv[0]); - return -1; + } + + } + else if (strcmp(com->argv[0], "") == 0) { + return 0; + } + else if (strcmp(com->argv[0], "exit") == 0) { + return 1; } - } - return 0; -} + else + { + int success_execv; + pid_t pid=fork(); + if(pid>0) + { + if(background==1) + {background_st=pid; return 0;}// + else + {waitpid(pid,0,0);printf("END");return 0;} + } + + else if(pid==0) + { + if( background==0) + { if(!(execv(com->argv[0],com->argv))) + { for(int i=0;i<5;i++) + { + tmp_absolute_path[0]='\0'; + strcpy(tmp_absolute_path,mk_absolute_path[i]); + strcat(tmp_absolute_path,com->argv[0]); + + success_execv=execv(tmp_absolute_path,com->argv); + // execv(tmp_absolute_path,com->argv); + } + + printf("not found\n"); exit(0);} + } + + else + { + int background_st=getpid(); + if(background==1){sleep(2); printf("%d\n",getpid());} return 0; + + } + } + + else + { printf("error : fork failure"); exit(0);} + + } + + } + else{return 0;} +} void free_commands(int n_commands, struct single_command (*commands)[512]) { for (int i = 0; i < n_commands; ++i) { @@ -74,3 +142,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512]) memset((*commands), 0, sizeof(struct single_command) * n_commands); } + diff --git a/src/main.c b/src/main.c index 77c78049273e013e278ad9021fa088531b636a97..a32aec42ccea234bce56c86cc4b0a5a35db5bc2c 100644 --- a/src/main.c +++ b/src/main.c @@ -1,31 +1,52 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> - +#include "signal_handlers.h" #include "commands.h" #include "built_in.h" #include "utils.h" - +#include <sys/types.h> +#include <sys/wait.h> +#include<signal.h> int main() { +extern int background_st; char buf[8096]; + + while (1) { - fgets(buf, 8096, stdin); + signal(SIGINT,(void*)catch_sigint); + signal(SIGTSTP,(void*)catch_sigtstp); + + + + fgets(buf, 8096, stdin); + + + + + + + struct single_command commands[512]; + int n_commands = 0; + + mysh_parse_command(buf, &n_commands, &commands); + + + - struct single_command commands[512]; - int n_commands = 0; - mysh_parse_command(buf, &n_commands, &commands); int ret = evaluate_command(n_commands, &commands); free_commands(n_commands, &commands); n_commands = 0; - if (ret == 1) { + if (ret == 1) { // if argv[]= exit->break; break; } } return 0; } + diff --git a/src/signal_handlers.c b/src/signal_handlers.c index 4b6fe2e073f327917964b5327b6649f74bcbda1e..858c3f9abbb32341aafbeb37347445addcee2b16 100644 --- a/src/signal_handlers.c +++ b/src/signal_handlers.c @@ -1,11 +1,29 @@ +#define _POSIX_SOURCE #include "signal_handlers.h" - +#include<unistd.h> +#include<signal.h> +#include<stdio.h> +#include"commands.h" +#include<sys/types.h> +#include<sys/wait.h> void catch_sigint(int signalNo) { - // TODO: File this! +//TODO: File this! + + printf("your input CTRL+C shell doesn't Close."); +printf("\n"); + + + } void catch_sigtstp(int signalNo) { - // TODO: File this! + +printf("your input CTRL+X shell doesn't Close."); + + +printf("\n"); + +//TODO:File this! } diff --git a/src/utils.c b/src/utils.c index bbe6ed098ec9a4b7e0548d84f5c3f49260d5a8a7..dc21d0fcd222c2b1f8118213ae4819ce413fd1fe 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,9 +1,10 @@ -#include "utils.h" +#include "utils.h" #define _GNU_SOURCE #include <stdlib.h> #include <string.h> - +#include <stdio.h> +#include "commands.h" void mysh_parse_command(const char* command, int* n_commands, struct single_command (*commands)[]) @@ -21,11 +22,15 @@ void mysh_parse_command(const char* command, parse_single_command(tok, &com->argc, &com->argv); ++ti; - + tok = strtok_r(NULL, "|", &saveptr); + } - *n_commands = ti; + *n_commands = ti;//if pipe that 2 notpipe that 1 + + + } void parse_single_command(const char* command, @@ -60,4 +65,6 @@ void parse_single_command(const char* command, (*argv)[0] = (char*)malloc(1); (*argv)[0][0] = '\0'; } + + }