Make wildcard expansion quoted
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 22 Aug 2024 11:05:21 +0000 (13:05 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 22 Aug 2024 13:27:40 +0000 (15:27 +0200)
This is done for the expansion not to be expanded further for variable
names. This also prevents files with spae in their name to be split to
multiple tokens.

src/wildcards.c

index 62edf860133a17ed2258f9c10a04c3d502238f75..8274f49ce70d2a167e0ccd15cf1bc0a06dd633d2 100644 (file)
@@ -133,16 +133,46 @@ int       replace_str_by_joint_split(char **str, char **expanded_split)
        while (expanded_split[i])
                len += ft_strlen(expanded_split[i++]) + 1;
        *str = malloc(len + 1 + 1);
+       if (!str)
+       {
+               *str = tmp;
+               return (1);
+       }
        str[0][0] = '\0';
+       i = 0;
+       while (expanded_split[i])
+       {
+               ft_strlcat(*str, expanded_split[i++], len + 1 + 1);
+               ft_strlcat(*str, " ", len + 1 + 1);
+       }
+       free(tmp);
+       return (0);
+}
+
+int    replace_str_by_joint_quoted_split(char **str, char **expanded_split)
+{
+       char    *tmp;
+       size_t  len;
+       size_t  i;
+
+       tmp = *str;
+       len = 0;
+       i = 0;
+       while (expanded_split[i])
+               len += ft_strlen(expanded_split[i++]) + 2 + 1;
+       *str = malloc(len + 1 + 1);
        if (!str)
        {
                *str = tmp;
                return (1);
        }
+       str[0][0] = '\0';
        i = 0;
        while (expanded_split[i])
        {
+               ft_strlcat(*str, "'", len + 1 + 1);
                ft_strlcat(*str, expanded_split[i++], len + 1 + 1);
+               ft_strlcat(*str, "'", len + 1 + 1);
                ft_strlcat(*str, " ", len + 1 + 1);
        }
        free(tmp);
@@ -179,7 +209,7 @@ int expand_word(char **str, const t_execution_env *env)
                ++info.to_expand;
        res = expand_dir(&matched, &info);
        res = res || (ft_vec_append(&matched, &nullptr) != success);
-       res = res || replace_str_by_joint_split(str, matched.vec);
+       res = res || replace_str_by_joint_quoted_split(str, matched.vec);
        ft_vec_free(&matched, free_str);
        free((char *)info.path);
        if (last_char == '\n')