summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornasrlol <nsrddyn@gmail.com>2025-02-12 23:02:07 +0100
committernasrlol <nsrddyn@gmail.com>2025-02-12 23:02:07 +0100
commit3010301bd7d20cd14b1eda9bd24da172c3924c89 (patch)
tree7fb33cd85ece474c2b6cc0bf8276bb8bbe3d1783
parent87fb4d9ea774bc7fd69076947925fac01e68f60b (diff)
working on the main function of the code trying to get the lines to correctly dislay everything, still having a little of trouble though
-rw-r--r--CMakeLists.txt5
-rwxr-xr-xa.outbin15816 -> 0 bytes
-rw-r--r--completion.c10
-rw-r--r--current_directory.c37
-rw-r--r--gnu_history.c43
-rw-r--r--main.c143
-rw-r--r--pipeling.c8
-rw-r--r--scripting.c0
-rwxr-xr-xshellbin15816 -> 0 bytes
-rw-r--r--variables.c7
10 files changed, 61 insertions, 192 deletions
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
--- a/a.out
+++ /dev/null
Binary files 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 <stdlib.h>
-#include <unistd.h>
-
-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 <unistd.h>
-#include <stdio.h>
-#include <linux/limits.h>
-
-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 <stdio.h>
-#include <readline/readline.h>
-#include <readline/history.h>
-#include <stdlib.h>
-
-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);
+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);
- // input converted to struct with members: command and argument
- 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);
- if (strcmp(new_input.com, "ls"))
- {
- // failed logic, argv is for when executing the program, not for calling functions
- }
- }
-
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 <stdlib.h>
-#include <unistd.h>
-
-int main(int argc, char *argv[])
-{
-
- return 0;
-}
diff --git a/scripting.c b/scripting.c
deleted file mode 100644
index e69de29..0000000
--- a/scripting.c
+++ /dev/null
diff --git a/shell b/shell
deleted file mode 100755
index be27e11..0000000
--- a/shell
+++ /dev/null
Binary files 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 <stdlib.h>
-
-int main(int argc, char *argv)
-{
- printf("Trying to save variables...");
- return 0;
-}