-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuiltins.c
106 lines (95 loc) · 2.19 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
#include "olaf.h"
/**
* cd_b - Changes the current working directory to the parameter passed to cd.
* if no parameter is passed it will change directory to HOME.
* @line: A string representing the input from the user.
*/
void cd_b(char *line)
{
int index;
int token_count;
char **param_array;
const char *delim = "\n\t ";
token_count = 0;
param_array = token_interface(line, delim, token_count);
if (param_array[0] == NULL)
{
single_free(2, param_array, line);
return;
}
if (param_array[1] == NULL)
{
index = find_path("HOME");
chdir((environ[index]) + 5);
}
else if (_strcmp(param_array[1], "-") == 0)
print_str(param_array[1], 0);
else
chdir(param_array[1]);
double_free(param_array);
}
/**
* env_b - Prints all the environmental variables in the current shell.
* @line: A string representing the input from the user.
*/
void env_b(__attribute__((unused))char *line)
{
int i;
int j;
for (i = 0; environ[i] != NULL; i++)
{
for (j = 0; environ[i][j] != '\0'; j++)
write(STDOUT_FILENO, &environ[i][j], 1);
write(STDOUT_FILENO, "\n", 1);
}
}
/**
* exit_b - Exits the shell. After freeing allocated resources.
* @line: A string representing the input from the user.
*/
void exit_b(char *line)
{
free(line);
print_str("\n", 1);
exit(1);
}
/**
* check_built_ins - Finds the right function needed for execution.
* @str: The name of the function that is needed.
* Return: Upon sucess a pointer to a void function. Otherwise NULL.
*/
void (*check_built_ins(char *str))(char *str)
{
int i;
builtin_t buildin[] = {
{"exit", exit_b},
{"env", env_b},
{"cd", cd_b},
{NULL, NULL}
};
for (i = 0; buildin[i].built != NULL; i++)
{
if (_strcmp(str, buildin[i].built) == 0)
{
return (buildin[i].f);
}
}
return (NULL);
}
/**
* built_in - Checks for builtin functions.
* @command: An array of all the arguments passed to the shell.
* @line: A string representing the input from the user.
* Return: If function is found 0. Otherwise -1.
*/
int built_in(char **command, char *line)
{
void (*build)(char *);
build = check_built_ins(command[0]);
if (build == NULL)
return (-1);
if (_strcmp("exit", command[0]) == 0)
double_free(command);
build(line);
return (0);
}