blob: 900833ad5f4d67b19566e76e77e1a65eb783d817 (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/01 17:11:34 by charles #+# #+# */
/* Updated: 2020/10/09 12:50:37 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
/*
** \file export.c
** \brief `export` builtin
*/
#include "minishell.h"
/*
** \brief Put an environment variable in the format
** "declare -x id=value" of bash
** \param s Full variable (id=value)
*/
static void st_put_declare_x(char *s)
{
char *equal_ptr;
if (s == NULL)
return ;
if ((equal_ptr = ft_strchr(s, '=')) == NULL)
equal_ptr = ft_strchr(s, '\0');
*equal_ptr = '\0';
if (ft_strcmp(s, "_") == 0)
return ;
ft_putstr("declare -x ");
ft_putstr(s);
ft_putchar('=');
ft_putchar('"');
while (*++equal_ptr != '\0')
{
if (*equal_ptr == '"')
ft_putchar('\\');
ft_putchar(*equal_ptr);
}
ft_putchar('"');
ft_putchar('\n');
}
/*
** \brief Garbage function for norm complience
*/
int st_declare_x_iter(t_env env)
{
ft_veciter(env, (void (*)(void*))st_put_declare_x);
return (0);
}
/*
** \brief Export variables to the environment
** \param argv arguments
** \param env environment
** \return a status code or EVAL_FATAL on fatal error
*/
int builtin_export(char **argv, t_env env)
{
int status;
size_t i;
char *equal_ptr;
if (argv[1] == NULL)
return (st_declare_x_iter(env));
status = 0;
i = 0;
while (argv[++i] != NULL)
{
if ((equal_ptr = ft_strchr(argv[i], '=')) != NULL)
*equal_ptr = '\0';
if (*argv[i] == '\0'
|| env_key_len(argv[i], false) != ft_strlen(argv[i]))
{
if (equal_ptr != NULL)
*equal_ptr = '=';
errorf("export: `%s': not a valid identifier\n", argv[i]);
status = 1;
}
else if (equal_ptr != NULL
&& env_export(env, argv[i], equal_ptr + 1) == NULL)
return (EVAL_FATAL);
}
return (status);
}
|