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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* s_init_next.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <dirent.h>
#include <errno.h>
#include <libft.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "b_export_next.h"
#include "f_fail.h"
#include "s_destroy.h"
#include "s_struct.h"
#include "u_vars.h"
#include "u_vars_next.h"
char s_inc_shlvl(t_msh *msh)
{
char fmt[8];
char tmp[255];
char str[255];
int shlvl;
u_get_var_value(tmp, "$SHLVL", 255, msh);
if (tmp[0] == C_NUL)
{
ft_memcpy(fmt, "SHLVL=1", 8);
b_export_with_equals(fmt, msh);
}
else
{
shlvl = ft_atoi(tmp);
if (shlvl >= 999)
f_shlvl_too_high(shlvl);
shlvl = (shlvl >= 999) ? 0 : shlvl;
shlvl = (shlvl < 0) ? 0 : shlvl + 1;
ft_itoa_s(str, shlvl);
if (u_subst_var_value("$SHLVL", str, msh) == 1)
{
return (1);
}
}
return (0);
}
char **s_dupenv_del(char **nenvp, unsigned long i)
{
while (i > 0)
{
ft_memdel((void*)&nenvp[i]);
i--;
}
ft_memdel((void*)&nenvp);
return (NULL);
}
static char s_cpy_path(char **ptr, char *nenvp[], size_t *i)
{
char tmp[255];
if (*ptr != NULL)
return (0);
if (*ptr == NULL)
{
ft_sprintf(tmp, "PATH=%s", _PATH_STDPATH);
if ((nenvp[*i] = ft_strdup(tmp)) == NULL)
return (1);
*(i) += 1;
}
return (0);
}
char **s_dupenv(char *const envp[])
{
size_t i;
char **nenvp;
char **ptr;
ptr = (char**)envp;
while (*ptr != NULL && ft_strncmp(*ptr, "PATH=", 5) != 0)
ptr++;
i = 0;
while (envp[i] != NULL)
i++;
i += (*ptr == NULL) ? (1) : (0);
if ((nenvp = (char**)malloc((i + 1) * sizeof(char*))) == NULL)
return (NULL);
i = 0;
while (envp[i] != NULL)
{
if ((nenvp[i] = ft_strdup(envp[i])) == NULL)
return (s_dupenv_del(nenvp, i));
i++;
}
if (s_cpy_path(ptr, nenvp, &i) == 1)
return (s_dupenv_del(nenvp, i));
nenvp[i] = NULL;
return (nenvp);
}
void s_set_cwd(char cwd[], t_msh *msh)
{
char fmt[PATH_MAX];
DIR *dir;
u_get_var_value(cwd, "$PWD", PATH_MAX, msh);
if (cwd[0] == C_NUL)
{
if (getcwd(cwd, PATH_MAX) != NULL)
{
ft_sprintf(fmt, "%s=%s", "PWD", cwd);
b_export_with_equals(fmt, msh);
}
else
ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno));
return ;
}
if ((dir = opendir(cwd)) != NULL)
closedir(dir);
else if (errno == ENOENT)
{
if (getcwd(cwd, PATH_MAX) != NULL)
{
ft_sprintf(fmt, "%s=%s", "PWD", cwd);
b_export_with_equals(fmt, msh);
}
else
ft_dprintf(STDERR_FILENO, "minishell: %s\n", strerror(errno));
return ;
}
}
|