aboutsummaryrefslogtreecommitdiff
path: root/src/execute_cmd.c
blob: 3730d4b02eb93a7d98f5ba1c0fcc1230e3fe3f6e (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   execute_cmd.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/21 13:58:56 by dkaiser           #+#    #+#             */
/*   Updated: 2024/10/21 15:07:37 by dkaiser          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "minishell.h"
#include <stdlib.h>
#include <sys/_types/_pid_t.h>
#include <unistd.h>

int	execute_cmd(t_cmd *cmd, t_env *env)
{
	int		result;
	pid_t	pid;

	pid = fork();
	if (pid < 0)
		return (EXIT_FAILURE);
	if (pid == 0)
	{
		result = execve(cmd->args[0], cmd->args, env_to_strlst(env));
		exit(result);
	}
	else
	{
		// only wait if cmd is on rightmost side of pipe?
		// so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4
		waitpid(pid, &result, 0);
	}
	return (result);
}