Fix export var validation
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 30 Aug 2024 07:20:56 +0000 (09:20 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 30 Aug 2024 07:20:56 +0000 (09:20 +0200)
Assignment word recognition was changed so that export can
reuse part of its logic.

inc/minishell_structs.h
src/builtins/export.c
src/vars.c

index e3894c7446463c614443ee83e1a8a6bdf80640c2..0ebdff970e858fd69514262b4b1eb0ea3f2f4a15 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/26 09:08:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/28 09:36:55 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/30 09:16:51 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -54,6 +54,7 @@ int                   init_vars(t_vars *vars, char **envp);
 int                    init_env(t_execution_env *env, char **envp);
 int                    init_tree(t_tree *tree);
 
+int                    has_valid_name(const char *str);
 int                    is_assignment_word(const char *str);
 int                    cmp_var_name(const char *a, const char *b);
 int                    add_var_line(t_vec *vec, const char *line);
index 0c8593411d1d5ce58fcfc42d145125d86b2a9867..c919a3e308bb81777cda69147218c893e6ea0a12 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/23 09:40:38 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 13:37:21 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/30 09:16:28 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -22,7 +22,7 @@ static int    export_single(const char *var, t_execution_env *env)
        const char      *const_value;
        int                     res;
 
-       if (!is_assignment_word(var))
+       if (!has_valid_name(var))
        {
                ft_dprintf(STDERR_FILENO, "export: '%s': not a valid identifier\n", var);
                return (1);
index a59e40e3f0a898be7f11abe07a8ee94ca30d8207..d9b445b1e8e63246791cf611f614f1555df0c0f0 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:21:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 13:36:47 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/30 09:14:10 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "libft.h"
 #include <stdlib.h>
 
-static int     is_valid_var_name(const char *str, size_t size)
+// This function returns 0 when str does not contain valid variable name
+// in front of = else it returns the length of the valid name
+int    has_valid_name(const char *str)
 {
-       size_t  j;
+       size_t  i;
 
-       j = 0;
-       if (!str || size == 0)
+       if (!str)
                return (0);
        if (ft_isdigit(str[0]))
                return (0);
-       while (j < size)
+       i = 0;
+       while (str[i])
        {
-               if (!ft_isalnum(str[j]) && str[j] != '_')
+               if (str[i] == '=')
+                       return (i);
+               if (!ft_isalnum(str[i]) && str[i] != '_')
                        return (0);
-               ++j;
+               ++i;
        }
-       return (1);
+       return (i);
 }
 
 int    is_assignment_word(const char *str)
 {
-       size_t  i;
+       size_t  len;
 
-       i = 0;
-       while (str[i])
-       {
-               if (str[i] == '"')
-                       while (str[i] && str[i] != '"')
-                               ++i;
-               else if (str[i] == '\'')
-                       while (str[i] && str[i] != '\'')
-                               ++i;
-               else if (str[i] == '=')
-                       return (is_valid_var_name(str, i));
-               ++i;
-       }
-       return (0);
+       len = has_valid_name(str);
+       return (len > 0 && str[len] == '=');
 }
 
 int    add_var_line(t_vec *vec, const char *line)