-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand_utils1.c
More file actions
97 lines (87 loc) · 2.32 KB
/
Copy pathexpand_utils1.c
File metadata and controls
97 lines (87 loc) · 2.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_utils1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yucchen <yucchen@student.42singapore.sg +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/07 12:28:56 by sileow #+# #+# */
/* Updated: 2026/01/11 16:01:13 by yucchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "expand.h"
char *str_append_char(char *s, char c)
{
char *append;
char temp[2];
temp[0] = c;
temp[1] = '\0';
append = ft_strjoin(s, temp);
free(s);
if (!append)
return (NULL);
return (append);
}
char *str_append_str(char *s1, char *s2)
{
char *append;
if (!s2)
return (s1);
append = ft_strjoin(s1, s2);
free(s1);
if (!append)
return (NULL);
return (append);
}
void update_q_status(char c, int *q_status)
{
if (*q_status == 0 && c == '\'')
*q_status = 1;
else if (*q_status == 0 && c == '"')
*q_status = 2;
else if (*q_status == 1 && c == '\'')
*q_status = 0;
else if (*q_status == 2 && c == '"')
*q_status = 0;
else
*q_status += 0;
}
static void expand_status(char **expand, int *i)
{
char *str_status;
str_status = ft_itoa(g_last_status);
if (!str_status)
{
free(*expand);
*expand = NULL;
return ;
}
*expand = str_append_str(*expand, str_status);
free(str_status);
(*i)++;
}
void expand_word(const char *src, char **expand, int *i, t_env *env)
{
int start;
char *key;
(*i)++;
if (src[*i] == '?')
expand_status(expand, i);
else if (src[*i] && (ft_isalpha(src[*i]) || src[*i] == '_'))
{
start = *i;
while (src[*i] && (ft_isalnum(src[*i]) || src[*i] == '_'))
(*i)++;
key = ft_substr(src, start, *i - start);
if (!key)
{
free(*expand);
*expand = NULL;
return ;
}
*expand = str_append_str(*expand, getenv_value(env, key));
free(key);
}
else
*expand = str_append_char(*expand, '$');
}