summaryrefslogtreecommitdiffstats
path: root/src/u_vars_next.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-04 15:52:47 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-04 15:52:47 +0200
commit5cb3373a2e5a5109a5d3b72ef45978b98f885706 (patch)
tree11a0a6f467da3ddf227d2eaf6824c4509242025e /src/u_vars_next.c
parentok nice (diff)
parent$? fix (diff)
download42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.gz
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.bz2
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.xz
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.tar.zst
42-minishell-5cb3373a2e5a5109a5d3b72ef45978b98f885706.zip
Merge branch 'master' into fix-pwd
Diffstat (limited to 'src/u_vars_next.c')
-rw-r--r--src/u_vars_next.c102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/u_vars_next.c b/src/u_vars_next.c
new file mode 100644
index 0000000..820bbe1
--- /dev/null
+++ b/src/u_vars_next.c
@@ -0,0 +1,102 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* u_vars_next.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 <inttypes.h>
+#include <stdlib.h>
+
+#include "d_enum.h"
+#include "s_lvars.h"
+#include "s_struct.h"
+
+static int64_t
+ u_get_env_var_line(const char varname[],
+ t_msh *msh)
+{
+ char **env_ptr;
+ int64_t i;
+
+ env_ptr = msh->envp;
+ i = 0;
+ while (*env_ptr &&
+ ft_strncmp(varname, *env_ptr, ft_strclen(*env_ptr, '=')) != 0)
+ {
+ env_ptr++;
+ i++;
+ }
+ if (*env_ptr)
+ return (i);
+ else
+ return (-1);
+}
+
+static t_bool
+ u_get_custom_var_existance(const char varname[],
+ t_msh *msh)
+{
+ t_lvars *ptr;
+
+ ptr = msh->vars;
+ while (ptr &&
+ ft_strncmp(varname, ptr->name, ft_strlen(varname) + 1) != 0)
+ {
+ ptr = ptr->next;
+ }
+ if (ptr != NULL)
+ {
+ return (TRUE);
+ }
+ else
+ {
+ return (FALSE);
+ }
+}
+
+/*
+** void
+** u_subst_var_value(const char varname[], const char newval[], t_msh *msh);
+**
+** DESCRIPTION
+** The subst_var_value() changes the value of msh->envp
+** variable varname[] with newval[]. If varname[] wasn't found
+** in msh->envp, varname[] is searched in msh->vars.
+*/
+
+void
+ u_subst_var_value(const char varname[],
+ const char newval[],
+ t_msh *msh)
+{
+ char new_line_fmt[2048];
+ int64_t env_line;
+
+ if ((env_line = u_get_env_var_line(varname + 1, msh)) > -1)
+ {
+ ft_memdel((void*)&msh->envp[env_line]);
+ ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval);
+ if (!(msh->envp[env_line] =
+ (char*)malloc((ft_strlen(new_line_fmt) + 1) * sizeof(char))))
+ {
+ /* TODO: Handle this fail */
+ }
+ (void)ft_strlcpy(msh->envp[env_line],
+ new_line_fmt, ft_strlen(new_line_fmt) + 1);
+ }
+ else if ((env_line = u_get_custom_var_existance(varname + 1, msh)) == TRUE)
+ {
+ lvars_rebind(&msh->vars, varname + 1, newval);
+ }
+ else
+ {
+ lvars_add_front(&msh->vars, lvars_new(varname + 1, newval));
+ }
+}