summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2025-02-05 22:37:27 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2025-02-05 22:37:27 +0100
commita03bda11909b066095f734c01988d2c0d75c261f (patch)
treebe1cdde04e208eb2ffc2255ea6be29935ac40d15 /main.c
parentdb36212d04156f775e7065c59ed69cd65a54b075 (diff)
switching away from using readline after realizing it was making things a little to easy, wouldnt give me the fullfilling feeling im looking for...
Diffstat (limited to 'main.c')
-rw-r--r--main.c63
1 files changed, 49 insertions, 14 deletions
diff --git a/main.c b/main.c
index da1bd6c..f25e2d3 100644
--- a/main.c
+++ b/main.c
@@ -1,11 +1,8 @@
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
-#include <readline/readline.h>
#include <dirent.h>
#include <unistd.h>
-#include <readline/readline.h>
-#include <readline/history.h>
/* BOOLEANS */
@@ -13,6 +10,21 @@
#define false 0
#define _DEFAULT_SOURCE
+#define MAX_HISTORY 10
+#define MAX_COMMAND_LENGTH 256
+
+
+static int CURRENT_COMMAND_INDEX = 0;
+static int CURRENT_COMMAND_HISTORY_INDEX = 0;
+static char COMMAND_HISTORY[MAX_HISTORY][MAX_COMMAND_LENGTH];
+
+
+
+static char *line_read = (char*)NULL;
+
+
+// DEFAULT SHELL COMMANDS
+// exit() command
void exit_program(char *argv)
{
if (strcmp(argv, "exit"))
@@ -21,11 +33,7 @@ void exit_program(char *argv)
}
}
-void get_pid_parent_process()
-{
- printf("PID %ld\n", (long)getpid());
-}
-
+// pwd command
void list_directories()
{
struct dirent **namelist;
@@ -47,8 +55,7 @@ void list_directories()
exit(EXIT_SUCCESS);
}
-
-
+// echo emulation command, without variables
void echo(char *command, char *argv)
{
if (strcmp(command, "echo") == 0)
@@ -57,12 +64,40 @@ void echo(char *command, char *argv)
}
}
-int main(void)
+char *save_history(char *user_input)
{
+
+ while (CURRENT_COMMAND_HISTORY_INDEX < MAX_HISTORY)
+ {
+ while (CURRENT_COMMAND_INDEX < MAX_COMMAND_LENGTH)
+ {
+ COMMAND_HISTORY[CURRENT_COMMAND_HISTORY_INDEX][CURRENT_COMMAND_INDEX] = *user_input;
+ CURRENT_COMMAND_HISTORY_INDEX++;
+ }
+ }
+ return *COMMAND_HISTORY;
+}
+
+// GET USER INPUT
+char * get_line()
+{
+ if (line_read)
+ {
+ free(line_read);
+ line_read = (char *)NULL;
+ }
+ scanf("%s", &(*line_read));
- char *input;
- readline(input);
-
+ if (line_read && *line_read)
+ save_history(line_read);
+
+ return line_read;
+}
+
+
+int main(void)
+{
+ char *input;
while(strcmp(input, "exit") != 0)
{
list_directories();