-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpender_utils.c
95 lines (87 loc) · 2.49 KB
/
expender_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expender_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ulyildiz <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 21:05:33 by ysarac #+# #+# */
/* Updated: 2024/05/21 16:31:11 by ulyildiz ### ########.fr */
/* */
/* ************************************************************************** */
#include "42-libft/libft.h"
#include "functions.h"
#include <stdlib.h>
#include <unistd.h>
static char *expand_dollar_dollar(char *tmp)
{
char *pid_str;
char *new_tmp;
pid_str = ft_itoa(getpid());
if (!pid_str)
return (NULL);
new_tmp = ft_strappend(tmp, pid_str, ft_strlen(pid_str));
free(pid_str);
return (new_tmp);
}
static char *expand_dollar_question(char *tmp)
{
return (ft_strappend(tmp, "0", 1));
}
static char *expand_variable(char *tmp, const char *token_value, size_t *i,
t_env *env)
{
size_t j;
char *new_tmp;
t_env *expnd_value;
j = 0;
(*i)++;
while (ft_isalpha(token_value[*i + j]))
{
j++;
}
new_tmp = ft_strndup(&token_value[*i], j);
expnd_value = find_env(env, new_tmp);
if (expnd_value)
{
tmp = ft_strappend(tmp, expnd_value->value,
ft_strlen(expnd_value->value));
}
free(new_tmp);
*i += j;
return (tmp);
}
char *append_literal(char *tmp, char *token_value, size_t *start, size_t *i)
{
tmp = ft_strappend(tmp, &token_value[*start], *i - *start);
return (tmp);
}
char *handle_dollar_sign(char *tmp, const char *token_value, size_t *i,
t_env *env)
{
if (token_value[*i + 1] == '$')
{
tmp = expand_dollar_dollar(tmp);
*i += 2;
}
else if (token_value[*i + 1] == '?')
{
tmp = expand_dollar_question(tmp);
*i += 2;
}
else if (token_value[*i + 1] == '\0')
{
tmp = ft_strappend(tmp, "$", 1);
(*i)++;
}
else if (ft_isalpha(token_value[*i + 1]))
{
tmp = expand_variable(tmp, token_value, i, env);
}
/* else
{
tmp = ft_strappend(tmp, "$", 1); //bak
(*i)++;
} */
return (tmp);
}