Fix the wildcards' interaction with directories
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 22 Aug 2024 09:53:24 +0000 (11:53 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 22 Aug 2024 09:53:24 +0000 (11:53 +0200)
src/wildcards.c

index 703608a351fb0f7299dc91f65e84f62b8bb0d378..010c36d7dda7a40e983e71f069a86f70d8618162 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/08 10:50:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/08 15:34:37 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/22 11:30:03 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -75,6 +75,11 @@ int  add_conformant(t_vec *expanded, t_wildcard_info *info)
                ++info->current_expand_char;
                return (expand_further(expanded, *info));
        }
+       if (*info->current_expand_char == '/' && info->current_entry_char == info->entry)
+       {
+               ++info->current_expand_char;
+               return (add_conformant(expanded, info));
+       }
        if ((*info->current_expand_char == '?' && *info->current_entry_char != '\0')
                        || *info->current_expand_char == *info->current_entry_char)
        {
@@ -92,7 +97,10 @@ int  expand_dir(t_vec *expanded, t_wildcard_info *info)
        DIR                             *dir;
        struct dirent   *dir_entry;
 
-       dir = opendir(info->path);
+       if (info->path[0] == '\0')
+               dir = opendir("./");
+       else
+               dir = opendir(info->path);
        if (!dir)
                return (0);
        dir_entry = readdir(dir);
@@ -153,15 +161,25 @@ int       expand_word(char **str, const t_execution_env *env)
                str[0][ft_strlen(*str) - 1] = '\0';
        if (ft_vec_init(&matched, sizeof(char *)) != success)
                return (1);
+       info.to_expand = *str;
        if (str[0][0] == '/')
                info.path = ft_strdup("/");
-       else if (str[0][0] == '~')
-               info.path = ft_strjoin(get_env_var_value(env, "HOME"), "/");
+       else if (str[0][0] == '~' && (ft_isalpha(str[0][1]) || str[0][1] == '/' || str[0][1] == '~' || str[0][1] == '+' || str[0][1] == '-'))
+       {
+               if (str[0][1] == '/' || str[0][1] == '\0')
+                       info.path = ft_strdup(get_env_var_value(env, "HOME"));
+               else
+               {
+                       ft_printf("Advanced tilde expansion not implemented.\n");
+                       return (1);
+               }
+       }
        else
-               info.path = ft_strdup("./");
+               info.path = ft_strdup("");
        if (!info.path)
                return (1);
-       info.to_expand = *str;
+       if (info.path[0] && ft_strchr("/~", info.path[0]) != NULL)
+               ++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);