]> git.dkaiser.de - 42/minishell.git/commitdiff
update for builtins and extra functions
authorChristopher Uhlig <chuhlig@1-D-6.42heilbronn.de>
Fri, 25 Oct 2024 18:53:44 +0000 (20:53 +0200)
committerChristopher Uhlig <chuhlig@1-D-6.42heilbronn.de>
Fri, 25 Oct 2024 18:53:44 +0000 (20:53 +0200)
src/builtins_part_one.c
src/builtins_part_two.c [new file with mode: 0644]

index d817336ed40d8dc039719cfbf3102aa1e2683973..6b92d9c012acc3979ef56a4bc8bd00d3d4d1cf23 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/09 17:01:16 by chuhlig           #+#    #+#             */
-/*   Updated: 2024/10/17 14:29:40 by chuhlig          ###   ########.fr       */
+/*   Updated: 2024/10/25 20:52:36 by chuhlig          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -39,7 +39,6 @@ int   echo(char **av)
        return (0);
 }
 
-
 int    pwd(t_env **env, char *av)
 {
        t_env   *current;
@@ -50,7 +49,7 @@ int   pwd(t_env **env, char *av)
        prev = NULL;
        while (current)
        {
-               if(ft_strcmp(current->name, av == 0))
+               if (ft_strcmp(current->name, av == 0))
                        break ;
                prev = current;
                current = current->next;
@@ -76,85 +75,12 @@ int env(t_env **env)
        return (0);
 }
 
-void   update_oldpwd(t_env **env)
-{
-       t_env   *current;
-       t_env   *prev;
-       char    cwd[1028];
-       char    *tmp;
-
-       while (current) // home command
-       {
-               if (ft_strncmp(current->name, "OLDPWD", 6) == 0)
-                       break ;
-               prev = current;
-               current = current->next;
-       }
-       getcwd(cwd, sizeof(cwd));
-       tmp = ft_strdup(cwd);
-       free(current->value);
-       current->value = tmp; 
-}
-
-void   update_pwd(t_env **env)
-{
-       t_env   *current;
-       t_env   *prev;
-       char    cwd[1028];
-       char    *tmp;
-
-       while (current)
-       {
-               if (ft_strncmp(current->name, "PWD", 3) == 0)
-                       break ;
-               prev = current;
-               current = current->next;
-       }
-       getcwd(cwd, sizeof(cwd));
-       tmp = ft_strdup(cwd);
-       free(current->value);
-       current->value = tmp;
-}
-
-int    cd(t_env **env,char **av)
-{
-       t_env   *current;
-       t_env   *prev;
-       t_env   *pwd;
-
-       current = env;
-       if(av[1] == NULL)
-       {
-               update_oldpwd(&env);
-               while (current) // home command
-               {
-                       if (ft_strncmp(current->name, "HOME", 4) == 0)
-                               break ;
-                       prev = current;
-                       current = current->next;
-               }
-               if(chdir(current->value) == -1)
-                       return ;
-       }
-       else
-       {
-               update_oldpwd(&env);
-               if(chdir(av[1]) ==  -1)
-                       return ;
-               update_pwd(&env);
-       }
-}
-
-// exit
-
 int    exit(char *av)
 {
-       freenode free toke free sequence stop repl free env
-       clear history
+       freenode free toke free sequence stop repl free env;
+       clear history;
 }
 
-//export
-
 int    export(char **av, t_env **env)
 {
        char    *tmp;
@@ -162,87 +88,65 @@ int        export(char **av, t_env **env)
        int             i;
 
        i = i;
-       while(av[i])
+       while (av[i])
        {
-               if(tmp = ft_strchr(av[i], '='))
+               if (t_strchr(av[i], '='))
                {
+                       tmp = ft_strchr(av[i], '=');
                        tmp = '\0';
                        current = *env;
-                       while(current)
+                       while (current)
                        {
-                               if (strcmp(current->name, av[i]) == 0)
+                               if (ft_strcmp(current->name, tmp[i]) == 0)
                                {
-                   free(current->value);
-                   current->value = ft_strdup(tmp + 1);
-                   break;
-               }
-               current = current->next;
+                                       free(current->value);
+                                       current->value = ft_strdup(tmp + 1);
+                                       break ;
+                               }
+                               current = current->next;
                        }
                        if (!current)
                        {
-               current = malloc(sizeof(t_env));
-               current->name = ft_strdup(av[i]);
-               current->value = ft_strdup(tmp + 1);
-               current->next = *env;
-               *env = current;
-           }
-                       return (0);
+                               current = malloc(sizeof(t_env));
+                               current->name = ft_strdup(av[i]);
+                               current->value = ft_strdup(tmp + 1);
+                               current->next = *env;
+                               *env = current;
+                       }
                }
                i++;
        }
-       return 1;
+       return (1);
 }
 
-//unset
-//for unset as well check and for name part
 int    unset(char **av, t_env **env)
 {
        t_env   *current;
        t_env   *prev;
+       int             i;
 
+       i = 0;
        current = env;
        prev = NULL;
-       while (current)
-       {
-               if(ft_strcmp(current->name, av[1] == 0))
-                       break ;
-               prev = current;
-               current = current->next;
-       }
-       if(current)
+       while (av[i])
        {
-               if(prev)
-                       prev->next = current->next;
-               else
-                       *env = current->next;
-               free(current->value);
-               free(current);
+               while (current)
+               {
+                       if (ft_strcmp(current->name, av[i] == 0))
+                               break ;
+                       prev = current;
+                       current = current->next;
+               }
+               if (current)
+               {
+                       if (prev)
+                               prev->next = current->next;
+                       else
+                               *env = current->next;
+                       free(current->value);
+                       free(current);
+               }
+               i++;
        }
        return (0);
-}
-
-
-// void        getenvlststr(t_env **env, char **en)//without serparation
-// {
-//     int                     i;
-//     size_t          t;
-//     t_env   *current;
-
-//     t = 0;
-//     i = 0;
-//     while (en[i] != NULL)
-//     {
-//             current = *env;
-//         current = malloc(sizeof(t_env));
-//             while(en[t] != '=')
-//                     t++;
-//         current->name = ft_substr(en[i], 0, t);
-//         current->value = ft_strdup(en[i]);
-//         current->next = *env;
-//         *env = current;
-//             i++;
-//     }
-//     return (0);
-// }
-
-//should name the list different
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c
new file mode 100644 (file)
index 0000000..94ef258
--- /dev/null
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   builtins_part_two.c                                :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/10/25 20:52:16 by chuhlig           #+#    #+#             */
+/*   Updated: 2024/10/25 20:52:46 by chuhlig          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "env.h"
+
+void   update_oldpwd(t_env **env)
+{
+       t_env   *current;
+       t_env   *prev;
+       char    cwd[1028];
+       char    *tmp;
+
+       while (current)
+       {
+               if (ft_strncmp(current->name, "OLDPWD", 6) == 0)
+                       break ;
+               prev = current;
+               current = current->next;
+       }
+       getcwd(cwd, sizeof(cwd));
+       tmp = ft_strdup(cwd);
+       free(current->value);
+       current->value = tmp;
+}
+
+void   update_pwd(t_env **env)
+{
+       t_env   *current;
+       t_env   *prev;
+       char    cwd[1028];
+       char    *tmp;
+
+       while (current)
+       {
+               if (ft_strncmp(current->name, "PWD", 3) == 0)
+                       break ;
+               prev = current;
+               current = current->next;
+       }
+       getcwd(cwd, sizeof(cwd));
+       tmp = ft_strdup(cwd);
+       free(current->value);
+       current->value = tmp;
+}
+
+int    cd(t_env **env, char **av)
+{
+       t_env   *current;
+       t_env   *prev;
+       t_env   *pwd;
+
+       current = env;
+       if (av[1] == NULL)
+       {
+               update_oldpwd(&env);
+               while (current)
+               {
+                       if (ft_strncmp(current->name, "HOME", 4) == 0)
+                               break ;
+                       prev = current;
+                       current = current->next;
+               }
+               if (chdir(current->value) == -1)
+                       return ;
+       }
+       else
+       {
+               update_oldpwd(&env);
+               if (chdir(av[1]) == -1)
+                       return ;
+               update_pwd(&env);
+       }
+}
\ No newline at end of file