-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils2.c
110 lines (99 loc) · 2.1 KB
/
utils2.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
104
105
106
107
108
109
110
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhalid <akhalid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/25 03:14:41 by akhalid #+# #+# */
/* Updated: 2022/03/01 16:30:03 by akhalid ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_strlen(char *s)
{
int i;
i = 0;
if (s)
while (s[i])
i++;
return (i);
}
char *ft_strdup(char *s1)
{
char *dup;
int i;
i = 0;
if (!s1)
return (0);
while (s1[i])
i++;
dup = (char *)malloc(i + 1);
if (dup == NULL)
return (NULL);
i = 0;
while (s1[i] != '\0')
{
dup[i] = s1[i];
i++;
}
dup[i] = '\0';
return (dup);
}
char *ft_substr(char *s, int start, size_t len)
{
char *ss;
char *sub;
int i;
if (s && start > ft_strlen(s))
len = 0;
ss = (char *)s;
if (ss)
{
sub = (char *)malloc(sizeof(char) * (len + 1));
if (!sub)
return (0);
i = 0;
while (len--)
sub[i++] = ss[start++];
sub[i] = '\0';
return (sub);
}
return (0);
}
char *ft_strjoin(char *s1, char *s2)
{
char *str;
int i;
int j;
str = (char *)malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
if (!s1)
return (s2);
if (!s2)
return (s1);
if (str == NULL)
return (NULL);
i = -1;
while (s1[++i])
str[i] = s1[i];
j = -1;
while (s2[++j])
{
str[i] = s2[j];
i++;
}
str[i] = '\0';
free(s1);
free(s2);
return (str);
}
int ft_strcmp(char *s1, char *s2)
{
int i;
i = 0;
if (!s1 || !s2)
return (1);
while (s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0')
i++;
return (s1[i] - s2[i]);
}