-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuilt.c
More file actions
150 lines (125 loc) · 3.26 KB
/
built.c
File metadata and controls
150 lines (125 loc) · 3.26 KB
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
#include "main.h"
/**
* get_built - checks if input is a built-in command and if so
* executes its associated function
* @input_list: list representing input command
* @shell_name: name of shell program
* @env_list: list of environment variables
*
* Return: integer, -1 if built-in does not exist
*/
int get_built(list_t *input_list, char *shell_name, list_t *env_list)
{
char *name;
int i;
built_s built_ins[] = {
{"exit", exit_shell},
{"env", env_func},
{"setenv", setenv_func},
{"unsetenv", unsetenv_func},
{NULL, NULL}
};
name = input_list->name;
for (i = 0; built_ins[i].name; i++)
{
if (_strcmp(name, built_ins[i].name) == 0)
return (built_ins[i].f(input_list, shell_name, &env_list));
}
return (-1);
}
/**
* exit_shell - implements the exit built-in functionality
* @input_list: list of command and arguments
* @shell_name: name of shell program
* @env_list_ptr: pointer to list of environment variables
*
* Return: exit code. Otherwise -2
*/
int exit_shell(list_t *input_list, char *shell_name, list_t **env_list_ptr)
{
int num;
char *error_message[4];
error_message_init(error_message, shell_name, input_list->name);
if (input_list->next == NULL)
return (0);
num = _atoi(input_list->next->name);
if (num == -1)
{
error_message[2] = "Illegal number";
error_message[3] = input_list->next->name;
print_error(error_message);
return (-2);
}
(void)env_list_ptr;
return (num);
}
/**
* env_func - implements the env built-in functionality
* @input_list: list of command and arguments
* @shell_name: name of shell program
* @env_list_ptr: pointer list of environment variables
*
* Return: -2
*/
int env_func(list_t *input_list, char *shell_name, list_t **env_list_ptr)
{
print_env(*env_list_ptr);
(void)input_list;
(void)shell_name;
return (-2);
}
/**
* setenv_func - implements the setenv built-in functionality
* @input_list: list of command and arguments
* @shell_name: name of shell program
* @env_list_ptr: pointer list of environment variables
*
* Return: -2
*/
int setenv_func(list_t *input_list, char *shell_name, list_t **env_list_ptr)
{
char *name, *value;
char *error_message[4];
error_message_init(error_message, shell_name, input_list->name);
if (input_list->next == NULL || input_list->next->next == NULL)
{
error_message[2] = "invalid number of arguments";
print_error(error_message);
return (-2);
}
name = input_list->next->name;
value = input_list->next->next->name;
if (_setenv(*env_list_ptr, name, value, 1) == -1)
{
error_message[2] = "error";
print_error(error_message);
}
return (-2);
}
/**
* unsetenv_func - implements the unsetenv built-in functionality
* @input_list: list of command and arguments
* @shell_name: name of shell program
* @env_list_ptr: pointer list of environment variables
*
* Return: -2
*/
int unsetenv_func(list_t *input_list, char *shell_name, list_t **env_list_ptr)
{
char *name;
char *error_message[4];
error_message_init(error_message, shell_name, input_list->name);
if (input_list->next == NULL)
{
error_message[2] = "invalid number of arguments";
print_error(error_message);
return (-2);
}
name = input_list->next->name;
if (_unsetenv(*env_list_ptr, name) == -1)
{
error_message[2] = "error";
print_error(error_message);
}
return (-2);
}