From 3010301bd7d20cd14b1eda9bd24da172c3924c89 Mon Sep 17 00:00:00 2001 From: nasrlol Date: Wed, 12 Feb 2025 23:02:07 +0100 Subject: working on the main function of the code trying to get the lines to correctly dislay everything, still having a little of trouble though --- CMakeLists.txt | 5 ++ a.out | Bin 15816 -> 0 bytes completion.c | 10 ---- current_directory.c | 37 ------------- gnu_history.c | 43 --------------- main.c | 147 +++++++++++++++++++++------------------------------- pipeling.c | 8 --- scripting.c | 0 shell | Bin 15816 -> 0 bytes variables.c | 7 --- 10 files changed, 63 insertions(+), 194 deletions(-) create mode 100644 CMakeLists.txt delete mode 100755 a.out delete mode 100644 completion.c delete mode 100644 current_directory.c delete mode 100644 gnu_history.c delete mode 100644 pipeling.c delete mode 100644 scripting.c delete mode 100755 shell delete mode 100644 variables.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6ebafe3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.10) +set(CMAKE_C23_STANDARD) + +project(unixshell) +add_executable(program main.c) \ No newline at end of file diff --git a/a.out b/a.out deleted file mode 100755 index ee42734..0000000 Binary files a/a.out and /dev/null differ diff --git a/completion.c b/completion.c deleted file mode 100644 index 2ffe5e6..0000000 --- a/completion.c +++ /dev/null @@ -1,10 +0,0 @@ -#include -#include - -int main (int argc, char *argv[]) -{ - - - return 0; -} - diff --git a/current_directory.c b/current_directory.c deleted file mode 100644 index a9eec1b..0000000 --- a/current_directory.c +++ /dev/null @@ -1,37 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: current_directory.c - * - * - * Version: 1.0 - * Created: 02/10/2025 10:26:51 AM - * Revision: none - * Compiler: gcc - * - * Author: YOUR NAME (), - * Organization: - * - * ===================================================================================== - */ - -#include -#include -#include - -int main() -{ - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) != NULL) - printf("%s", cwd); - else { - perror("getcwd() error"); - return 1; - } - return 0; -} - - - - - diff --git a/gnu_history.c b/gnu_history.c deleted file mode 100644 index 9a9b794..0000000 --- a/gnu_history.c +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ===================================================================================== - * - * Filename: gnu_history.c - * - * Description: testing GNU HISTORY AND GNU READLINE - * - * Version: 1.0 - * Created: 02/05/2025 19:31:12 - * Revision: none - * Compiler: gcc - * - * Author: YOUR NAME (), - * Organization: - * - * ===================================================================================== - */ -#include -#include -#include -#include - -int main() { - char *input; - - using_history(); // Initialize history - - while (1) { - input = readline("shell> "); // Read user input - - if (!input) break; // Handle Ctrl+D (EOF) - - if (*input) { // If input is not empty - add_history(input); // Save input to history - } - - printf("You typed: %s\n", input); - - free(input); // readline() allocates memory, so free it - } - - return 0; -} diff --git a/main.c b/main.c index 1b70b0f..901b2c4 100644 --- a/main.c +++ b/main.c @@ -15,141 +15,104 @@ // static int CURRENT_HISTORY_INDEX = 0; // static char *COMMAND_HISTORY[MAX_HISTORY] = { NULL}; -struct com_struct -{ +struct com_struct { char *com; char *arg; }; -void test() -{ +void test() { printf("print to output"); } // exit() command -void exit_() -{ - exit(0); +void exit_() { + exit(0); } // ls command // get the requested path and put it into a const later on when getting the user input -void ls(const char *path) -{ - fork(); - +void ls(const char *path) { struct dirent *entry; DIR *dP = opendir(path); - // check if the directory got opened succefully - if (dP == NULL) - { + // check if the directory got opened successfully + if (dP == NULL) { perror("opendir"); return; } // print the folder|directory name - while((entry = readdir(dP)) != NULL) - { + while ((entry = readdir(dP)) != NULL) { printf("s\n", entry->d_name); } closedir(dP); - } // pwd command -char *pwd() -{ +char *pwd() { char *current_working_directory = malloc(sizeof(char) * 100); - - if(getcwd(current_working_directory, sizeof(current_working_directory) != 0)) - { + + if (getcwd(current_working_directory, sizeof(current_working_directory) != 0)) { return current_working_directory; - } - else - { + } else { perror("getcwd() error"); return "ERROR"; } } // echo command -char *echo(char *command) -{ - char *argv = (char *)malloc(sizeof(char) * 4); - char *command_argument = (char *)malloc(sizeof(char) * 100); +char *echo(char *command) { + char *argv = (char *) malloc(sizeof(char) * 4); + char *command_argument = (char *) malloc(sizeof(char) * 100); int separation_index = 0; - for (int i = 0; i < sizeof(command); i++) - { - if (strcmp(&command[i], " ") == 0) - { + for (int i = 0; i < sizeof(command); i++) { + if (strcmp(&command[i], " ") == 0) { separation_index = i; - for ( int j = 0; j < separation_index; j++) - { + for (int j = 0; j < separation_index; j++) { argv = &command[j]; } } } - if (strcmp(argv, "echo") == 0) - { - - for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++) - { - if(strcmp(&command[i], "\0") != 0) - { - break; - } - else - { - strcpy(&command_argument[i - (separation_index - 1)], &command[i]); - } + if (strcmp(argv, "echo") == 0) { + + for (int i = separation_index + 1; i < (sizeof(command) - separation_index); i++) { + if (strcmp(&command[i], "\0") != 0) { + break; + } else { + strcpy(&command_argument[i - (separation_index - 1)], &command[i]); + } } } - return command_argument; - } -struct com_struct split_command(char *input) -{ - - - // command bestaat uit 2 delen - // het commando zelf - // en argumenten voor het commado - // we moeten het commando opsplitsen in 2 delen - // we moeten een struct gebruiken +struct com_struct split_command(char *input) { struct com_struct NEW_COMMAND; - char *command = (char *)malloc(sizeof(char) * 4); - char *argument = (char *)malloc(sizeof(char) * 100); + char *command = (char *) malloc(sizeof(char) * 4); + char *argument = (char *) malloc(sizeof(char) * 100); - if (command == NULL || argument == NULL) - { + if (command == NULL || argument == NULL) { printf("found a null pointer when dynamically assigning memory to the command or variable argument"); exit(1); } int space_position = 0; - for (int i = 0; i < sizeof(command); i++) - { - if (strcmp(&input[i], " ") == 0) - { + for (int i = 0; i < sizeof(command); i++) { + if (strcmp(&input[i], " ") == 0) { space_position = i; - for ( int j = 0; j < space_position; j++) - { + for (int j = 0; j < space_position; j++) { command = &input[j]; } - for (int x = space_position; x < sizeof(input) - space_position; x++) - { + for (int x = space_position; x < sizeof(input) - space_position; x++) { argument = &input[x]; } } - + } NEW_COMMAND.com = command; NEW_COMMAND.arg = argument; @@ -158,22 +121,28 @@ struct com_struct split_command(char *input) } -int main(int argc, char *argv[]) -{ - char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); - while (true) - { - printf("\nSHELL> "); - scanf("%s", input); - - // input converted to struct with members: command and argument - struct com_struct new_input = split_command(input); - - if (strcmp(new_input.com, "ls")) - { - // failed logic, argv is for when executing the program, not for calling functions - } - } - +int main(void) { + char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH); + + do { + if (getchar()) { + printf("\nSHELL> "); + scanf("%s", input); + + struct com_struct new_input = split_command(input); + + if (strcmp(new_input.com, "ls") == 0) { + char *path; + path = (char *) malloc(sizeof(char) * 100); + + strcpy(path, new_input.arg); + ls(path); + } else if (strcmp(new_input.com, "test") == 0){ + test(); + } + } + } + while (true); + return 0; } diff --git a/pipeling.c b/pipeling.c deleted file mode 100644 index 75adc93..0000000 --- a/pipeling.c +++ /dev/null @@ -1,8 +0,0 @@ -#include -#include - -int main(int argc, char *argv[]) -{ - - return 0; -} diff --git a/scripting.c b/scripting.c deleted file mode 100644 index e69de29..0000000 diff --git a/shell b/shell deleted file mode 100755 index be27e11..0000000 Binary files a/shell and /dev/null differ diff --git a/variables.c b/variables.c deleted file mode 100644 index ad36573..0000000 --- a/variables.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main(int argc, char *argv) -{ - printf("Trying to save variables..."); - return 0; -} -- cgit v1.2.3-70-g09d2