-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
108 lines (97 loc) · 2.43 KB
/
Copy pathutils.c
File metadata and controls
108 lines (97 loc) · 2.43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: isel-azz <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/11 07:52:21 by isel-azz #+# #+# */
/* Updated: 2024/05/11 07:52:22 by isel-azz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
char *ft_strjoin(char const *s1, char const *s2)
{
char *res;
int len;
if (!s1 && !s2)
return (NULL);
if (!s1)
return (ft_strdup(s2));
if (!s2)
return (ft_strdup(s1));
len = ft_strlen(s1) + ft_strlen(s2) + 2;
res = (char *)malloc(len * sizeof(char));
if (!res)
return (NULL);
ft_strlcpy(res, s1, ft_strlen(s1) + 2);
ft_strlcpy(res + ft_strlen(s1), s2, ft_strlen(s2) + 2);
free((char *)s1);
return (res);
}
int ft_strlen(const char *s)
{
int i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}
char *ft_strdup(const char *s1)
{
char *dst;
int i;
i = 0;
dst = (char *)malloc(ft_strlen(s1) + 2);
if (!dst)
return (NULL);
while (s1[i])
{
dst[i] = s1[i];
i++;
}
dst[i] = ' ';
dst[++i] = '\0';
return (dst);
}
int ft_strlcpy(char *dst, const char *src, int dstsize)
{
char *s;
int i;
s = (char *)src;
i = 0;
if (dstsize < 1)
return (ft_strlen(src));
while (s[i] && dstsize - 1)
{
dst[i] = s[i];
i++;
dstsize--;
}
dst[i] = ' ';
dst[++i] = '\0';
return (ft_strlen(src));
}
int ft_atoi(const char *str, char **numbers)
{
unsigned long long result;
int i;
int sign;
int ret;
sign = 1;
i = 0;
result = 0;
while (str[i] && ((str[i] >= 9 && str[i] <= 13) || str[i] == 32))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
result = result * 10 + str[i++] - 48;
if ((result > 2147483648 && sign == -1) || (result > INT_MAX && sign == 1))
free_numbers(numbers, 1);
return (ret = result, ret * sign);
}