Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/parsing/parsing_utils.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* parsing_utils.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/07/26 18:17:08 by yabtaour #+# #+# */
9
/* Updated: 2022/09/29 17:31:59 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../minishell.h"
14
15
char *ft_substr(char *s, int start, size_t len)
16
{
17
char *ptr;
18
unsigned int i;
19
int end;
20
21
i = 0;
22
end = len + start;
23
if (!s)
24
return (NULL);
25
ptr = (char *)malloc((len + 1) * sizeof(char));
26
if (!ptr)
27
exit (1);
28
if (start <= ft_strlen(s))
29
{
30
while (start < end && s[start] != '\0')
31
{
32
ptr[i] = s[start];
33
i++;
34
start++;
35
}
36
}
37
ptr[i] = '\0';
38
return (ptr);
39
}
40
41
void free_split(char **arguments)
42
{
43
int i;
44
45
i = 0;
46
while (arguments[i])
47
{
48
free(arguments[i]);
49
i++;
50
}
51
free(arguments);
52
}
53
54
char *ft_join_args(char *s1, char *s2)
55
{
56
char *result;
57
58
result = NULL;
59
result = ft_strjoin(s1, " ");
60
result = ft_strjoin(result, s2);
61
return (result);
62
}
63
64
char *ft_strjoin(char *s1, char *s2)
65
{
66
int i;
67
int j;
68
char *str;
69
70
if (!s1)
71
{
72
s1 = (char *)malloc(1 * sizeof(char));
73
if (!s1)
74
exit (1);
75
s1[0] = '\0';
76
}
77
if (!s2)
78
return (s1);
79
str = malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
80
if (!str)
81
exit (1);
82
i = -1;
83
j = 0;
84
while (s1 && s1[++i])
85
str[i] = s1[i];
86
while (s2[j])
87
str[i++] = s2[j++];
88
str[ft_strlen(s1) + ft_strlen(s2)] = '\0';
89
free(s1);
90
return (str);
91
}
92
93