-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
99 lines (90 loc) · 2.53 KB
/
Copy pathutils.c
File metadata and controls
99 lines (90 loc) · 2.53 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: kamanfo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/13 13:22:41 by kamanfo #+# #+# */
/* Updated: 2021/12/13 13:22:43 by kamanfo ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
size_t ft_strlen(const char *s)
{
int len;
len = 0;
while (s[len] != '\0')
len++;
return (len);
}
char *ft_strjoin(char const *s1, char const *s2)
{
int idx;
int idx_join;
char *join;
join = malloc((ft_strlen((char *)s1) + ft_strlen((char *)s2) + 1));
if (!s1 || !s2 || !join)
return (NULL);
idx = 0;
idx_join = 0;
while (s1[idx])
join[idx_join++] = s1[idx++];
idx = 0;
while (s2[idx])
join[idx_join++] = s2[idx++];
join[idx_join] = '\0';
return (join);
}
char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t j;
j = 0;
if (!needle[j])
return ((char *)haystack);
i = 0;
while (haystack[i] && i < len)
{
j = 0;
while (haystack[i + j] && (i + j < len) && needle[j]
&& haystack[i + j] == needle[j])
j++;
if (needle[j] == '\0')
return ((char *)(haystack + i));
i++;
}
return (NULL);
}
int permission_error(char *filename)
{
write(2, "pipex: ", 7);
write(2, "permission denied: ", 19);
write(2, filename, ft_strlen(filename));
write(2, "\n", 1);
return (-1);
}
int openfile(char *filename, int mode)
{
if (mode == 0)
{
if (access(filename, F_OK) == -1)
{
write(2, "pipex: ", 7);
write(2, "No such file or directory: ", 28);
write(2, filename, ft_strlen(filename));
write(2, "\n", 1);
return (-1);
}
else if (access(filename, R_OK) == -1)
return (permission_error(filename));
return (open(filename, O_RDONLY));
}
else
{
if (access(filename, F_OK) == 0 && access(filename, W_OK) == -1)
return (permission_error(filename));
return (open(filename, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH));
}
}