summaryrefslogtreecommitdiffstats
path: root/src/e_unshebanged.c
blob: 49978a9b632f42fde9443451b45071ccc796f4c0 (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
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   e_unshebanged.c                                    :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <errno.h>
#include <libft.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif
#include <stdlib.h>
#include <unistd.h>

#include "f_fail.h"
#include "s_struct.h"
#include "s_com.h"
#include "s_destroy.h"
#include "s_line.h"
#include "s_lpipes.h"
#include "u_utils.h"

static char	**e_get_new_argv(const char fullpath[], t_com *ptr, t_msh *msh)
{
	size_t	i;
	char	**argv;

	if ((argv = (char**)malloc((3 +
		u_builtins_get_argc((const char**)ptr->argv)) * sizeof(char*))) == NULL)
		f_alloc_and_destroy_msh(msh);
	if ((argv[0] = ft_strdup(msh->argv[0])) == NULL
		|| (argv[1] = ft_strdup(fullpath)) == NULL)
	{
		ft_memdel((void*)&argv);
		f_alloc_and_destroy_msh(msh);
	}
	i = 2;
	while (ptr->argv[i - 2] != NULL)
	{
		if ((argv[i] = ft_strdup(ptr->argv[i - 2])) == NULL)
		{
			ft_memdel((void*)&argv);
			f_alloc_and_destroy_msh(msh);
		}
		i++;
	}
	argv[i] = NULL;
	return (argv);
}

void		e_extern_read_script(const char fullpath[],
								t_com *ptr,
								t_msh *msh,
								t_bool pipe)
{
	char	**argv;

	argv = e_get_new_argv(fullpath, ptr, msh);
	if (execve(msh->argv[0], (char *const *)argv, msh->envp) == -1)
	{
		f_exec(msh->argv[0], argv[0]);
		ft_delwords(argv);
		u_eof_fd(msh->fd);
		s_com_destroy(&msh->com);
		if (pipe == TRUE)
			s_lpipes_clear(&msh->pipes);
		s_line_clear(&msh->curr);
		s_destroy(msh);
		exit(errno);
	}
}