From 0a3efb2bd9ca15425f02f9dd0b310ee74d2ff811 Mon Sep 17 00:00:00 2001 From: iridiumR Date: Sun, 18 Sep 2022 23:35:26 +0800 Subject: [PATCH] feat(builtin): add the founction to read exit status --- builtin.c | 14 +++++++++++--- exec.c | 4 ++-- loop.c | 20 +++++++++++--------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/builtin.c b/builtin.c index e91217d..9531412 100644 --- a/builtin.c +++ b/builtin.c @@ -2,7 +2,7 @@ * @Author: 1ridic * @Date: 2022-09-18 14:16:19 * @Last Modified by: 1ridic - * @Last Modified time: 2022-09-18 20:46:12 + * @Last Modified time: 2022-09-18 23:32:50 */ #include "builtin.h" #include @@ -18,12 +18,13 @@ int dish_clear(char **args); int dish_setenv(char **args); int dish_echo(char **args); int dish_unset(char **args); +int dish_laststatus(char **args); -char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv", "echo", "unset"}; +char *builtin_cmd[] = {"cd", "help", "exit", "clear", "setenv", "echo", "unset","laststatus"}; int (*builtin_func[])(char **) = {&dish_cd, &dish_help, &dish_exit, &dish_clear, &dish_setenv, &dish_echo, - &dish_unset}; + &dish_unset,&dish_laststatus}; int getBuiltinNum() { return sizeof(builtin_cmd) / sizeof(char *); } @@ -78,4 +79,11 @@ int dish_unset(char **args) { i++; } return 0; +} + +int dish_laststatus(char **args) +{ + extern int status; + fprintf(stdout,"%d\n",status); + return 0; } \ No newline at end of file diff --git a/exec.c b/exec.c index 5413e8a..4f0cd04 100644 --- a/exec.c +++ b/exec.c @@ -2,7 +2,7 @@ * @Author: 1ridic * @Date: 2022-09-18 14:14:23 * @Last Modified by: 1ridic - * @Last Modified time: 2022-09-18 23:21:56 + * @Last Modified time: 2022-09-18 23:31:39 */ #include "builtin.h" #include @@ -45,7 +45,7 @@ int forkExec(char **args) { return WTERMSIG(stat_val); } } - return -1; + return -100; } int commandExec(char **args) { diff --git a/loop.c b/loop.c index f8f9cc7..dfc004a 100644 --- a/loop.c +++ b/loop.c @@ -1,24 +1,26 @@ /* - * @Author: 1ridic - * @Date: 2022-09-18 14:13:53 - * @Last Modified by: 1ridic - * @Last Modified time: 2022-09-18 14:13:53 + * @Author: 1ridic + * @Date: 2022-09-18 14:13:53 + * @Last Modified by: 1ridic + * @Last Modified time: 2022-09-18 23:32:02 */ +#include "loop.h" +#include "exec.h" +#include "line.h" #include #include #include -#include "line.h" -#include "loop.h" -#include "exec.h" + +int status; int loop() { - + char *line; char **args; printf("> "); line = readLine(); args = splitLine(line); - commandExec(args); + status = commandExec(args); free(line); free(args); return 0;