Repository navigation
Expand file tree
/
Copy pathenv.c
More file actions
79 lines (73 loc) · 1.97 KB
/
Copy pathenv.c
File metadata and controls
79 lines (73 loc) · 1.97 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* env.c :+: :+: */
/* +:+ */
/* By: sde-rijk <sde-rijk@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/12/13 10:16:48 by sde-rijk #+# #+# */
/* Updated: 2022/01/19 13:48:50 by sde-rijk ######## odam.nl */
/* */
/* ************************************************************************** */
#include "Libft/libft.h"
#include <stdio.h>
int ft_env(char **envp)
{
while (*envp)
{
if (ft_strchr(*envp, '='))
{
ft_putstr_fd(*envp, 1);
ft_putchar_fd('\n', 1);
}
envp++;
}
return (0);
}
void print_env_export(char *env)
{
int len_before_equals;
len_before_equals = 0;
while (env[len_before_equals] && env[len_before_equals] != '=')
len_before_equals++;
if (env[len_before_equals] == '=')
len_before_equals++;
ft_putstr_fd("declare -x ", 1);
write(1, env, len_before_equals);
if (!ft_strchr(env, '='))
{
ft_putchar_fd('\n', 1);
return ;
}
ft_putchar_fd('"', 1);
write(1, env + len_before_equals, ft_strlen(env) - len_before_equals);
ft_putchar_fd('"', 1);
ft_putchar_fd('\n', 1);
}
int ft_export_print(char **envp)
{
char *first;
char *last;
int i;
if (!*envp)
return (0);
i = 0;
first = NULL;
while (1)
{
last = first;
first = NULL;
i = 0;
while (envp[i])
{
if ((!first || (ft_strcmp(envp[i], first) < 0))
&& (!last || ft_strcmp(envp[i], last) > 0))
first = envp[i];
i++;
}
if (!first)
return (0);
else
print_env_export(first);
}
}