From 7bbfb7556662ff8e415f7a3de529ea5bb102fe7f Mon Sep 17 00:00:00 2001 From: Aleksandr Lebedev Date: Thu, 19 Feb 2026 11:53:46 +0100 Subject: [PATCH 1/3] basic shell --- main.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..4df6d87 --- /dev/null +++ b/main.c @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static sigjmp_buf restart_jmp_buf; +static const int restart_value = 69; +static volatile sig_atomic_t jump_active = 0; + +void sigint_handler(int signo) +{ + if(!jump_active) + { + return; + } + siglongjmp(restart_jmp_buf, restart_value); +} + +char** split_input(char* input) +{ + const size_t alloc_size = ((strlen(input) + 1) / 2) + 1; + char** command = malloc(alloc_size * sizeof(char*)); + if (command == NULL) + { + perror("malloc failed"); + exit(1); + } + const char* separator = " "; + char* parsed; + uint32_t index = 0; + parsed = strtok(input, separator); + while (parsed != NULL) + { + command[index] = parsed; + index++; + parsed = strtok(NULL, separator); + } + command[index] = NULL; + return command; +} + +int cd(char* path) +{ + return chdir(path); +} + +int main() +{ + struct sigaction s; + s.sa_handler = sigint_handler; + sigemptyset(&s.sa_mask); + s.sa_flags = SA_RESTART; + struct sigaction s_old; + sigaction(SIGINT, &s, &s_old); + + int stat_loc; + + if(sigsetjmp(restart_jmp_buf, 1) == restart_value) + { + printf("\n"); + } + jump_active = 1; + + while(true) + { + char* input = readline("arsh> "); + if(input == NULL) //CTRL + D + { + continue; + } + char** command = split_input(input); + + if(strcmp(command[0], "cd") == 0) + { + if (cd(command[1]) < 0) + { + perror(command[1]); + } + + continue; //skip fork() + } + if(strcmp(command[0], "exit") == 0) + { + int32_t status_code = command[1] != NULL ? atoi(command[1]) : 0; + exit(status_code); + } + + pid_t child_pid = fork(); + if (child_pid < 0) + { + perror("Fork failed"); + exit(1); + } + if (child_pid == 0) + { + sigaction(SIGINT, &s_old, NULL); + //never returns if call is successful + if(execvp(command[0], command) < 0) + { + perror(command[0]); + exit(1); + } + } + else + { + waitpid(child_pid, &stat_loc, WUNTRACED); + } + + free(input); + free(command); + } +} From 2825aa5d883267da6c0da28371ae81739147021f Mon Sep 17 00:00:00 2001 From: Aleksandr Lebedev Date: Thu, 19 Feb 2026 14:20:43 +0100 Subject: [PATCH 2/3] Added README --- README.org | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 README.org diff --git a/README.org b/README.org new file mode 100644 index 0000000..8dbfbb8 --- /dev/null +++ b/README.org @@ -0,0 +1,12 @@ +#+title: arsh - ARchaic SHell +#+options: toc:nil +Simple shell for Unix-like systems written in C, that has a funny name (for germans). +* Features +- Can run one command at a given time (no | , &&, ||, functions, etc) +- ~cd~ builtin command +- ~exit~ builtin command +* Build +To build it, you need ~readline~ library. Then use +#+begin_src shell +gcc main.c -O3 -lreadline -o arsh +#+end_src From 60b9863f087d073e2b35396cc2d46ba9f25d4aec Mon Sep 17 00:00:00 2001 From: Aleksandr Lebedev Date: Thu, 19 Feb 2026 14:22:37 +0100 Subject: [PATCH 3/3] Deleted README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index d417a82..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# arsh - -Simple shell for Unix-like systems written in C \ No newline at end of file