diff options
Diffstat (limited to 'src/s_init.c')
-rw-r--r-- | src/s_init.c | 44 |
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); |