]> git.dkaiser.de - 42/pipex.git/commitdiff
Add changes from input_handling
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 7 May 2024 13:18:01 +0000 (15:18 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 7 May 2024 13:18:01 +0000 (15:18 +0200)
This is not how it should be done, but I just need to finish this quickly.

Makefile
src/env_utils.c
src/input_handling.c [new file with mode: 0644]

index 662ec7c53c2cdb26632598d618e39748f6394a7d..69c304efbe4e079446069031d364688129e58d04 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,7 @@ HEADERS =       -Iinclude -Ilibft
 LIBS   =       -Llibft -lft
 
 VPATH  :=      src
-SRC            =       main.c
+SRC            =       main.c env_utils.c input_handling.c
 
 OBJ_DIR        :=      obj
 OBJ            :=      $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o))
index b352d728a956fa338d512ee8000df41aacfb6e3f..4e02032346756905974c966f5301d3445b9d9369 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 16:19:31 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/07 15:13:51 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/07 15:16:32 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -62,8 +62,7 @@ char  *get_cmd_path(char *cmd, char **path, char *pwd)
        char    *cur_dir;
 
        if (cmd[0] == '/')
-               return (cmd); // TODO: Maybe use duplicate instead,
-                       so there will be no problem on free()
+               return (cmd); // TODO: Maybe use duplicate instead,     so there will be no problem on free()
        else if (strchr(cmd, '/'))
        {
                cur_dir = ft_strjoin(pwd, "/");
diff --git a/src/input_handling.c b/src/input_handling.c
new file mode 100644 (file)
index 0000000..e4bf7d6
--- /dev/null
@@ -0,0 +1,56 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   input_handling.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/05/02 12:13:23 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/05/07 15:17:19 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "pipex.h"
+#include <fcntl.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+static char    **get_cmds(int argc, char *argv[], char *envp[])
+{
+       char    **cmds;
+       int             i;
+       char    **path;
+       char    *pwd;
+
+       cmds = malloc(sizeof(char *) * (argc - 2));
+       if (!cmds)
+               return (NULL);
+       path = get_split_path(envp);
+       // TODO: Free on fail
+       pwd = get_pwd(envp);
+       i = 2;
+       while (i < argc - 1)
+       {
+               cmds[i - 2] = get_cmd_path(argv[i], path, pwd);
+               i++;
+       }
+       i = 0;
+       while (path[i])
+               free(path[i++]);
+       free(path);
+       return (cmds);
+}
+
+t_pxdata       *get_pxdata(int argc, char *argv[], char *envp[])
+{
+       t_pxdata        *result;
+
+       result = malloc(sizeof(t_pxdata));
+       if (!result)
+               return (NULL); // TODO: Check if an error message needs to be sent
+       result->in_fd = open(argv[1], O_RDONLY);
+       result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC);
+       result->cmds = get_cmds(argc, argv, envp);
+       return (result);
+}