Browse Source

[enh] store env var on general struct and no more on global var.

Moul 2 years ago
parent
commit
2f19d6cafd
7 changed files with 54 additions and 56 deletions
  1. 6 6
      src/cd.c
  2. 11 11
      src/env.c
  3. 13 13
      src/exec.c
  4. 1 1
      src/main.c
  5. 2 2
      src/move_horizontally.c
  6. 8 10
      src/sh.h
  7. 13 13
      src/tabenv.c

+ 6 - 6
src/cd.c

@@ -18,16 +18,16 @@ void	cd(t_e *e, char **paths)
 		return ;
 	}
 	path = paths[0];
-	if ((pwd = get_tenv("PWD")) == NULL)
+	if ((pwd = get_tenv(e, "PWD")) == NULL)
 		pwd = get_pwd();
-	oldpwd = get_tenv("OLDPWD");
+	oldpwd = get_tenv(e, "OLDPWD");
 	if (paths[0] == NULL)
 		path = e->home != NULL ? e->home : ".";
 	if (ft_strcmp(paths[0], ".") == 0)
 		path = pwd;
 	if (ft_strcmp(paths[0], "-") == 0)
 		path = oldpwd;
-	apply_cd(path, pwd);
+	apply_cd(e, path, pwd);
 }
 
 /*
@@ -35,14 +35,14 @@ void	cd(t_e *e, char **paths)
 ** If directory changed, change PWD and OLDPWD env var
 */
 
-void	apply_cd(char *path, char *pwd)
+void	apply_cd(t_e *e, char *path, char *pwd)
 {
 	if (chdir(path) == -1)
 		error_message("cd", path, "no such file or directory");
 	else
 	{
-		ft_setenv(ft_strjoin("OLDPWD=", pwd));
-		ft_setenv(ft_strjoin("PWD=", get_pwd()));
+		ft_setenv(e, ft_strjoin("OLDPWD=", pwd));
+		ft_setenv(e, ft_strjoin("PWD=", get_pwd()));
 	}
 }
 

+ 11 - 11
src/env.c

@@ -1,17 +1,17 @@
 #include "sh.h"
 
-char	*get_tenv(char *var)
+char	*get_tenv(t_e *e, char *var)
 {
 	int		i;
 
 	i = -1;
-	while (g_tenv[++i] != NULL)
-		if (ft_strcmp(g_tenv[i], var) == 61)
-			return (ft_strchr(g_tenv[i], '=') + 1);
+	while (e->env[++i] != NULL)
+		if (ft_strcmp(e->env[i], var) == 61)
+			return (ft_strchr(e->env[i], '=') + 1);
 	return (NULL);
 }
 
-void	ft_setenv(char *namevalue)
+void	ft_setenv(t_e *e, char *namevalue)
 {
 	char	**name;
 
@@ -21,19 +21,19 @@ void	ft_setenv(char *namevalue)
 		error_message("setenv use", "setenv ENV=env", NULL);
 	else
 	{
-		if (get_tenv(name[0]) != NULL)
-			del_env(name[0]);
-		add_env(namevalue);
+		if (get_tenv(e, name[0]) != NULL)
+			del_env(e, name[0]);
+		add_env(e, namevalue);
 	}
 	ft_tabdel(name);
 }
 
-void	ft_unsetenv(char *name)
+void	ft_unsetenv(t_e *e, char *name)
 {
 	if (name == NULL || isenv(name) == 0)
 		error_message("unsetenv use", "unsetenv ENV", NULL);
-	else if (get_tenv(name) != NULL)
-		del_env(name);
+	else if (get_tenv(e, name) != NULL)
+		del_env(e, name);
 }
 
 int		isenv(char *str)

+ 13 - 13
src/exec.c

@@ -9,16 +9,16 @@
 
 int		manage_cmd_1(t_e *e, char **cmd)
 {
-	e->home = get_tenv("HOME");
+	e->home = get_tenv(e, "HOME");
 	cmd = change_tilde_to_home(e, cmd);
-	g_path = ft_strsplit(get_tenv("PATH"), ':');
+	g_path = ft_strsplit(get_tenv(e, "PATH"), ':');
 	if (ft_strcmp(cmd[0], "cd") == 0)
 		cd(e, &cmd[1]);
 	else if (ft_strcmp(cmd[0], "setenv") == 0 || \
 			ft_strcmp(cmd[0], "export") == 0)
-		ft_setenv(cmd[1]);
+		ft_setenv(e, cmd[1]);
 	else if (ft_strcmp(cmd[0], "unsetenv") == 0)
-		ft_unsetenv(cmd[1]);
+		ft_unsetenv(e, cmd[1]);
 	else if (ft_strcmp(cmd[0], "exit") == 0)
 		exit(0);
 	else
@@ -31,7 +31,7 @@ void	manage_cmd_2(t_e *e, char **cmd)
 	char	*pwd;
 
 	if (ft_strcmp(cmd[0], "env") == 0 && cmd[1] == NULL)
-		ft_puttab(g_tenv);
+		ft_puttab(e->env);
 	else if (ft_strcmp(cmd[0], "pwd") == 0)
 	{
 		pwd = get_pwd();
@@ -39,7 +39,7 @@ void	manage_cmd_2(t_e *e, char **cmd)
 		ft_strdel(&pwd);
 	}
 	else
-		execute(cmd);
+		execute(e, cmd);
 }
 
 /*
@@ -55,7 +55,7 @@ void	manage_cmd_2(t_e *e, char **cmd)
 ** exit the fork if the no execution because execution stop the fork
 */
 
-void	execute(char **cmd)
+void	execute(t_e *e, char **cmd)
 {
 	int		i;
 
@@ -68,14 +68,14 @@ void	execute(char **cmd)
 		else if (access(cmd[0], X_OK) == -1)
 			error_message(cmd[0], "no rights to execute it", "exit");
 		else
-			execve(cmd[0], &cmd[0], g_tenv);
+			execve(cmd[0], &cmd[0], e->env);
 	}
 	else
 	{
-		exec_if_perm_ok(cmd[0], cmd);
+		exec_if_perm_ok(e, cmd[0], cmd);
 		if (g_path != NULL)
 			while (g_path[++i] != NULL)
-				exec_if_perm_ok(ft_strjoinf(g_path[i],\
+				exec_if_perm_ok(e, ft_strjoinf(g_path[i],\
 				ft_strjoin("/", cmd[0]), 2), cmd);
 	}
 	error_message(cmd[0], "no such command", "exit");
@@ -87,12 +87,12 @@ void	execute(char **cmd)
 ** Else: error message
 */
 
-void	exec_if_perm_ok(char *path, char **cmd)
+void	exec_if_perm_ok(t_e *e, char *path, char **cmd)
 {
 	if (access(path, F_OK) != -1)
 	{
 		if (access(path, X_OK) != -1)
-			execve(path, &cmd[0], g_tenv);
+			execve(path, &cmd[0], e->env);
 		else
 			error_message(path, "no rights to execute it", "exit");
 	}
@@ -115,7 +115,7 @@ char	**change_tilde_to_home(t_e *e, char **cmd)
 		if (cmd[i] && cmd[i][0] == '~' && cmd[i][1] == '/')
 			cmd[i] = ft_strjoin(e->home, &cmd[i][1]);
 		if (cmd[i][0] == '$' && cmd[i][1] != '\0')
-			cmd[i] = get_tenv(&cmd[i][1]);
+			cmd[i] = get_tenv(e, &cmd[i][1]);
 	}
 	return (cmd);
 }

+ 1 - 1
src/main.c

@@ -8,8 +8,8 @@ int		main(int ac, char **av, char **env)
 {
 	t_e		*e;
 
-	g_tenv = env;
 	e = (t_e*)malloc(sizeof(t_e));
+	e->env = env;
 	e->par_open = "\'\"`[({";
 	e->par_close = "\'\"`])}";
 	signal(SIGINT, sighandler);

+ 2 - 2
src/move_horizontally.c

@@ -51,7 +51,7 @@ void	move_left(t_e *e)
 	--e->pos;
 	line = e->linep;
 	e->linep = (e->plen + e->pos) / e->col_nbr;
-	if (ft_strcmp(get_tenv("TERM_PROGRAM"), "iTerm.app") != 0
+	if (ft_strcmp(get_tenv(e, "TERM_PROGRAM"), "iTerm.app") != 0
 			&& line > e->linep)
 	{
 		i = 0;
@@ -84,7 +84,7 @@ void	move_right(t_e *e)
 	if (line < e->linep)
 	{
 		term_action("do");
-		if (ft_strcmp(get_tenv("TERM_PROGRAM"), "iTerm.app") != 0)
+		if (ft_strcmp(get_tenv(e, "TERM_PROGRAM"), "iTerm.app") != 0)
 		{
 			i = e->col_nbr;
 			while (i-- > 0)

+ 8 - 10
src/sh.h

@@ -15,8 +15,6 @@
 # define STDOUT 1
 # define STDERR 2
 
-char	**g_tenv;
-char	*g_home;
 char	**g_path;
 
 /*
@@ -84,26 +82,26 @@ char	**fill_tabe(char **tabe, char *cmd, int nbr, int *poss);
 */
 int		manage_cmd_1(t_e *e, char **cmdt);
 void	manage_cmd_2(t_e *e, char **cmdt);
-void	execute(char **cmdt);
-void	exec_if_perm_ok(char *path, char **cmd);
+void	execute(t_e *e, char **cmdt);
+void	exec_if_perm_ok(t_e *e, char *path, char **cmd);
 char	**change_tilde_to_home(t_e *e, char **cmd);
 /*
 ** env.c
 */
-char	*get_tenv(char *var);
-void	ft_setenv(char *namevalue);
-void	ft_unsetenv(char *name);
+char	*get_tenv(t_e *e, char *var);
+void	ft_setenv(t_e *e, char *namevalue);
+void	ft_unsetenv(t_e *e, char *name);
 int		isenv(char *str);
 /*
 ** tabenv.c
 */
-void	add_env(char *nenv);
-void	del_env(char *old_env);
+void	add_env(t_e *e, char *nenv);
+void	del_env(t_e *e, char *old_env);
 /*
 ** cd.c
 */
 void	cd(t_e *e, char **paths);
-void	apply_cd(char *path, char *pwd);
+void	apply_cd(t_e *e, char *path, char *pwd);
 char	*get_pwd();
 /*
 ** tools.c

+ 13 - 13
src/tabenv.c

@@ -1,34 +1,34 @@
 #include "sh.h"
 
-void	add_env(char *nenv)
+void	add_env(t_e *e, char *nenv)
 {
 	int		i;
 	char	**env;
 
-	env = (char **)malloc(sizeof(char *) * (ft_tabsize(g_tenv) + 2));
+	env = (char **)malloc(sizeof(char *) * (ft_tabsize(e->env) + 2));
 	env[0] = nenv;
 	i = 0;
-	if (g_tenv != NULL)
-		while (g_tenv[++i - 1] != NULL)
-			env[i] = g_tenv[i - 1];
+	if (e->env != NULL)
+		while (e->env[++i - 1] != NULL)
+			env[i] = e->env[i - 1];
 	env[i] = NULL;
-	g_tenv = env;
+	e->env = env;
 }
 
-void	del_env(char *old_env)
+void	del_env(t_e *e, char *old_env)
 {
 	int		i;
 	char	**env;
 
-	env = (char **)malloc(sizeof(char *) * ft_tabsize(g_tenv));
+	env = (char **)malloc(sizeof(char *) * ft_tabsize(e->env));
 	i = -1;
-	while (g_tenv[++i] != NULL && ft_strcmp(g_tenv[i], old_env) != 61)
-		env[i] = g_tenv[i];
-	while (g_tenv[i] != NULL)
+	while (e->env[++i] != NULL && ft_strcmp(e->env[i], old_env) != 61)
+		env[i] = e->env[i];
+	while (e->env[i] != NULL)
 	{
-		env[i] = g_tenv[i + 1];
+		env[i] = e->env[i + 1];
 		i++;
 	}
 	env[i] = NULL;
-	g_tenv = env;
+	e->env = env;
 }