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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins_part_one.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */
/* Updated: 2024/09/04 17:52:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
int echo(char **av)//more or less done
{
int i;
int f;
i = 1;
f = 1;
if (av[1][0] == '\0')
{
write(1, "\n", 1);
return (1);
}
if (ft_strcmp(av[1], "-n"))
{
i++;
f = 0;
}
while (av[i])
{
write(1, &av[1], ft_strlen(av[i]));
i++;
}
if (f)
write(1, "\n", 1);
return (0);
}
int pwd(t_env **env, char *av) //done
{
t_env *current;
t_env *prev;
char *tmp;
current = env;
prev = NULL;
while (current)
{
if(ft_strcmp(current->name, av == 0))
break ;
prev = current;
current = current->next;
}
ft_printf("%s\n", current->value);
return (0);
}
int env(t_env **env)// here i need str env oder ich print es alles zusammen xD
{
t_env *current;
t_env *prev;
char *tmp;
current = env;
prev = NULL;
while (current)
{
ft_printf("%s\n", current->name);
prev = current;
current = current->next;
}
return (0);
}
int cd(t_env **env,char **av) // here also need to do the same again for envstrarr. dont like it
{
if(av[1] == NULL)
{
while (env[i] != NULL) // home command
{
if (ft_strncmp(env[i], "HOME=", 5) == 0)
break ;
i++;
}
// before chdir we need to update old pwd
// getcwd(cwd, sizeof(cwd));
// str = ft_strjoin("PWD= or old pwd=", cwd);
// env[i] = str;
if(chdir(env[i] + 5) == -1)
return;
}
else
//update old pwd
//
while (env[i] != NULL) // home command
{
if (ft_strncmp(env[i], "old PWD=", 5) == 0)
break ;
i++;
}
str = ft_strjoin("old pwd=", cwd);
env[i] = str;
//
chdir(av[1] == -1);
return ;
// udpate pwd
while (env[i] != NULL) // home command
{
if (ft_strncmp(env[i], "PWD=", 5) == 0)
break ;
i++;
}
str = ft_strjoin("PWD=", cwd);
env[i] = str;
}
// exit
int exit(char *av)
{
freenode free toke free sequence stop repl free env
clear history
}
//joar
//export
int export(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it
{
char *tmp;
if(tmp = ft_strchr(av[1], '='))
{
tmp = '\0';
t_env *current;
current = *env;
while(current)
{
if (strcmp(current->name, av[1]) == 0)
{
free(current->value);
current->value = ft_strdup(tmp + 1);
break;
}
current = current->next;
}
if (!current)
{
current = malloc(sizeof(t_env));
current->name = ft_strdup(av[1]);
current->value = ft_strdup(tmp + 1);
current->next = *env;
*env = current;
}
return (0);
}
else
syntax return 1;
}
//unset
//for unset as well check and for name part
int unset(char **av, t_env **env) // here also need to do the same again for envstrarr. dont like it
{
t_env *current;
t_env *prev;
char *tmp;
current = env;
prev = NULL;
tmp = ft_strchr(av[1], '=');
tmp = '\0';
while (current)
{
if(ft_strcmp(current->name, av[1] == 0))
break ;
prev = current;
current = current->next;
}
if(current)
{
if(prev)
prev->next = current->next;
else
*env = current->next
free(t_env);//need function for this
}
return (0);
}
void getenvlst(t_env **env, char **en)// seperated name and value
{
char *tmp;
int i;
t_env *current;
i = 0;
while (en[i] != NULL)
{
tmp = ft_strchr(en[i], '=');
tmp = '\0';
current = *env;
current = malloc(sizeof(t_env));
current->name = ft_strdup(en[i]);
current->value = ft_strdup(tmp + 1);
current->next = *env;
*env = current;
i++;
}
return (0);
}
void getenvlststr(t_env **env, char **en)//without serparation
{
int i;
size_t t;
t_env *current;
t = 0;
i = 0;
while (en[i] != NULL)
{
current = *env;
current = malloc(sizeof(t_env));
while(en[t] != '=')
t++;
current->name = ft_substr(en[i], 0, t);
current->value = ft_strdup(en[i]);
current->next = *env;
*env = current;
i++;
}
return (0);
}
//should name the list different
|