summaryrefslogtreecommitdiff
path: root/src/input_handling.c
blob: ef4cec80fe99e8e6e3a1a84f7365d7f9b6a9923d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   input_handling.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/05/02 12:13:23 by dkaiser           #+#    #+#             */
/*   Updated: 2024/05/07 15:26:08 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "libft.h"
#include "pipex.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/_types/_s_ifmt.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, S_IREAD | S_IWUSR);
	result->cmds = get_cmds(argc, argv, envp);
	return (result);
}