-
Notifications
You must be signed in to change notification settings - Fork 0
/
built_ins_env.c
98 lines (85 loc) · 1.7 KB
/
built_ins_env.c
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
#include "shell.h"
/**
* built_in_env - shows the environment where the shell runs
*
* @data: struct for the program's data
*
* Return: zero if sucess, or other number if its declared in the arguments
*/
int built_in_env(data_of_program *data)
{
int i;
char copy_name[50] = {'\0'};
char *var_copy = NULL;
if (data->tokens[1] == NULL)
print_environ(data);
else
{
for (i = 0; data->tokens[1][i]; i++)
{
if (data->tokens[1][i] == '=')
{
var_copy = str_duplicate(env_get_key(copy_name, data));
if (var_copy != NULL)
env_set_key(copy_name, data->tokens[1] + i + 1, data);
print_environ(data);
if (env_get_key(copy_name, data) == NULL)
{
_print(data->tokens[1]);
_print("\n");
}
else
{
env_set_key(copy_name, var_copy, data);
free(var_copy);
}
return (0);
}
copy_name[i] = data->tokens[1][i];
}
errno = 2;
perror(data->command_name);
errno = 127;
}
return (0);
}
/**
* built_in_set_env - ............
*
* @data: struct for the program's data
*
* Return: 0 if sucess, or other ..........
*/
int built_in_set_env(data_of_program *data)
{
if (data->tokens[1] == NULL || data->tokens[2] == NULL)
return (0);
if (data->tokens[3] != NULL)
{
errno = E2BIG;
perror(data->command_name);
return (5);
}
env_set_key(data->tokens[1], data->tokens[2], data);
return (0);
}
/**
* built_in_unset_env - ...............
*
* @data: struct for the program's data'
*
* Return: ..
*/
int built_in_unset_env(data_of_program *data)
{
if (data->tokens[1] == NULL)
return (0);
if (data->tokens[2] != NULL)
{
errno = E2BIG;
perror(data->command_name);
return (5);
}
env_remove_key(data->tokens[1], data);
return (0);
}