-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_export_utils2.c
More file actions
executable file
·120 lines (109 loc) · 3.01 KB
/
builtin_export_utils2.c
File metadata and controls
executable file
·120 lines (109 loc) · 3.01 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_export_utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyeyeom <hyeyeom@42student.gyeongsan.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 01:49:16 by hyeyeom #+# #+# */
/* Updated: 2025/04/14 00:38:35 by hyeyeom ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int check_whole_name(char *name, int before_equal_sign)
{
int valid;
int first;
int i;
first = ft_isalnum(*name);
if (!(first == 1 || first == 2 || *name == '_'))
return (-1);
i = 0;
while (i < before_equal_sign)
{
valid = ft_isalnum(name[i]);
if (valid == 0 && name[i] != '_' && name[i] != '=')
return (-1);
i++;
}
return (0);
}
int is_valid_name(char *name)
{
int flag;
int i;
int valid;
i = 0;
flag = 0;
while (name[i])
{
if (name[i] == '=')
{
flag = 1;
break ;
}
i++;
}
valid = check_whole_name(name, i);
if (valid == -1)
return (-1);
if (flag == 1 && name[(i + 1)] != '\0')
return (2);
if (flag == 0 && name[i] == '\0')
return (0);
return (1);
}
int process_envp_update(t_env_context *ctx, t_minish *sh)
{
char *new_cmd;
if (ctx->is_name == -1)
return (-1);
if (ctx->is_name == 2 || ctx->is_name == 0 || \
*(ctx->cmd + 1) == NULL)
update_or_add_envps(sh, *(ctx->cmd));
else if (ctx->is_name == 1)
{
new_cmd = ft_strjoin(*(ctx->cmd), *(ctx->cmd + 1));
update_or_add_envps(sh, new_cmd);
free(new_cmd);
return (2);
}
return (0);
}
int is_valid_value(t_env_context *ctx, size_t *index_stack, t_minish *sh)
{
char *find_in_o_cmd;
int length_original_cmds;
int res;
length_original_cmds = ft_strlen(ctx->original_cmd);
find_in_o_cmd = ft_strnstr(&(ctx->original_cmd[*index_stack]), \
*(ctx->cmd), length_original_cmds);
ctx->index = (find_in_o_cmd - ctx->original_cmd);
(*index_stack) = ctx->index + 1;
res = process_envp_update(ctx, sh);
return (res);
}
void export_update_or_add(char **cmds, char *original_cmd, t_minish *sh)
{
int is_name;
int is_value;
size_t index_stack;
char **current_cmd;
t_env_context ctx;
current_cmd = cmds;
index_stack = ft_strlen(*current_cmd);
current_cmd++;
while (*current_cmd && **current_cmd != '\0')
{
is_name = is_valid_name(*current_cmd);
ctx.cmd = current_cmd;
ctx.original_cmd = original_cmd;
ctx.is_name = is_name;
is_value = is_valid_value(&ctx, &index_stack, sh);
if (is_value == 2)
current_cmd++;
else if (is_value == -1)
export_err_msg(*current_cmd);
current_cmd++;
}
}