summaryrefslogtreecommitdiffstats
path: root/src/u_vars_next.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-07-27 22:38:54 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-07-27 22:38:54 +0200
commit280ecd341f1ec88abe325fa69b197bca3e43d2f1 (patch)
tree3f10bdb634f5a727bbf2e410072bc8f6537310e4 /src/u_vars_next.c
parentIn progress (diff)
download42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.gz
42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.bz2
42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.xz
42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.tar.zst
42-minishell-280ecd341f1ec88abe325fa69b197bca3e43d2f1.zip
Now can subst env vars
Diffstat (limited to 'src/u_vars_next.c')
-rw-r--r--src/u_vars_next.c55
1 files changed, 49 insertions, 6 deletions
diff --git a/src/u_vars_next.c b/src/u_vars_next.c
index 5626376..8134de3 100644
--- a/src/u_vars_next.c
+++ b/src/u_vars_next.c
@@ -10,21 +10,64 @@
/* */
/* ************************************************************************** */
+#include <libft.h>
+#include <inttypes.h>
+#include <stdlib.h>
+
#include "s_struct.h"
+
+static int64_t
+ u_get_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(*env_ptr, varname, ft_strlen(varname)) != 0)
+ {
+ env_ptr++;
+ i++;
+ }
+ if (*env_ptr)
+ return (i);
+ else
+ return (-1);
+}
+
/*
** void
-** subst_var_value(const char varname[], t_msh *msh);
+** u_subst_var_value(const char varname[], const char newval[], t_msh *msh);
**
** DESCRIPTION
-** The subst_var_value() changes the msh->envp value
-** of the variable varname[]. If varname[] wasn't found
+** 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
- subst_var_value(const char varname[],
+ u_subst_var_value(const char varname[],
+ const char newval[],
t_msh *msh)
{
- (void)varname;
- (void)msh;
+ char new_line_fmt[2048];
+ int64_t env_line;
+
+ if ((env_line = u_get_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
+ {
+ /* TODO: Search in custom vars */
+ }
}