summaryrefslogtreecommitdiffstats
path: root/src/s_lvars.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-07-27 18:55:07 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-07-27 18:55:07 +0200
commit487a66394061f2d14a2fa421302966b5442d643f (patch)
tree34237d4e9cb85f94bcefd46f399d2224dff3f6b3 /src/s_lvars.c
parentCorrect shell prompt (diff)
download42-minishell-487a66394061f2d14a2fa421302966b5442d643f.tar.gz
42-minishell-487a66394061f2d14a2fa421302966b5442d643f.tar.bz2
42-minishell-487a66394061f2d14a2fa421302966b5442d643f.tar.xz
42-minishell-487a66394061f2d14a2fa421302966b5442d643f.tar.zst
42-minishell-487a66394061f2d14a2fa421302966b5442d643f.zip
I couldn't see shit
Diffstat (limited to 'src/s_lvars.c')
-rw-r--r--src/s_lvars.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/src/s_lvars.c b/src/s_lvars.c
new file mode 100644
index 0000000..f2a1151
--- /dev/null
+++ b/src/s_lvars.c
@@ -0,0 +1,126 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* s_lvars.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "s_struct.h"
+
+void
+ lvars_rebind(t_lvars **lvars,
+ const char name[],
+ const char newval[])
+{
+ t_lvars *tmp;
+
+ tmp = *lvars;
+ while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name)))
+ {
+ tmp = tmp->next;
+ }
+ if (tmp == NULL)
+ {
+ return ;
+ }
+ ft_memdel((void*)&tmp->val);
+ if (!(tmp->val = ft_strdup(newval)))
+ {
+ ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno));
+ exit(FT_RET_ALLOC);
+ }
+}
+
+void
+ lvars_delone(t_lvars **lvars,
+ const char name[])
+{
+ t_lvars *tmp;
+ t_lvars *prev;
+
+ tmp = *lvars;
+ if (tmp != NULL && !ft_strncmp(tmp->name, name, ft_strlen(name)))
+ {
+ *lvars = tmp->next;
+ ft_memdel((void*)&tmp->name);
+ ft_memdel((void*)&tmp->val);
+ ft_memdel((void*)&tmp);
+ return ;
+ }
+ while (tmp && ft_strncmp(tmp->name, name, ft_strlen(name)))
+ {
+ prev = tmp;
+ tmp = tmp->next;
+ }
+ if (tmp == NULL)
+ return ;
+ prev->next = tmp->next;
+ ft_memdel((void*)&tmp->name);
+ ft_memdel((void*)&tmp->val);
+ ft_memdel((void*)&tmp);
+}
+
+void
+ lvars_add_front(t_lvars **alvars,
+ t_lvars *new)
+{
+ if (!alvars || !new)
+ {
+ return ;
+ }
+ new->next = *alvars;
+ *alvars = new;
+}
+
+void
+ lvars_clear(t_lvars **lvars)
+{
+ t_lvars *tmp;
+ t_lvars *renext;
+
+ if (!lvars)
+ return ;
+ tmp = *lvars;
+ while (tmp)
+ {
+ renext = tmp->next;
+ ft_memdel((void*)&tmp->name);
+ ft_memdel((void*)&tmp->val);
+ ft_memdel((void*)&tmp);
+ tmp = renext;
+ }
+ *lvars = NULL;
+}
+
+t_lvars
+ *lvars_new(const char name[],
+ const char val[])
+{
+ t_lvars *link;
+
+ if (!(link = (t_lvars*)malloc(sizeof(t_lvars))))
+ {
+ return (NULL);
+ }
+ if (!(link->name = ft_strdup(name)))
+ {
+ return (NULL);
+ }
+ if (!(link->val = ft_strdup(val)))
+ {
+ return (NULL);
+ }
+ link->next = NULL;
+ return (link);
+}