-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_redirect_input.c
32 lines (29 loc) · 1.42 KB
/
ft_redirect_input.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_redirect_input.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/01 15:04:18 by abenamar #+# #+# */
/* Updated: 2023/09/13 16:50:47 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
uint8_t ft_redirect_input(char *file, t_list **env, size_t last)
{
char **argv;
int fd;
argv = ft_command_split(file, env, ' ', 1);
if (!argv || argv[1])
return (ft_pstderr2(file, ": ambiguous redirect"), \
ft_tab_free(argv), 0);
fd = open(argv[0], O_RDONLY);
if (fd == -1)
return (ft_perror2(argv[0], ": "), ft_tab_free(argv), 0);
if (last && dup2(fd, STDIN_FILENO) == -1)
return (ft_perror2("dup2", argv[0]), 0);
if (close(fd) == -1)
return (ft_perror2("close", argv[0]), 0);
return (ft_tab_free(argv), 1);
}