Skip to content
Snippets Groups Projects
Commit ce0d6da3 authored by KimTaeYun's avatar KimTaeYun
Browse files

i don't know about infinite print i'sorry

parent 98fbc382
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,8 @@ struct built_in_command ...@@ -22,6 +22,8 @@ struct built_in_command
If success, return 0. If success, return 0.
Else if arguments are not valid, return -1. Else if arguments are not valid, return -1.
*/ */
int do_sleep(int argc,char** argv);
int do_cd(int argc, char** argv); int do_cd(int argc, char** argv);
/** /**
...@@ -57,5 +59,5 @@ int validate_cd_argv(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_pwd_argv(int argc, char** argv);
int validate_fg_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_ #endif // BUILT_IN_H_
...@@ -10,5 +10,5 @@ struct single_command ...@@ -10,5 +10,5 @@ struct single_command
int evaluate_command(int n_commands, struct single_command (*commands)[512]); int evaluate_command(int n_commands, struct single_command (*commands)[512]);
void free_commands(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_ #endif // MYSH_COMMANDS_H_
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "commands.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
...@@ -8,6 +8,11 @@ ...@@ -8,6 +8,11 @@
#include "built_in.h" #include "built_in.h"
int do_sleep(int argc, char** argv){
sleep(100);
}
int do_cd(int argc, char** argv) { int do_cd(int argc, char** argv) {
if (!validate_cd_argv(argc, argv)) if (!validate_cd_argv(argc, argv))
return -1; return -1;
...@@ -33,13 +38,18 @@ int do_pwd(int argc, char** argv) { ...@@ -33,13 +38,18 @@ int do_pwd(int argc, char** argv) {
} }
int do_fg(int argc, char** argv) { int do_fg(int argc, char** argv) {
if (!validate_fg_argv(argc, argv)) if (!validate_fg_argv(argc, argv))
return -1; return -1;
else{
if(background_st!=0){printf("%d running",background_st);}
//TODO: Fill this. //TODO: Fill this.
return 0; return 0;
} }
}
int validate_sleep_argv(int argc,char **argv){
return 1;}
int validate_cd_argv(int argc, char** argv) { int validate_cd_argv(int argc, char** argv) {
if (argc != 2) return 0; if (argc != 2) return 0;
......
...@@ -2,14 +2,19 @@ ...@@ -2,14 +2,19 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <error.h>
#include "commands.h" #include "commands.h"
#include "built_in.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[] = { static struct built_in_command built_in_commands[] = {
{ "cd", do_cd, validate_cd_argv }, { "cd", do_cd, validate_cd_argv },
{ "pwd", do_pwd, validate_pwd_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) static int is_built_in_command(const char* command_name)
...@@ -30,34 +35,97 @@ 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]) int evaluate_command(int n_commands, struct single_command (*commands)[512])
{ {
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',};
int background=0;
if (n_commands >0) { if (n_commands >0) {
struct single_command* com = (*commands); 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); assert(com->argc != 0);
int built_in_pos = is_built_in_command(com->argv[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_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) { if (built_in_commands[built_in_pos].command_do(com->argc, com->argv) != 0) {
fprintf(stderr, "%s: Error occurs\n", com->argv[0]); fprintf(stderr, "%s: Error occurs\n", com->argv[0]);
} }
} else { }
else
{
fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]); fprintf(stderr, "%s: Invalid arguments\n", com->argv[0]);
return -1; return -1;
} }
} else if (strcmp(com->argv[0], "") == 0) {
}
else if (strcmp(com->argv[0], "") == 0) {
return 0; return 0;
} else if (strcmp(com->argv[0], "exit") == 0) { }
else if (strcmp(com->argv[0], "exit") == 0) {
return 1; return 1;
} else {
fprintf(stderr, "%s: command not found\n", com->argv[0]);
return -1;
} }
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);}
} }
return 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]) void free_commands(int n_commands, struct single_command (*commands)[512])
{ {
for (int i = 0; i < n_commands; ++i) { for (int i = 0; i < n_commands; ++i) {
...@@ -74,3 +142,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512]) ...@@ -74,3 +142,4 @@ void free_commands(int n_commands, struct single_command (*commands)[512])
memset((*commands), 0, sizeof(struct single_command) * n_commands); memset((*commands), 0, sizeof(struct single_command) * n_commands);
} }
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "signal_handlers.h"
#include "commands.h" #include "commands.h"
#include "built_in.h" #include "built_in.h"
#include "utils.h" #include "utils.h"
#include <sys/types.h>
#include <sys/wait.h>
#include<signal.h>
int main() int main()
{ {
extern int background_st;
char buf[8096]; char buf[8096];
while (1) { while (1) {
signal(SIGINT,(void*)catch_sigint);
signal(SIGTSTP,(void*)catch_sigtstp);
fgets(buf, 8096, stdin); fgets(buf, 8096, stdin);
struct single_command commands[512]; struct single_command commands[512];
int n_commands = 0; int n_commands = 0;
mysh_parse_command(buf, &n_commands, &commands); mysh_parse_command(buf, &n_commands, &commands);
int ret = evaluate_command(n_commands, &commands); int ret = evaluate_command(n_commands, &commands);
free_commands(n_commands, &commands); free_commands(n_commands, &commands);
n_commands = 0; n_commands = 0;
if (ret == 1) { if (ret == 1) { // if argv[]= exit->break;
break; break;
} }
} }
return 0; return 0;
} }
#define _POSIX_SOURCE
#include "signal_handlers.h" #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) 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) void catch_sigtstp(int signalNo)
{ {
printf("your input CTRL+X shell doesn't Close.");
printf("\n");
//TODO:File this! //TODO:File this!
} }
#include "utils.h"
#include "utils.h"
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h>
#include "commands.h"
void mysh_parse_command(const char* command, void mysh_parse_command(const char* command,
int* n_commands, int* n_commands,
struct single_command (*commands)[]) struct single_command (*commands)[])
...@@ -23,9 +24,13 @@ void mysh_parse_command(const char* command, ...@@ -23,9 +24,13 @@ void mysh_parse_command(const char* command,
++ti; ++ti;
tok = strtok_r(NULL, "|", &saveptr); 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, void parse_single_command(const char* command,
...@@ -60,4 +65,6 @@ 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] = (char*)malloc(1);
(*argv)[0][0] = '\0'; (*argv)[0][0] = '\0';
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment