-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_pipex.c
87 lines (78 loc) · 2.29 KB
/
utils_pipex.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
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_pipex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <zhabri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/22 11:02:00 by zhabri #+# #+# */
/* Updated: 2023/01/09 13:49:05 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void exit_on_not_existing_file(char *cmd, char **cmd_split, \
int *pipes, int *children_pid)
{
char **cmd_split_bis;
cmd_split_bis = ft_split_quotes(cmd, " \t");
if (cmd_split[0] == NULL && ft_strchr(cmd, '/') && \
access(cmd_split_bis[0], F_OK) != 0)
{
ft_putstr_fd(cmd_split[0], 2);
ft_putstr_fd(": No such file or directory\n", 2);
free_tab(cmd_split);
free_tab(cmd_split_bis);
close_pipes(pipes);
clean_exit(children_pid);
exit(127);
}
free_tab(cmd_split_bis);
}
void exit_on_bad_cmd(char **cmd_split, \
int *pipes, char *cmd, int *children_pid)
{
if (cmd_split[0] == NULL)
{
free_tab_bis(cmd_split);
close_pipes(pipes);
print_cmd_not_found(cmd);
clean_exit(children_pid);
exit(127);
}
}
void exit_on_permission(char **cmd_split, \
int *pipes, int *children_pid)
{
if (access(cmd_split[0], X_OK) != 0)
{
ft_putstr_fd(cmd_split[0], 2);
ft_putstr_fd(": Permission denied\n", 2);
free_tab(cmd_split);
close_pipes(pipes);
clean_exit(children_pid);
exit(126);
}
}
void print_cmd_not_found(char *str)
{
char **str_split;
str_split = ft_split_quotes(str, " \t");
if (str_split[0][0] == '\0')
{
free(str_split[0]);
str_split[0] = ft_strdup("\'\'");
}
ft_putstr_fd(str_split[0], 2);
ft_putstr_fd(": command not found\n", 2);
free_tab(str_split);
}
void close_pipes(int *pipes)
{
int i;
i = -1;
while (++i < 4)
{
if (pipes[i] > 2)
close(pipes[i]);
}
}