-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
103 lines (93 loc) · 2.25 KB
/
utils.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
94
95
96
97
98
99
100
101
102
103
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alkaram <alkaram@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 21:22:01 by alkaram #+# #+# */
/* Updated: 2024/06/12 17:04:05 by alkaram ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_error(char *error_message, int system_function)
{
if (!system_function)
ft_putendl_fd(error_message, STDERR_FILENO);
else
perror(error_message);
exit(1);
}
void ft_free(char **s)
{
size_t i;
i = -1;
while (s[++i])
free(s[i]);
free(s);
}
char *get_env(char *name, char **env)
{
int i;
int j;
char *sub;
i = 0;
while (env[i])
{
j = 0;
while (env[i][j] && env[i][j] != '=')
j++;
sub = ft_substr(env[i], 0, j);
if (ft_strcmp(sub, name) == 0)
{
free(sub);
return (env[i] + j + 1);
}
free(sub);
i++;
}
return (NULL);
}
char *get_path(char *cmd, char **env)
{
int i;
char *exec;
char **all_path;
char *path_part;
char **s_cmd;
i = 0;
all_path = ft_split(get_env("PATH", env), ':');
s_cmd = ft_split(cmd, ' ');
while (all_path[i])
{
path_part = ft_strjoin(all_path[i], "/");
exec = ft_strjoin(path_part, s_cmd[0]);
free(path_part);
if (access(exec, F_OK | X_OK) == 0)
{
ft_free(s_cmd);
return (exec);
}
free(exec);
i++;
}
ft_free(all_path);
ft_free(s_cmd);
return (cmd);
}
int ft_strcmp(char *s1, char *s2)
{
unsigned char *str1;
unsigned char *str2;
size_t i;
if (!s1)
return (0);
if (ft_strlen(s1) != ft_strlen(s2))
return (1);
str1 = (unsigned char *)s1;
str2 = (unsigned char *)s2;
i = 0;
while (str1[i] && str2[i] && str1[i] == str2[i])
i++;
return (str1[i] - str2[i]);
}