summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authornasrlol <nsrddyn@gmail.com>2025-02-18 22:56:53 +0100
committernasrlol <nsrddyn@gmail.com>2025-02-18 22:56:53 +0100
commit2bafe1c8d17533e9e45c96c243b6d2413a7ab67b (patch)
tree68ed4d9f15a259306594fdb71869ae9c186a7744 /main.c
parent3960ffc393a97a26805f7a2e9e98f3125ae0f20d (diff)
Reworked alot of code and features
Signed-off-by: nasrlol <nsrddyn@gmail.com>
Diffstat (limited to 'main.c')
-rw-r--r--main.c70
1 files changed, 35 insertions, 35 deletions
diff --git a/main.c b/main.c
index 5db0488..b0299c5 100644
--- a/main.c
+++ b/main.c
@@ -5,16 +5,45 @@
#include <unistd.h>
#define MAX_HISTORY 100
-#define MAX_COMMAND_LENGTH 255
+#define MAX_COMMAND_LENGTH 255 // not setting a macro for the argument because they are included in the command)
+#define MAX_PATH_LENGTH 1024
-struct com_struct {
+struct command {
char *com;
char *arg;
- int error; // 0: okay 1: not okay
};
-void test() {
- printf("\nprint to output");
+struct exec_command {
+ struct command *cmd;
+ void (*func);
+};
+
+
+// code prototype functions
+struct command split_command(char *input);
+
+void exec_command(char *input);
+
+// code functions
+struct command split_command(char *input) {
+ struct command NEW_COMMAND = {};
+
+ char *command = strtok(input, " ");
+ char *argument = strtok(NULL, " ");
+
+ if (command != NULL) {
+ NEW_COMMAND.com = strdup(command);
+ } else {
+ printf("failed, null pointer detected, no initial command has been inserted");
+ free(command);
+ }
+
+ if (argument != NULL) {
+ NEW_COMMAND.arg = strdup(argument);
+ } else {
+ free(argument);
+ }
+ return NEW_COMMAND;
}
void exec_command(char *input) {
@@ -51,8 +80,6 @@ struct exec_command CommandsList[] = {
// shell command functions
// ls command
-// get the requested path and put it into a const later on when getting the user input
-
void ls(const char *path) {
struct dirent *entry;
if (path == NULL) {
@@ -69,36 +96,11 @@ void ls(const char *path) {
// print the folder|directory name
while ((entry = readdir(dP)) != NULL) {
- char *temporary_variable = entry->d_name;
printf("%s\n", entry->d_name);
}
closedir(dP);
}
-struct com_struct split_command(char *input) {
- struct com_struct NEW_COMMAND = {};
-
- char *command = strtok(input, " ");
- char *argument = strtok(NULL, " ");
-
-
- if (command != NULL) {
- NEW_COMMAND.com = strdup(command);
- } else {
- printf("failed, null pointer detected, no initial command has been inserted");
- free(command);
- }
-
- if (argument != NULL) {
- NEW_COMMAND.arg = strdup(argument);
- } else {
- printf("failed, null pointer detected, no argument has been added");
- free(argument);
- }
-
- return NEW_COMMAND;
-}
-
void clear() {
// clear the visible part of the terminal -> see man pages clear command
printf("\033[2J");
@@ -122,7 +124,6 @@ char *print_cdirectory() {
void change_directory(const char *path) {
if (path == NULL) {
printf("No path found\n");
- return;
}
}
@@ -134,8 +135,7 @@ int main(void) {
char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH);
fgets(input, MAX_COMMAND_LENGTH, stdin);
- const struct com_struct new_input = split_command(input);
-
+ const struct command new_input = split_command(input);
new_input.com[strcspn(new_input.com, "\n")] = '\0';
if (new_input.arg != NULL) {