-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_exec.c
More file actions
41 lines (38 loc) · 1.5 KB
/
Copy pathcmd_exec.c
File metadata and controls
41 lines (38 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariyad <ariyad@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/31 17:04:32 by ariyad #+# #+# */
/* Updated: 2025/01/27 16:23:37 by ariyad ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int exec(char *av, char **envp, int *pipe, int infile)
{
char *cmd;
char **args;
char **paths;
int out;
if (fork() == 0)
{
out = pipe[1];
if (dup2(infile, 0) < 0 || dup2(out, 1) < 0)
exit(EXIT_FAILURE);
close(pipe[0]);
paths = get_paths(envp);
args = ft_split(av, ' ');
if (!paths || !args)
exit(1);
if (ft_strncmp(args[0], "./", 2) == 0 || *args[0] == '/'
|| !access(args[0], X_OK))
cmd = args[0];
else
cmd = get_cmd_path(paths, args[0]);
return (execve(cmd, args, envp), free_table(paths), free_table(args),
write(2, "Command invalid\n", 17), exit(1), 1);
}
return (0);
}