aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-14 16:46:38 +0100
committerGitHub2025-01-14 16:46:38 +0100
commitce448752a4915806bb4cd5ac67b8855362135828 (patch)
tree397ae613dd39a35a6c91e7e30426f86ade4e24bb
parent553204e584dd08987902c7693e47744192e6bd85 (diff)
parent398b0d39cbbe2cdabbfae00f799181a37754d5c1 (diff)
downloadminishell-ce448752a4915806bb4cd5ac67b8855362135828.tar.gz
minishell-ce448752a4915806bb4cd5ac67b8855362135828.zip
Merge builtins into mai
-rw-r--r--src/builtins_part_one.c152
-rw-r--r--src/builtins_part_two.c82
-rw-r--r--src/main.c1
-rw-r--r--src/new_token.c29
-rw-r--r--src/tokenizer.c2
5 files changed, 263 insertions, 3 deletions
diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c
new file mode 100644
index 0000000..6b92d9c
--- /dev/null
+++ b/src/builtins_part_one.c
@@ -0,0 +1,152 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* builtins_part_one.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */
+/* Updated: 2024/10/25 20:52:36 by chuhlig ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "env.h"
+
+int echo(char **av)
+{
+ int i;
+ int f;
+
+ i = 1;
+ f = 1;
+ if (av[1][0] == '\0')
+ {
+ write(1, "\n", 1);
+ return (1);
+ }
+ if (ft_strcmp(av[1], "-n"))
+ {
+ i++;
+ f = 0;
+ }
+ while (av[i])
+ {
+ write(1, &av[1], ft_strlen(av[i]));
+ i++;
+ }
+ if (f)
+ write(1, "\n", 1);
+ return (0);
+}
+
+int pwd(t_env **env, char *av)
+{
+ t_env *current;
+ t_env *prev;
+ char *tmp;
+
+ current = env;
+ prev = NULL;
+ while (current)
+ {
+ if (ft_strcmp(current->name, av == 0))
+ break ;
+ prev = current;
+ current = current->next;
+ }
+ ft_printf("%s\n", current->value);
+ return (0);
+}
+
+int env(t_env **env)
+{
+ t_env *current;
+ t_env *prev;
+
+ current = env;
+ prev = NULL;
+ while (current)
+ {
+ ft_printf("%s", current->name);
+ ft_printf("=%s\n", current->value);
+ prev = current;
+ current = current->next;
+ }
+ return (0);
+}
+
+int exit(char *av)
+{
+ freenode free toke free sequence stop repl free env;
+ clear history;
+}
+
+int export(char **av, t_env **env)
+{
+ char *tmp;
+ t_env *current;
+ int i;
+
+ i = i;
+ while (av[i])
+ {
+ if (t_strchr(av[i], '='))
+ {
+ tmp = ft_strchr(av[i], '=');
+ tmp = '\0';
+ current = *env;
+ while (current)
+ {
+ if (ft_strcmp(current->name, tmp[i]) == 0)
+ {
+ free(current->value);
+ current->value = ft_strdup(tmp + 1);
+ break ;
+ }
+ current = current->next;
+ }
+ if (!current)
+ {
+ current = malloc(sizeof(t_env));
+ current->name = ft_strdup(av[i]);
+ current->value = ft_strdup(tmp + 1);
+ current->next = *env;
+ *env = current;
+ }
+ }
+ i++;
+ }
+ return (1);
+}
+
+int unset(char **av, t_env **env)
+{
+ t_env *current;
+ t_env *prev;
+ int i;
+
+ i = 0;
+ current = env;
+ prev = NULL;
+ while (av[i])
+ {
+ while (current)
+ {
+ if (ft_strcmp(current->name, av[i] == 0))
+ break ;
+ prev = current;
+ current = current->next;
+ }
+ if (current)
+ {
+ if (prev)
+ prev->next = current->next;
+ else
+ *env = current->next;
+ free(current->value);
+ free(current);
+ }
+ i++;
+ }
+ return (0);
+} \ No newline at end of file
diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c
new file mode 100644
index 0000000..94ef258
--- /dev/null
+++ b/src/builtins_part_two.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* builtins_part_two.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */
+/* Updated: 2024/10/25 20:52:46 by chuhlig ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "env.h"
+
+void update_oldpwd(t_env **env)
+{
+ t_env *current;
+ t_env *prev;
+ char cwd[1028];
+ char *tmp;
+
+ while (current)
+ {
+ if (ft_strncmp(current->name, "OLDPWD", 6) == 0)
+ break ;
+ prev = current;
+ current = current->next;
+ }
+ getcwd(cwd, sizeof(cwd));
+ tmp = ft_strdup(cwd);
+ free(current->value);
+ current->value = tmp;
+}
+
+void update_pwd(t_env **env)
+{
+ t_env *current;
+ t_env *prev;
+ char cwd[1028];
+ char *tmp;
+
+ while (current)
+ {
+ if (ft_strncmp(current->name, "PWD", 3) == 0)
+ break ;
+ prev = current;
+ current = current->next;
+ }
+ getcwd(cwd, sizeof(cwd));
+ tmp = ft_strdup(cwd);
+ free(current->value);
+ current->value = tmp;
+}
+
+int cd(t_env **env, char **av)
+{
+ t_env *current;
+ t_env *prev;
+ t_env *pwd;
+
+ current = env;
+ if (av[1] == NULL)
+ {
+ update_oldpwd(&env);
+ while (current)
+ {
+ if (ft_strncmp(current->name, "HOME", 4) == 0)
+ break ;
+ prev = current;
+ current = current->next;
+ }
+ if (chdir(current->value) == -1)
+ return ;
+ }
+ else
+ {
+ update_oldpwd(&env);
+ if (chdir(av[1]) == -1)
+ return ;
+ update_pwd(&env);
+ }
+} \ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 18530bb..4e1d9da 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,6 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */
+/* Updated: 2024/08/26 19:55:57 by chuhlig ### ########.fr */
/* Updated: 2024/10/25 16:06:32 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
diff --git a/src/new_token.c b/src/new_token.c
index 92ff421..6431c7d 100644
--- a/src/new_token.c
+++ b/src/new_token.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* new_token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 14:29:44 by dkaiser #+# #+# */
-/* Updated: 2024/06/28 14:59:34 by dkaiser ### ########.fr */
+/* Updated: 2024/08/29 15:30:52 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -50,3 +50,28 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next)
token->content.redir_type = type;
return (token);
}
+
+// void ft_append_token(int type, t_token **list)// but we need somewhere token/node head initialize with nul
+// {
+// t_token *node;
+// t_token *last_node;
+
+// if (!list)
+// return ;
+// node = malloc(sizeof(t_token));
+// if (!node)
+// return ;
+// node->next = NULL;
+// node->type = type;
+// if (!*list)
+// {
+// *list = node;
+// node->previous = NULL;
+// }
+// else
+// {
+// last_node = ft_lstlast(*list);
+// last_node->next = node;
+// node->previous = last_node;
+// }
+// } \ No newline at end of file
diff --git a/src/tokenizer.c b/src/tokenizer.c
index ab8a6ac..d7a96f7 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */
-/* Updated: 2024/08/29 15:26:55 by dkaiser ### ########.fr */
+/* Updated: 2024/10/17 14:21:26 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */