Fix export return value and error output
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 11:21:21 +0000 (13:21 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 12:08:30 +0000 (14:08 +0200)
inc/minishell_structs.h
src/builtins/export.c
src/tokenization.c
src/vars.c

index 3cfc08898571cb1cd2a74116930969970b307b62..4b8b98071dfcf2ade53e372408c6b2f362402dc2 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/26 09:08:46 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 11:35:10 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 13:37:07 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -52,6 +52,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                    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);
 int                    add_var(t_vec *vec, const char *key, const char *value);
index 1189227357ccce6c8f4eaf748192487db949e936..0c8593411d1d5ce58fcfc42d145125d86b2a9867 100644 (file)
@@ -6,13 +6,14 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/23 09:40:38 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/26 11:47:56 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 13:37:21 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "builtins.h"
 #include "minishell_structs.h"
 #include <stdlib.h>
+#include <unistd.h>
 
 static int     export_single(const char *var, t_execution_env *env)
 {
@@ -21,8 +22,13 @@ static int   export_single(const char *var, t_execution_env *env)
        const char      *const_value;
        int                     res;
 
+       if (!is_assignment_word(var))
+       {
+               ft_dprintf(STDERR_FILENO, "export: '%s': not a valid identifier\n", var);
+               return (1);
+       }
        name = get_var_name(var);
-       if (!name || !name[0] || (var[ft_strlen(name)] != '=' && var[ft_strlen(name)] != '\0'))
+       if (!name)
        {
                free(name);
                return (1);
@@ -50,13 +56,15 @@ static int  export_single(const char *var, t_execution_env *env)
 
 int    export(__attribute__((unused)) int argc, char **argv, t_execution_env *env)
 {
+       int             res;
        size_t  i;
 
+       res = 0;
        i = 1;
        while (argv[i])
        {
-               export_single(argv[i], env);
+               res = export_single(argv[i], env) || res;
                ++i;
        }
-       return (0);
+       return (res);
 }
index befbd2816d89a095e890ec6ea991a345639aa7e7..d41179d046f39c3acd26ffd547ad3179c1983731 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/21 16:34:43 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/26 16:16:11 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 13:36:27 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -96,38 +96,6 @@ enum token_types
        RPARA,
 };
 
-static int     is_assignment_word(const char *str)
-{
-       size_t  i;
-       size_t  j;
-
-       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] == '=')
-               {
-                       j = 0;
-                       if (ft_isdigit(str[0]))
-                               return (0);
-                       while (j < i)
-                       {
-                               if (!ft_isalnum(str[j]) && str[j] != '_')
-                                       return (0);
-                               ++j;
-                       }
-                       return (1);
-               }
-               ++i;
-       }
-       return (0);
-}
-
 int    only_contains_digits(const char *str)
 {
        while (ft_isdigit(*str))
index bebd8bf9e61488762f6316fc9e7cecc4cd1a8f28..a59e40e3f0a898be7f11abe07a8ee94ca30d8207 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:21:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/26 09:20:20 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 13:36:47 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "libft.h"
 #include <stdlib.h>
 
+static int     is_valid_var_name(const char *str, size_t size)
+{
+       size_t  j;
+
+       j = 0;
+       if (!str || size == 0)
+               return (0);
+       if (ft_isdigit(str[0]))
+               return (0);
+       while (j < size)
+       {
+               if (!ft_isalnum(str[j]) && str[j] != '_')
+                       return (0);
+               ++j;
+       }
+       return (1);
+}
+
+int    is_assignment_word(const char *str)
+{
+       size_t  i;
+
+       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);
+}
+
 int    add_var_line(t_vec *vec, const char *line)
 {
        char    *tmp;