summaryrefslogtreecommitdiffstats
path: root/src/s_init.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-09-06 21:56:20 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-09-06 21:56:20 +0200
commit4543c3ba3222d47780ad3e091cfe6f3098cc2bca (patch)
treefb8a70bf05464a6fa2c820cc43ea34421340c903 /src/s_init.c
parentCommit from minishell (diff)
download42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.gz
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.bz2
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.xz
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.zst
42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.zip
Stacked
Diffstat (limited to 'src/s_init.c')
-rw-r--r--src/s_init.c44
1 files changed, 21 insertions, 23 deletions
diff --git a/src/s_init.c b/src/s_init.c
index 7353ed8..8fc8a79 100644
--- a/src/s_init.c
+++ b/src/s_init.c
@@ -107,64 +107,60 @@ static char
static void
inc_shlvl(t_msh *msh)
{
- int32_t shlvl;
- char *str_one;
- char *str_two;
- char *tmp;
char fmt[8];
+ char tmp[255];
+ char *str;
+ int32_t shlvl;
- if ((tmp = u_get_var_value("$SHLVL", msh)) == NULL)
+ u_get_var_value(tmp, "$SHLVL", 255, msh);
+ if (tmp[0] == C_NUL)
{
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);
+ shlvl = ft_atoi(tmp);
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);
+ str = ft_itoa(shlvl);
+ u_subst_var_value("$SHLVL", str, msh);
+ ft_memdel((void*)&str);
}
}
-static char
- *set_cwd(t_msh *msh)
+static void
+ set_cwd(char cwd[], t_msh *msh)
{
- char *cwd;
char fmt[PATH_MAX];
DIR *dir;
- if ((cwd = u_get_var_value("$PWD", msh)) == NULL)
+ u_get_var_value(cwd, "$PWD", PATH_MAX, msh);
+ if (cwd[0] == C_NUL)
{
- cwd = getcwd(NULL, 0);
+ getcwd(cwd, PATH_MAX);
ft_sprintf(fmt, "%s=%s", "PWD", cwd);
b_export_with_equals(fmt, msh);
- return (cwd);
+ return ;
}
if ((dir = opendir(cwd)) != NULL)
closedir(dir);
else if (errno == ENOENT)
{
- ft_memdel((void*)&cwd);
- cwd = getcwd(NULL, 0);
+ getcwd(cwd, PATH_MAX);
ft_sprintf(fmt, "%s=%s", "PWD", cwd);
b_export_with_equals(fmt, msh);
- return (cwd);
+ return ;
}
- return (cwd);
}
t_msh
*init_msh(char *const argv[],
char *const envp[])
{
+ char cwd[PATH_MAX];
t_msh *msh;
if (!(msh = (t_msh*)malloc(sizeof(t_msh))))
@@ -179,7 +175,9 @@ t_msh
init_buptr(msh);
msh->curr = NULL;
msh->vars = NULL;
- msh->cwd = set_cwd(msh);
+ set_cwd(cwd, msh);
+ if ((msh->cwd = ft_strdup(cwd)) == NULL)
+ return (NULL);
msh->env_fork_tmp[0][0] = '\0';
inc_shlvl(msh);
s_init_sqb_ref_one(msh);