Fix setting another value to exported variable
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 08:07:46 +0000 (10:07 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 08:07:46 +0000 (10:07 +0200)
Instead of setting an exported variable to new value with the command of
the form <NAME>=<NEW_VALUE> a new non-exported variable was created and
used for variable expasion. The exported value was passed to child
processes.
This behaviour is fixed by this commit to the expected setting of
exported variable to the new value.

src/execution.c

index 1e41ab9a8fef2cbd8f926cac51b820beb564803e..775cf77a7c9258a4c4b5a0fa6e8e54b9a0ab2b8f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/26 16:47:04 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 10:06:59 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -554,7 +554,26 @@ char       **expand(t_parse_tree_node *simple_command, const t_execution_env *env)
 
 int    assignments_to_env(const t_vec *assignments, t_execution_env *env)
 {
-       return (ft_vec_insert_range(&env->vars->other, assignments->vec, assignments->size, 0) == success);
+       size_t          i;
+       const char      *var_line;
+       char            *var_name;
+
+       i = 0;
+       while (i < assignments->size)
+       {
+               var_line = ft_vec_caccess(assignments, i);
+               var_name = get_var_name(var_line);
+               if (!var_name)
+                       return (1);
+               if (set_env_var_value(env, var_name, var_line + ft_strlen(var_name) + 1))
+               {
+                       free(var_name);
+                       return (1);
+               }
+               free(var_name);
+               ++i;
+       }
+       return (0);
 }
 
 void   free_split(char **fields)