forked from zguaidh/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.c
157 lines (145 loc) · 3 KB
/
builtins.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
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
151
152
153
154
155
156
157
#include "main.h"
/**
* cd_req - Changes current working directory to specified argument
*
* @str: voided parameter string
* @env: voided parameter env
* @exec_arg: array of arguments
*
* Return: no return value
*/
void cd_req(char *str, char **env, char **exec_arg)
{
int i = 0;
char *home_dir = getenv("HOME"), *old_dir = getenv("OLDPWD");
char cwd[1024];
(void)str, (void)env;
if (_strcmp(exec_arg[0], "cd") == 0)
{
get_cwd(cwd, sizeof(cwd), "OLDPWD");
if (exec_arg[1] == NULL || _strcmp(exec_arg[1], "") == 0)
{
if (home_dir != NULL)
i = chdir(home_dir);
}
else if (_strcmp(exec_arg[1], "-") == 0)
{
if (old_dir != NULL)
{
i = chdir(old_dir);
_print(old_dir), _print("\n");
}
else
{
i = chdir(getenv("PWD"));
_print(getenv("PWD")), _print("\n");
}
}
else
i = chdir(exec_arg[1]);
if (i != 0)
{
name_of_dst = exec_arg[1];
cd_err();
exit_status = errno;
}
else
{
get_cwd(cwd, sizeof(cwd), "PWD");
}
}
}
/**
*exit_req - exit request function
*@str: a strings that needs to be free
*@env: an array of strings representing environment variables
*@args: Arguments
*
* Return: no return value
*/
void exit_req(char *str, char **env, char **args)
{
(void)env;
(void)args;
free(str);
errno = exit_status;
exit(errno);
}
/**
*env_req - Environment request function
*@str: a pointer to a string (not used)
*@env: an array of strings representing environment variables
*@args: Arguments
*
*Return: no return value
*/
void env_req(char *str, char **env, char **args)
{
int i, j;
(void)str;
(void)args;
for (i = 0; env[i] != NULL; i++)
{
if (env[i] != NULL)
{
j = 0;
while (env[i][j] != '\0')
{
write(1, &env[i][j], 1);
j++;
}
write(1, "\n", 1);
}
}
}
/**
*get_b - searches for a builtin command and returns a function pointer
*to the correspendind built-in function or NULL otherwise
*@s: a string representing the name of the built-in
*@env: an array of strings representing environment variables
*@a: Arguments
*
*Return: no return value
*/
void (*get_b(char *s, char **env, char **a))(char *s, char **env, char **a)
{
builtins builtin[] = {
{"exit", exit_req},
{"env", env_req},
{"cd", cd_req},
{NULL, NULL}
};
int i = 0;
(void)env;
(void)a;
while (builtin[i].built != NULL)
{
if (_strcmp(s, builtin[i].built) == 0)
return (builtin[i].f);
i++;
}
return (NULL);
}
/**
*_build - build and execute function
*@exec_arg: an array of strings representing the command and its arguments
*@buff: a string buffer for a possible usein the built-in function
*@env: an array of strings representing environment variables
*
* Return: no return value
*/
int _build(char **exec_arg, char *buff, char **env)
{
void (*func)(char *, char **, char **);
/* int exit_status; */
func = get_b(exec_arg[0], env, exec_arg);
if (func == NULL)
return (-1);
if (_strcmp("exit", exec_arg[0]) == 0)
{
free_contents(exec_arg);
exit_status = 1;
}
func(buff, env, exec_arg);
return (0);
}