summaryrefslogtreecommitdiffstats
path: root/src/s_init_next.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-10-03 16:35:35 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-10-03 16:35:35 +0200
commit3dcf57284e1e83544ed1569e2531061c5a390de3 (patch)
tree0f596ef25ac92f09751e0705105088c675e338e9 /src/s_init_next.c
parentfix (diff)
download42-minishell-3dcf57284e1e83544ed1569e2531061c5a390de3.tar.gz
42-minishell-3dcf57284e1e83544ed1569e2531061c5a390de3.tar.bz2
42-minishell-3dcf57284e1e83544ed1569e2531061c5a390de3.tar.xz
42-minishell-3dcf57284e1e83544ed1569e2531061c5a390de3.tar.zst
42-minishell-3dcf57284e1e83544ed1569e2531061c5a390de3.zip
Normed s_init
Diffstat (limited to '')
-rw-r--r--src/s_init_next.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/s_init_next.c b/src/s_init_next.c
new file mode 100644
index 0000000..e8e0006
--- /dev/null
+++ b/src/s_init_next.c
@@ -0,0 +1,109 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* s_init_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 <stdlib.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <errno.h>
+#include <limits.h>
+
+#include "b_export_next.h"
+#include "f_fail.h"
+#include "s_struct.h"
+#include "u_vars.h"
+#include "u_vars_next.h"
+
+void s_inc_shlvl(t_msh *msh)
+{
+ char fmt[8];
+ char tmp[255];
+ char str[255];
+ int32_t shlvl;
+
+ 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
+ {
+ shlvl = ft_atoi(tmp);
+ if (shlvl >= 999)
+ f_shlvl_too_high(shlvl);
+ shlvl = (shlvl >= 999) ? 0 : shlvl;
+ shlvl = (shlvl < 0) ? 0 : shlvl + 1;
+ ft_itoa_s(str, shlvl);
+ u_subst_var_value("$SHLVL", str, msh);
+ }
+}
+
+char **s_dupenv_del(char **nenvp, uint64_t i)
+{
+ while (i > 0)
+ {
+ ft_memdel((void*)&nenvp[i]);
+ i--;
+ }
+ ft_memdel((void*)&nenvp);
+ return (NULL);
+}
+
+char **s_dupenv(char *const envp[])
+{
+ uint64_t i;
+ char **nenvp;
+
+ i = 0;
+ while (envp[i] != NULL)
+ {
+ i++;
+ }
+ if ((nenvp = (char**)malloc((i + 1) * sizeof(char*))) == NULL)
+ {
+ return (NULL);
+ }
+ i = 0;
+ while (envp[i] != NULL)
+ {
+ if ((nenvp[i] = ft_strdup(envp[i])) == NULL)
+ return (s_dupenv_del(nenvp, i));
+ i++;
+ }
+ nenvp[i] = NULL;
+ return (nenvp);
+}
+
+void s_set_cwd(char cwd[], t_msh *msh)
+{
+ char fmt[PATH_MAX];
+ DIR *dir;
+
+ u_get_var_value(cwd, "$PWD", PATH_MAX, msh);
+ if (cwd[0] == C_NUL)
+ {
+ getcwd(cwd, PATH_MAX);
+ ft_sprintf(fmt, "%s=%s", "PWD", cwd);
+ b_export_with_equals(fmt, msh);
+ return ;
+ }
+ if ((dir = opendir(cwd)) != NULL)
+ closedir(dir);
+ else if (errno == ENOENT)
+ {
+ getcwd(cwd, PATH_MAX);
+ ft_sprintf(fmt, "%s=%s", "PWD", cwd);
+ b_export_with_equals(fmt, msh);
+ return ;
+ }
+}