-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand_single_quotes.c
89 lines (82 loc) · 2.26 KB
/
expand_single_quotes.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_single_quotes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emohamed <emohamed@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/29 10:58:11 by emohamed #+# #+# */
/* Updated: 2023/09/30 21:33:48 by emohamed ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <stdlib.h>
int count_single_quotes(char **tokens)
{
int count;
int i;
count = 0;
i = 0;
while (tokens[i])
{
if (ft_strchr(tokens[i], '\''))
{
count++;
}
i++;
}
return (count);
}
char *remove_single_quotes(const char *token)
{
int token_length;
char *modified_token;
int k;
int l;
token_length = ft_strlen(token);
modified_token = malloc_((token_length * sizeof(char)), NULL, 0, NULL);
if (modified_token == NULL)
{
return (NULL);
}
k = 0;
l = 0;
while (l < token_length)
{
if (token[l] != '\'')
{
modified_token[k] = token[l];
k++;
}
l++;
}
modified_token[k] = '\0';
return (modified_token);
}
char **expand_s_quotes(char **tokens)
{
t_sq sq;
sq.num_quotes = count_single_quotes(tokens);
sq.total_tokens = 0;
sq.i = -1;
while (tokens[++sq.i])
sq.total_tokens++;
sq.expanded_tokens = malloc_(((sq.total_tokens + sq.num_quotes + 1)
* sizeof(char *)), NULL, 0, NULL);
if (sq.expanded_tokens == NULL)
return (NULL);
sq.i = -1;
sq.j = 0;
while (tokens[++sq.i])
{
sq.current_token = tokens[sq.i];
if (ft_strchr(sq.current_token, '\''))
sq.expanded_tokens[sq.j] = remove_single_quotes(sq.current_token);
else
sq.expanded_tokens[sq.j] = ft_strdup(sq.current_token);
if (sq.expanded_tokens[sq.j] == NULL)
return (NULL);
sq.j++;
}
return ((sq.expanded_tokens[sq.j] = NULL), sq.expanded_tokens);
}