-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_execution2.c
88 lines (78 loc) · 2.1 KB
/
ft_execution2.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_execution2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ralopez- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/03 12:54:48 by ralopez- #+# #+# */
/* Updated: 2023/06/03 12:54:49 by ralopez- ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int num_cmds(t_inf *info)
{
int i;
t_command *aux;
i = 0;
aux = info->commands;
while (aux)
{
i++;
aux = aux->next;
}
return (i);
}
/** create_cmd:
* This function create the string with the command to execute
* Desalocates the memory that dont use.
*/
char *create_cmd(t_inf *info, int i, char *cmd)
{
char *cmd2;
char *res;
cmd2 = ft_strjoin(info->paths[i], "/");
res = ft_strjoin(cmd2, cmd);
free(cmd2);
return (res);
}
char *look_in_path(t_inf *info, char *cmd)
{
char *cmd_ret;
int i;
i = -1;
while (info->paths[++i] != NULL)
{
cmd_ret = create_cmd(info, i, cmd);
if (access(cmd_ret, F_OK | X_OK) == 0)
break ;
free(cmd_ret);
cmd_ret = NULL;
}
return (cmd_ret);
}
char *get_path(char *cmd, t_inf *info)
{
char *cmd_ret;
if (cmd == NULL || cmd[0] == '\0')
return (NULL);
if (access(cmd, F_OK | X_OK) == 0)
return (cmd);
if (info->paths == NULL)
{
if (access(cmd, F_OK | X_OK) == 0)
return (cmd);
free(cmd);
return (NULL);
}
cmd_ret = look_in_path(info, cmd);
free(cmd);
return (cmd_ret);
}
int display_error_path(char *cmd_original)
{
if (g_info.paths == NULL)
return (msg(cmd_original, NOFILE, "", 127));
else
return (msg(cmd_original, NOCMD, "", 127));
}