-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecute_built_ins1.c
122 lines (117 loc) · 2.64 KB
/
execute_built_ins1.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "main.h"
/**
* execute_setenv - function that executes the setenv command
* @vector: array of command and its argument
* @envlist: pointer to the environment variables linked list
* Return: integer
*/
int execute_setenv(char **vector, envlist_t **envlist)
{
int i, j, set;
char *s = "Invalid number of arguments\n";
j = _strlen(s);
for (i = 0; vector[i]; i++)
;
if (i != 3)
{
write(STDERR_FILENO, s, j);
return (0);
}
set = _setenv(vector[1], vector[2], envlist);
if (set == -1)
{
s = "setenv encountered an error\n";
j = _strlen(s);
write(STDERR_FILENO, s, j);
return (0);
}
return (1);
}
/**
* execute_env - function that executes the env command
* @vector: array of command and its argument
* @envlist: pointer to the environment variables linked list
* Return: integer
*/
int execute_env(char **vector, envlist_t **envlist)
{
int i, j;
char *s = "Invalid number of arguments";
j = _strlen(s);
for (i = 0; vector[i]; i++)
;
if (i != 1)
{
write(STDERR_FILENO, s, j);
return (0);
}
printenv(*envlist);
return (1);
}
/**
* execute_unsetenv - function that executes the unsetenv command
* @vector: array of command and its argument
* @envlist: pointer to the environment variables linked list
* Return: integer
*/
int execute_unsetenv(char **vector, envlist_t **envlist)
{
int i, j, unset;
char *s = "Invalid number of arguments\n";
j = _strlen(s);
for (i = 0; vector[i]; i++)
;
if (i != 2)
{
write(STDERR_FILENO, s, j);
return (0);
}
unset = _unsetenv(vector[1], envlist);
if (unset == -1)
{
s = "unsetenv encountered an error\n";
j = _strlen(s);
write(STDERR_FILENO, s, j);
return (0);
}
return (1);
}
/**
* execute_exit - function that executes the exit command
* @vector: array of command and its argument
* @envlist: pointer to the environment variables linked list
* Return: integer
*/
int execute_exit(char **vector, __attribute__((unused))envlist_t **envlist)
{
int i, j, k = 0, *exit_status = &k;
char *s = "Invalid number of arguments\n";
j = _strlen(s);
for (i = 0; vector[i]; i++)
;
if (i != 1 && i != 2)
{
write(STDERR_FILENO, s, j);
return (0);
}
exit_status = (i == 2) ? str_to_integer(vector[1], exit_status) : &k;
if (!exit_status)
return (-1);
return (*exit_status);
}
/**
* execute_cd - function that executes the cd command
* @vector: array of command and its argument
* @envlist: pointer to the environment variables linked list
* Return: integer
*/
int execute_cd(char **vector, envlist_t **envlist)
{
int i, k;
for (i = 0; vector[i]; i++)
;
k = (i > 1) ? cd(vector[1], envlist) : cd(vector[0], envlist);
if (k == -1)
return (1);
return (0);
}