Repository navigation
Expand file tree
/
Copy pathbuilt_ins.c
More file actions
130 lines (125 loc) · 2.48 KB
/
Copy pathbuilt_ins.c
File metadata and controls
130 lines (125 loc) · 2.48 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
#include "shell.h"
/**
* built_ins - checks if command is a built-in
* @input: tokenized line from the command line
* @env_head: pointer to environ list
* Return: 0 on success, 1 on error, -1 if not found
*/
int built_ins(char **input, list_t **env_head)
{
builtin_t builtins[] = {
{"exit", exit_bi},
{"env", print_env},
{"setenv", set_env},
{"unsetenv", unset_env},
{NULL, NULL}
};
int i, retval;
list_t *tmp;
tmp = *env_head;
if (!input)
{
perror("built_ins");
return (1);
}
i = 0;
while ((builtins + i)->bi != NULL)
{
if (_strcmp(input[0], (builtins + i)->bi) == 0)
{
if (_strcmp(input[0], END) == 0)
{
retval = (builtins + i)->f(input);
return (retval);
}
retval = (builtins + i)->f(input, &tmp);
return (retval);
}
i++;
}
return (-1);
}
/**
* exit_bi - exits shell with given status
* @line: tokenized line from command line
* Return: 0 on success, 1 on error
*/
int exit_bi(char **line)
{
int size, status;
size = arr_size(line);
if (size > 2)
{
_strprint("Failed: Command syntax: exit status\n");
return (1);
}
if (size == 1)
return (0);
if (size == 2)
{
status = _atoi(line[1]);
return (status);
}
return (1);
}
/**
* print_env - prints current environ
* @line: tokenized line from command line
* @env_head: pointer to environ list
* Return: 0 on success, 1 on error
*/
int print_env(char **line, list_t **env_head)
{
int size;
list_t *tmp;
tmp = *env_head;
size = arr_size(line);
if (size > 1)
{
_strprint("Failed: Command syntax: env\n");
return (1);
}
print_list(tmp);
return (0);
}
/**
* set_env - initializes a new environment variable, or modify an existing one
* @line: tokenized line from command line
* @env_head: pointer to environ list
* Return: 0 on success, 1 on error
*/
int set_env(char **line, list_t **env_head)
{
int size;
int retval;
list_t *tmp;
tmp = *env_head;
size = arr_size(line);
if (size != 3)
{
_strprint("Failed: Command syntax: setenv VARIABLE VALUE\n");
return (1);
}
retval = _setenv(line[1], line[2], &tmp);
return (retval);
}
/**
* unset_env - removes environment variable
* @line: tokenized line from command line
* @env_head: pointer to environ list
* Return: 0 on success, 1 on error
*/
int unset_env(char **line, list_t **env_head)
{
int size, retval;
list_t *tmp;
tmp = *env_head;
size = arr_size(line);
if (size != 2)
{
_strprint("Failed: Command syntax: unsetenv VARIABLE\n");
return (1);
}
retval = _unsetenv(line[1], &tmp);
return (retval);
}