-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltin_export.c
91 lines (81 loc) · 2.2 KB
/
builtin_export.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/02 13:56:00 by phelebra #+# #+# */
/* Updated: 2023/08/11 13:41:05 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
extern char **environ;
t_env *create_env_node(char *key, char *value)
{
t_env *new_node;
if (value == NULL)
value = strdup(" ");
new_node = (t_env *)malloc(sizeof(t_env));
if (new_node == NULL)
{
fprintf(stderr, "export: allocation error\n");
return (NULL);
}
new_node->key = strdup(key);
new_node->value = strdup(value);
new_node->next = NULL;
return (new_node);
}
t_env *find_env_key(t_env *env, char *key)
{
t_env *temp;
temp = env;
while (temp != NULL)
{
if (strcmp(temp->key, key) == 0)
return (temp);
temp = temp->next;
}
return (NULL);
}
void add_env_node(t_env **env, t_env *new_node)
{
t_env *last;
if (*env == NULL)
*env = new_node;
else
{
last = *env;
while (last->next != NULL)
last = last->next;
last->next = new_node;
}
}
int builtin_export(char **args, t_env **env)
{
char *key;
char *value;
t_env *temp;
t_env *new_node;
if (args[1] == NULL)
{
fprintf(stderr, "export: expected argument\n");
return (1);
}
key = strtok(args[1], "=");
value = strtok(NULL, "=");
if (value == NULL)
value = strdup(" ");
temp = find_env_key(*env, key);
if (temp != NULL)
{
free(temp->value);
temp->value = strdup(value);
return (1);
}
new_node = create_env_node(key, value);
if (new_node == NULL)
return (1);
return (add_env_node(env, new_node), 1);
}