From 305c14202b26212e8004552f1d77329f11a40733 Mon Sep 17 00:00:00 2001 From: "B.Delay" Date: Wed, 27 Nov 2024 23:11:35 +0100 Subject: [PATCH 1/4] feat: add redirect in shell --- usr/src/sh.c | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/usr/src/sh.c b/usr/src/sh.c index 4d4dccf459..da7722b633 100644 --- a/usr/src/sh.c +++ b/usr/src/sh.c @@ -10,7 +10,7 @@ * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * GNU General Public License for more details.printf("Ciao\n"); * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software @@ -28,12 +28,14 @@ #include #include #include +#include #define TOKEN_NR 10 #define ARGS_MAX 16 char tokens[TOKEN_NR][80]; char prompt[] = "so3% "; +char file_buff[500]; void parse_token(char *str) { int i = 0; @@ -53,10 +55,10 @@ void parse_token(char *str) { * Process the command with the different tokens */ void process_cmd(void) { - int i, pid_child, background, arg_pos, arg_pos2; + int i, pid_child, background, arg_pos, arg_pos2, redirection, byte_readen; char *argv[ARGS_MAX], *argv2[ARGS_MAX]; char filename[30]; - int pid, sig, pid_child2; + int pid, sig, pid_child2,fd; int pipe_on = 0; int pipe_fd[2]; @@ -133,6 +135,7 @@ void process_cmd(void) { arg_pos = 0; arg_pos2 = 0; background = 0; + redirection = 0; while (!background && tokens[arg_pos][0] != 0) { /* Check for & */ if (!strcmp(tokens[arg_pos], "&")) @@ -141,11 +144,17 @@ void process_cmd(void) { if (!strcmp(tokens[arg_pos], "|")) { pipe_on = 1; argv[arg_pos] = NULL; - } else { + }else if(!strcmp(tokens[arg_pos], ">")){ + redirection = 1; + argv[arg_pos] = NULL; + }else { if (pipe_on) { argv2[arg_pos2] = tokens[arg_pos]; arg_pos2++; - } else + }else if(redirection) + { + argv2[0] = tokens[arg_pos]; + }else argv[arg_pos] = tokens[arg_pos]; } arg_pos++; @@ -194,7 +203,35 @@ void process_cmd(void) { } } - } else { + }else if(redirection){ + fd = open(argv2[0], O_WRONLY | O_CREAT); + if(fd < 0){ + printf("Error opening/creating output file...\n"); + return; + } + + pipe(pipe_fd); + pid_child2 = fork(); + if(!pid_child2){ + close(pipe_fd[0]); + dup2(pipe_fd[1], STDOUT_FILENO); + strcpy(filename, argv[0]); + strcat(filename, ".elf"); + + if (execv(filename, argv) == -1) { + printf("%s: exec failed.\n", argv[0]); + exit(-1); + } + + }else{ + close(pipe_fd[1]); + while((byte_readen = read(pipe_fd[0], file_buff, 500)) > 0){ + write(fd, file_buff, byte_readen); + } + close(fd); + } + + }else { strcpy(filename, tokens[0]); argv[0] = tokens[0]; @@ -258,7 +295,6 @@ void main(int argc, char *argv[]) /* Check if there is at least one token to be processed */ if (tokens[0][0] != 0) process_cmd(); - - + } } From c9919077ad449494486b437f169b0197cc536a9f Mon Sep 17 00:00:00 2001 From: "B.Delay" Date: Thu, 28 Nov 2024 13:21:06 +0100 Subject: [PATCH 2/4] fmt: format code according to coding convention --- usr/src/sh.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/usr/src/sh.c b/usr/src/sh.c index da7722b633..43d4fcc339 100644 --- a/usr/src/sh.c +++ b/usr/src/sh.c @@ -10,7 +10,7 @@ * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details.printf("Ciao\n"); + * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software @@ -144,7 +144,7 @@ void process_cmd(void) { if (!strcmp(tokens[arg_pos], "|")) { pipe_on = 1; argv[arg_pos] = NULL; - }else if(!strcmp(tokens[arg_pos], ">")){ + }else if (!strcmp(tokens[arg_pos], ">")){ redirection = 1; argv[arg_pos] = NULL; }else { @@ -203,16 +203,16 @@ void process_cmd(void) { } } - }else if(redirection){ + }else if (redirection){ fd = open(argv2[0], O_WRONLY | O_CREAT); - if(fd < 0){ + if (fd < 0){ printf("Error opening/creating output file...\n"); return; } pipe(pipe_fd); pid_child2 = fork(); - if(!pid_child2){ + if (!pid_child2){ close(pipe_fd[0]); dup2(pipe_fd[1], STDOUT_FILENO); strcpy(filename, argv[0]); @@ -223,9 +223,9 @@ void process_cmd(void) { exit(-1); } - }else{ + }else { close(pipe_fd[1]); - while((byte_readen = read(pipe_fd[0], file_buff, 500)) > 0){ + while ((byte_readen = read(pipe_fd[0], file_buff, 500)) > 0){ write(fd, file_buff, byte_readen); } close(fd); From 8f375521570f5653a560c0c3e898e4600896b654 Mon Sep 17 00:00:00 2001 From: "B.Delay" Date: Fri, 29 Nov 2024 13:26:58 +0100 Subject: [PATCH 3/4] fmt: format code according to coding convention --- usr/src/sh.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/usr/src/sh.c b/usr/src/sh.c index 43d4fcc339..558074af4c 100644 --- a/usr/src/sh.c +++ b/usr/src/sh.c @@ -144,17 +144,16 @@ void process_cmd(void) { if (!strcmp(tokens[arg_pos], "|")) { pipe_on = 1; argv[arg_pos] = NULL; - }else if (!strcmp(tokens[arg_pos], ">")){ + } else if (!strcmp(tokens[arg_pos], ">")) { redirection = 1; argv[arg_pos] = NULL; - }else { + } else { if (pipe_on) { argv2[arg_pos2] = tokens[arg_pos]; arg_pos2++; - }else if(redirection) - { + } else if (redirection) { argv2[0] = tokens[arg_pos]; - }else + } else argv[arg_pos] = tokens[arg_pos]; } arg_pos++; @@ -188,8 +187,7 @@ void process_cmd(void) { exit(-1); } - } - else { + } else { close(pipe_fd[1]); dup2(pipe_fd[0], STDIN_FILENO); @@ -203,16 +201,16 @@ void process_cmd(void) { } } - }else if (redirection){ + } else if (redirection) { fd = open(argv2[0], O_WRONLY | O_CREAT); - if (fd < 0){ + if (fd < 0) { printf("Error opening/creating output file...\n"); return; } pipe(pipe_fd); pid_child2 = fork(); - if (!pid_child2){ + if (!pid_child2) { close(pipe_fd[0]); dup2(pipe_fd[1], STDOUT_FILENO); strcpy(filename, argv[0]); @@ -223,15 +221,15 @@ void process_cmd(void) { exit(-1); } - }else { + } else { close(pipe_fd[1]); - while ((byte_readen = read(pipe_fd[0], file_buff, 500)) > 0){ + while ((byte_readen = read(pipe_fd[0], file_buff, 500)) > 0) { write(fd, file_buff, byte_readen); } close(fd); } - }else { + } else { strcpy(filename, tokens[0]); argv[0] = tokens[0]; From e4a5bfa948ca66453bace0401ee3148e237cb70e Mon Sep 17 00:00:00 2001 From: "B.Delay" Date: Tue, 3 Dec 2024 10:33:21 +0100 Subject: [PATCH 4/4] fmt: format code according to coding convention --- usr/src/sh.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/usr/src/sh.c b/usr/src/sh.c index 558074af4c..41b77314a6 100644 --- a/usr/src/sh.c +++ b/usr/src/sh.c @@ -55,10 +55,10 @@ void parse_token(char *str) { * Process the command with the different tokens */ void process_cmd(void) { - int i, pid_child, background, arg_pos, arg_pos2, redirection, byte_readen; + int i, pid_child, background, arg_pos, arg_pos2, redirection, byte_read; char *argv[ARGS_MAX], *argv2[ARGS_MAX]; char filename[30]; - int pid, sig, pid_child2,fd; + int pid, sig, pid_child2, fd; int pipe_on = 0; int pipe_fd[2];