-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_onlyexport.c
More file actions
94 lines (86 loc) · 2.2 KB
/
ft_onlyexport.c
File metadata and controls
94 lines (86 loc) · 2.2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_onlyexport.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: makbulut <makbulut@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/15 19:10:09 by makbulut #+# #+# */
/* Updated: 2022/09/05 13:52:20 by makbulut ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "minishell.h"
#include "42-Libft/libft.h"
static void sort(char **arr)
{
char *tmp;
int i;
int j;
i = 0;
while (arr[i])
{
j = i + 1;
while (arr[j])
{
if (arr[i][0] > arr[j][0])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
j++;
}
i++;
}
}
static char *double_quotes(char *s)
{
char *buffer;
int len;
len = ft_strlen(s);
if (!len)
return (ft_strdup("\"\""));
buffer = ft_calloc(len + 3, sizeof(char));
buffer[0] = '\"';
ft_memmove(s, buffer + 1, len);
buffer[len + 1] = '\"';
free(s);
return (buffer);
}
static void __onlyexport(char ***env, t_command *command, int i)
{
sort(*env);
i = -1;
while ((*env)[++i])
{
ft_putstr_fd("declare -x", command->out);
ft_putendl_fd((*env)[i], command->out);
}
ft_freearr_str(*env);
}
void ft_onlyexport(t_command *command)
{
int i;
char *key;
char *value;
char **env;
env = ft_calloc(sizeof(char *), ft_arrlen((void **)g_mini->env) + 1);
i = -1;
while (g_mini->env[++i])
{
if (ft_strchr(g_mini->env[i], '='))
{
key = ft_get_key(g_mini->env[i]);
value = ft_get_value(g_mini->env[i]);
value = double_quotes(value);
ft_strappend(&key, "=");
ft_strappend(&key, value);
free(value);
env[i] = key;
}
else
env[i] = ft_strdup(g_mini->env[i]);
}
__onlyexport(&env, command, -1);
}