summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/b_export_next.c16
-rw-r--r--src/b_unset.c9
-rw-r--r--src/e_externs_next.c19
-rw-r--r--src/f_fail.h1
-rw-r--r--src/f_shlvl.c22
-rw-r--r--src/f_shlvl.h20
-rw-r--r--src/s_init.c30
-rw-r--r--src/u_vars.c51
-rw-r--r--src/u_vars_next.c20
9 files changed, 120 insertions, 68 deletions
diff --git a/src/b_export_next.c b/src/b_export_next.c
index 0a56968..7b70047 100644
--- a/src/b_export_next.c
+++ b/src/b_export_next.c
@@ -17,6 +17,7 @@
#include "f_fail.h"
#include "s_lvars.h"
#include "s_struct.h"
+#include "u_utils.h"
#include "u_vars.h"
static char
@@ -46,17 +47,20 @@ static int64_t
b_is_it_in_env(const char varname[],
t_msh *msh)
{
- char **p_env;
+ char **env_dup;
+ size_t i;
- p_env = msh->envp;
- while (*p_env != NULL)
+ env_dup = u_get_env_var_names(msh);
+ i = 0;
+ while (env_dup[i] != NULL)
{
- if (ft_strncmp(varname, *p_env, ft_strclen(*p_env, '=')) == 0)
+ if (ft_strncmp(varname, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
{
- return (p_env - msh->envp);
+ return (i);
}
- p_env++;
+ i++;
}
+ ft_delwords(env_dup);
return (-1);
}
diff --git a/src/b_unset.c b/src/b_unset.c
index 03fd2a8..c45d5ee 100644
--- a/src/b_unset.c
+++ b/src/b_unset.c
@@ -17,6 +17,7 @@
#include "f_fail.h"
#include "s_lvars.h"
#include "s_struct.h"
+#include "u_utils.h"
static t_bool
check_valid_identifier(const char arg[])
@@ -82,18 +83,22 @@ static t_bool
b_removed_from_env(const char arg[],
t_msh *msh)
{
+ char **env_dup;
size_t i;
+ env_dup = u_get_env_var_names(msh);
i = 0;
- while (msh->envp[i] != NULL)
+ while (env_dup[i] != NULL)
{
- if (ft_strncmp(arg, msh->envp[i], ft_strclen(msh->envp[i], '=')) == 0)
+ if (ft_strncmp(arg, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
{
b_realloc_env(i, msh);
+ ft_delwords(env_dup);
return (TRUE);
}
i++;
}
+ ft_delwords(env_dup);
return (FALSE);
}
diff --git a/src/e_externs_next.c b/src/e_externs_next.c
index e33405c..bbd9d81 100644
--- a/src/e_externs_next.c
+++ b/src/e_externs_next.c
@@ -19,6 +19,7 @@
#include "s_destroy.h"
#include "s_lcom.h"
#include "s_struct.h"
+#include "u_utils.h"
static char
*get_fullpath(const char p_path[],
@@ -47,7 +48,6 @@ char
char *envpath[],
t_msh *msh)
{
- /* TODO: norme */
struct dirent *ent;
char **p_path;
char *fullpath;
@@ -78,18 +78,19 @@ char
char
**get_env_path(t_msh *msh)
{
- char **p_env;
+ size_t i;
+ char **env_dup;
char **envpath;
char *envline;
- p_env = msh->envp;
- while (*p_env && ft_strncmp("PATH", *p_env, 4) != 0)
- {
- p_env++;
- }
- if (*p_env == NULL)
+ env_dup = u_get_env_var_names(msh);
+ i = 0;
+ while (env_dup[i] && ft_strncmp("PATH", env_dup[i], 5) != 0)
+ i++;
+ if (env_dup[i] == NULL)
return (NULL);
- envline = ft_strchr(*p_env, '=');
+ ft_delwords(env_dup);
+ envline = ft_strchr(msh->envp[i], '=');
envline += 1;
if (*envline != '\0')
{
diff --git a/src/f_fail.h b/src/f_fail.h
index 0d3c19b..b1b3b8c 100644
--- a/src/f_fail.h
+++ b/src/f_fail.h
@@ -18,6 +18,7 @@
#include "f_chdir.h"
#include "f_errno.h"
#include "f_redir.h"
+#include "f_shlvl.h"
#include "s_struct.h"
void f_fail_no_options(const char concern[], t_msh *msh);
diff --git a/src/f_shlvl.c b/src/f_shlvl.c
new file mode 100644
index 0000000..0bd432a
--- /dev/null
+++ b/src/f_shlvl.c
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* f_shlvl.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 <stdint.h>
+
+void
+ f_shlvl_too_high(int32_t shlvl)
+{
+ ft_dprintf(2,
+ "minishell: warning: shell level (%d) too high, resetting to 1\n",
+ shlvl + 1);
+}
diff --git a/src/f_shlvl.h b/src/f_shlvl.h
new file mode 100644
index 0000000..1b106f2
--- /dev/null
+++ b/src/f_shlvl.h
@@ -0,0 +1,20 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* f_shlvl.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* 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 */
+/* */
+/* ************************************************************************** */
+
+#ifndef F_SHLVL_H
+#define F_SHLVL_H
+
+#include <stdint.h>
+
+void f_shlvl_too_high(int32_t shlvl);
+
+#endif
diff --git a/src/s_init.c b/src/s_init.c
index 1dbbbb7..ab64603 100644
--- a/src/s_init.c
+++ b/src/s_init.c
@@ -20,6 +20,7 @@
#include "b_export_next.h"
#include "d_define.h"
+#include "f_fail.h"
#include "m_funptr.h"
#include "s_init.h"
#include "u_vars.h"
@@ -67,14 +68,31 @@ static char
static void
inc_shlvl(t_msh *msh)
{
+ int32_t shlvl;
char *str_one;
char *str_two;
+ char *tmp;
+ char fmt[8];
- u_subst_var_value("$SHLVL",
- str_one = ft_itoa(
- ft_atoi(str_two = u_get_var_value("$SHLVL", msh)) + 1), msh);
- ft_memdel((void*)&str_one);
- ft_memdel((void*)&str_two);
+ if ((tmp = u_get_var_value("$SHLVL", msh)) == NULL)
+ {
+ ft_memcpy(fmt, "SHLVL=1", 8);
+ b_export_with_equals(fmt, msh);
+ }
+ else
+ {
+ ft_memdel((void*)&tmp);
+ str_two = u_get_var_value("$SHLVL", msh);
+ shlvl = ft_atoi(str_two);
+ if (shlvl >= 999)
+ f_shlvl_too_high(shlvl);
+ shlvl = (shlvl >= 999) ? 0 : shlvl;
+ shlvl = (shlvl < 0) ? 0 : shlvl + 1;
+ str_one = ft_itoa(shlvl);
+ u_subst_var_value("$SHLVL", str_one, msh);
+ ft_memdel((void*)&str_one);
+ ft_memdel((void*)&str_two);
+ }
}
static char
@@ -91,7 +109,7 @@ static char
b_export_with_equals(fmt, msh);
return (cwd);
}
- if ((dir = opendir(cwd)) != 0)
+ if ((dir = opendir(cwd)) != NULL)
closedir(dir);
else if (errno == ENOENT)
{
diff --git a/src/u_vars.c b/src/u_vars.c
index 2a78f3d..e949cc9 100644
--- a/src/u_vars.c
+++ b/src/u_vars.c
@@ -19,68 +19,50 @@
#include "s_destroy.h"
#include "s_lcom.h"
#include "s_struct.h"
-
-static char
- *u_set_rva(const char varname[],
- t_msh *msh)
-{
- char *rvarname;
-
- if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char))))
- {
- lcom_clear(&msh->curr);
- s_destroy(msh);
- f_fail_alloc(msh);
- }
- (void)ft_memcpy((char*)rvarname, (const char*)varname + 1,
- ft_strlen(varname + 1));
- *(rvarname + ft_strlen(varname + 1)) = '=';
- *(rvarname + ft_strlen(varname + 1) + 1) = '\0';
- return (rvarname);
-}
+#include "u_utils.h"
static char
*u_dup_env(char *p_env,
- char *rvarname,
t_msh *msh)
{
char *varval;
if (!(varval = ft_strdup(p_env)))
{
- ft_memdel((void*)&rvarname);
lcom_clear(&msh->curr);
s_destroy(msh);
f_fail_alloc(msh);
}
- ft_memdel((void*)&rvarname);
return (varval);
}
static char
- *u_get_frm_env(char rvarname[],
- t_msh *msh)
+ *u_get_frm_env(const char varname[],
+ t_msh *msh)
{
- char **p_env;
+ char **env_dup;
char *pp_env;
char *varval;
+ size_t i;
- p_env = msh->envp;
- while (*p_env)
+ env_dup = u_get_env_var_names(msh);
+ i = 0;
+ while (env_dup[i] != NULL)
{
- if (!ft_strncmp(rvarname, *p_env, ft_strclen(*p_env, '=')))
+ if (ft_strncmp(varname + 1, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
{
- pp_env = *p_env;
+ pp_env = msh->envp[i];
while (*pp_env != '\0' && *pp_env != '=')
pp_env += 1;
if (*pp_env == '=')
pp_env += 1;
- varval = u_dup_env(pp_env, rvarname, msh);
+ varval = u_dup_env(pp_env, msh);
+ ft_delwords(env_dup);
return (varval);
}
- p_env += 1;
+ i++;
}
- ft_memdel((void*)&rvarname);
+ ft_delwords(env_dup);
return (NULL);
}
@@ -160,9 +142,7 @@ char
t_msh *msh)
{
/* TODO: check behaviour on empty vars -> "QWE=" */
- /* TODO: add support for global variables -> "$hey $nigga..." */
char *varval;
- char *rvarname;
varval = NULL;
if ((varval = u_get_special_var(varname, msh)) != NULL)
@@ -175,8 +155,7 @@ char
}
else
{
- rvarname = u_set_rva(varname, msh);
- varval = u_get_frm_env(rvarname, msh);
+ varval = u_get_frm_env(varname, msh);
return (varval);
}
return (NULL);
diff --git a/src/u_vars_next.c b/src/u_vars_next.c
index 820bbe1..c6c351b 100644
--- a/src/u_vars_next.c
+++ b/src/u_vars_next.c
@@ -17,26 +17,28 @@
#include "d_enum.h"
#include "s_lvars.h"
#include "s_struct.h"
+#include "u_utils.h"
static int64_t
u_get_env_var_line(const char varname[],
t_msh *msh)
{
- char **env_ptr;
+ char **env_dup;
int64_t i;
- env_ptr = msh->envp;
+ env_dup = u_get_env_var_names(msh);
i = 0;
- while (*env_ptr &&
- ft_strncmp(varname, *env_ptr, ft_strclen(*env_ptr, '=')) != 0)
+ while (env_dup[i] != NULL &&
+ ft_strncmp(varname, env_dup[i], ft_strlen(env_dup[i]) + 1) != 0)
{
- env_ptr++;
i++;
}
- if (*env_ptr)
- return (i);
- else
- return (-1);
+ if (env_dup[i] == NULL)
+ {
+ i = -1;
+ }
+ ft_delwords(env_dup);
+ return (i);
}
static t_bool