blob: 358cfc7598268773abb2a148274bcdea71a71d1d (
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
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "builtins.h"
#include "utils/env.h"
extern char **environ;
void printenv(void)
{
size_t i = 0;
while (environ[i] != NULL)
{
printf("export %s\n", environ[i]);
i += 1;
}
}
void export_var(struct string *arg)
{
char *equal = strchr(arg->data, '=');
if (equal != NULL)
{
size_t len = equal - arg->data;
char *var = malloc(len + 1);
memcpy(var, arg->data, len);
char *value = equal + 1;
var[len] = '\0';
env_set(var, value);
free(var);
}
else
{
env_set(arg->data, "");
}
}
int export(struct string **args)
{
if (args[0] == NULL)
{
printenv();
fflush(stdout);
return 0;
}
int flagp = 0;
size_t i = 0;
if (strcmp(args[0]->data, "-p") == 0)
{
flagp = 1;
i += 1;
}
if (args[1] == NULL && flagp)
{
printenv();
fflush(stdout);
return 0;
}
while (args[i] != NULL)
{
export_var(args[i]);
i += 1;
}
fflush(stdout);
return 0;
}
|