-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
120 lines (109 loc) · 2.87 KB
/
Copy pathutils.c
File metadata and controls
120 lines (109 loc) · 2.87 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tbui-quo <tbui-quo@student.42wolfsburg.d> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/13 18:10:39 by tbui-quo #+# #+# */
/* Updated: 2023/06/13 18:10:39 by tbui-quo ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_free_array(char **array)
{
size_t i;
i = 0;
while (array[i])
{
free(array[i]);
i++;
}
free(array);
}
int open_input_or_output_file(char *filename, int in_or_out)
{
int ret;
ret = 0;
if (in_or_out == INPUT)
ret = open(filename, O_RDONLY, 0666);
else if (in_or_out == OUTPUT)
ret = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0644);
else
print_error_msg_and_exit(ERR_IMPOSSIBLE);
if (ret == -1)
print_error_open_file_and_exit(filename, ERR_OPEN_FILE);
return (ret);
}
char *get_env_value_by_name(char *envVarName, char **env)
{
int i;
char *equal_sign_pos;
char *path;
i = 0;
if (*env == NULL)
return ("/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:");
while (env[i])
{
if (ft_strncmp(env[i], envVarName, ft_strlen(envVarName)) == 0)
{
equal_sign_pos = ft_strchr(env[i], '=');
if (equal_sign_pos)
{
path = equal_sign_pos + 1;
return (path);
}
}
i++;
}
return (NULL);
}
char *get_exec_path(char *cmd, char **env)
{
int i;
char *exec_path;
char **path_list;
char *current_path;
char **s_cmd;
i = -1;
path_list = ft_split(get_env_value_by_name("PATH", env), ':');
s_cmd = ft_split(cmd, ' ');
while (path_list[++i])
{
current_path = ft_strjoin(path_list[i], "/");
exec_path = ft_strjoin(current_path, s_cmd[0]);
free(current_path);
if (access(exec_path, X_OK) == 0)
{
ft_free_array(s_cmd);
return (exec_path);
}
free(exec_path);
}
ft_free_array(path_list);
ft_free_array(s_cmd);
print_error_cmd_not_found_and_exit(cmd, ERR_CMD);
return (NULL);
}
char **parse_command_with_quotes(char *cmd)
{
int c_index;
char quote_marker;
char **split_cmd;
c_index = -1;
while (cmd[++c_index])
{
if (cmd[c_index] == ' ')
cmd[c_index] = '\x1A';
if (cmd[c_index] == '\'' || cmd[c_index] == '\"')
{
quote_marker = cmd[c_index];
cmd[c_index] = '\x1A';
while (cmd[c_index] != quote_marker && cmd[c_index])
c_index++;
cmd[c_index] = '\x1A';
}
}
split_cmd = ft_split(cmd, '\x1A');
return (split_cmd);
}