-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_sum.c
93 lines (83 loc) · 2.37 KB
/
get_sum.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
88
89
90
91
92
93
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_sum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: brenaudo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/28 12:20:17 by brenaudo #+# #+# */
/* Updated: 2023/01/10 13:00:04 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void sha1sum_child(char **cmd, int *pipefd, int *pipes, \
int *children_pid);
void get_sum(char **cmd, char **ret, int *pipes, int *children_pid)
{
int fid;
int pipefd[2];
if (pipe(pipefd) < 0)
perror("minishell");
fid = fork();
if (fid == -1)
perror("fork");
else if (fid == 0)
sha1sum_child(cmd, pipefd, pipes, children_pid);
close(pipefd[1]);
waitpid(fid, NULL, 0);
free(*ret);
*ret = ft_calloc(41, sizeof(char));
read(pipefd[0], *ret, 40);
close(pipefd[0]);
}
bool is_dir(char *cmd)
{
DIR *dir;
dir = opendir(cmd);
if (dir == NULL)
return (false);
closedir(dir);
return (true);
}
static void free_cmd_tab(char **cmd)
{
if (cmd[0])
free_tab(cmd);
else
free_tab_bis(cmd);
}
static void sha1sum_free(char **cmd, char **envp, char *exec1, char *exec2)
{
free_cmd_tab(cmd);
free(exec1);
free(exec2);
free_tab(envp);
close(1);
}
static void sha1sum_child(char **cmd, int *pipefd, int *pipes, \
int *children_pid)
{
int i;
char *exec_args[3];
char **envp;
i = -1;
if (children_pid != NULL)
free(children_pid);
while (pipes && ++i < 4)
close(pipes[i]);
exec_args[0] = ft_strdup("/usr/bin/sha1sum");
exec_args[1] = ft_strdup(cmd[0]);
exec_args[2] = NULL;
close(pipefd[0]);
dup2(pipefd[1], 1);
close(pipefd[1]);
envp = envp_list_to_tab();
clean_exit(NULL);
if (!is_dir(cmd[0]) && !access(cmd[0], X_OK))
{
free_cmd_tab(cmd);
execve(exec_args[0], exec_args, envp);
}
sha1sum_free(cmd, envp, exec_args[0], exec_args[1]);
exit(1);
}