-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
99 lines (89 loc) · 1.98 KB
/
utils.c
File metadata and controls
99 lines (89 loc) · 1.98 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: moabed <moabed@student.42amman.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/12 21:49:37 by moabed #+# #+# */
/* Updated: 2025/12/30 15:37:06 by moabed ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
char **findpath(char **evar)
{
int i;
i = 0;
while (evar[i])
{
if (ft_strncmp(evar[i], "PATH=", 5) == 0)
return (ft_split(evar[i] + 5, ':'));
i++;
}
return (NULL);
}
void error(int check, int *fd)
{
if (check == 0)
{
write(2, "Bad args\nExample: "
"./pipex file1 cmd1 cmd2 file2\n", 49);
exit(EXIT_FAILURE);
}
else if (check == 1)
{
close(fd[0]);
close(fd[1]);
perror("Error");
exit(EXIT_FAILURE);
}
else if (check == 3)
{
close(fd[0]);
close(fd[1]);
write(2, "Error : Permission denied\n", 26);
exit(126);
}
}
void free2d_array(char **arr)
{
int i;
i = 0;
if (!arr)
return ;
while (arr[i])
{
free(arr[i]);
i++;
}
free(arr);
}
int fh1(char *av, int *fds)
{
int fd;
fd = -1;
if (access(av, F_OK) == 0)
fd = open(av, O_RDONLY);
if (fd == -1)
{
close(fds[0]);
close(fds[1]);
perror(av);
exit(EXIT_FAILURE);
}
return (fd);
}
int fh2(char *av, int *fds)
{
int fd;
fd = -1;
fd = open(av, O_WRONLY | O_TRUNC | O_CREAT, 0644);
if (fd == -1)
{
close(fds[0]);
close(fds[1]);
perror(av);
exit(EXIT_FAILURE);
}
return (fd);
}