-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_error.c
More file actions
88 lines (82 loc) · 2.41 KB
/
handler_error.c
File metadata and controls
88 lines (82 loc) · 2.41 KB
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
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handler_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ilnassi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/06 12:09:32 by ilnassi #+# #+# */
/* Updated: 2025/03/11 14:56:05 by ilnassi ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
/*Handles writing an error message and closing allocated resources
before exiting with a given error code*/
void error_close(char *error_message, int exit_code, t_pipex *pipex)
{
if (error_message)
{
write(STDERR_FILENO, error_message, ft_strlen(error_message));
write(STDERR_FILENO, "\n", 1);
perror("Error!");
}
if (pipex)
{
if (pipex->cmd_path1)
free(pipex->cmd_path1);
if (pipex->cmd_path2)
free(pipex->cmd_path2);
if (pipex->argv_cmd1)
free_parse_args(pipex->argv_cmd1);
if (pipex->argv_cmd2)
free_parse_args(pipex->argv_cmd2);
if (pipex->paths)
free_path_list(pipex);
if (pipex->input_fd != -1)
close(pipex->input_fd);
if (pipex->output_fd != -1)
close(pipex->output_fd);
}
exit(exit_code);
}
/*checks the validity of a command and in case of error, invokes
error_close function*/
char *cmd_and_error(char *cmd, t_pipex *pipex)
{
if (!cmd || cmd[0] == '\0')
{
error_close("Error: Not found!", EXIT_FAILURE, pipex);
return (NULL);
}
if (cmd[0] == '/' || (cmd[0] == '.' && cmd[1] == '/'))
{
if (access(cmd, X_OK) == 0)
return (ft_strdup(cmd));
else
return (NULL);
}
return (NULL);
}
/*Function that frees the list of paths associated with pipex*/
void free_path_list(t_pipex *pipex)
{
if (pipex->paths != NULL)
{
free_parse_args(pipex->paths);
pipex->paths = NULL;
}
}
/*free memory for a list of arguments*/
void free_parse_args(char **argv_cmd)
{
int i;
i = 0;
if (argv_cmd == NULL)
return ;
while (argv_cmd[i])
{
free(argv_cmd[i]);
i++;
}
free(argv_cmd);
}