-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmore_builtin.c
More file actions
146 lines (133 loc) · 3.23 KB
/
more_builtin.c
File metadata and controls
146 lines (133 loc) · 3.23 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
#include "shell.h"
/**
* builtins_list - search for match and execute the associate builtin
* @data: struct for the program's data
* Return: Returns the return of the function executed is there is a match,
* otherwise returns -1.
**/
int builtins_list(data_of_program *data)
{
int iterator;
builtins options[] = {
{"exit", builtin_exit},
{"help", builtin_help},
{"cd", builtin_cd},
{"alias", builtin_alias},
{"env", builtin_env},
{"setenv", builtin_set_env},
{"unsetenv", builtin_unset_env},
{NULL, NULL}
};
/*walk through the structure*/
for (iterator = 0; options[iterator].builtin != NULL; iterator++)
{
/*if there is a match between the given command and a builtin,*/
if (str_compare(options[iterator].builtin, data->command_name, 0))
{
/*execute the function, and return the return value of the function*/
return (options[iterator].function(data));
}
/*if there is no match return -1 */
}
return (-1);
}
/**
* builtin_cd - change the current directory
* @data: struct for the program's data
* Return: zero if sucess, or other number if its declared in the arguments
*/
int builtin_cd(data_of_program *data)
{
char *dir_home = env_get_key("HOME", data), *dir_old = NULL;
char old_dir[128] = {0};
int error_code = 0;
if (data->tokens[1])
{
if (str_compare(data->tokens[1], "-", 0))
{
dir_old = env_get_key("OLDPWD", data);
if (dir_old)
error_code = set_work_directory(data, dir_old);
_print(env_get_key("PWD", data));
_print("\n");
return (error_code);
}
else
{
return (set_work_directory(data, data->tokens[1]));
}
}
else
{
if (!dir_home)
dir_home = getcwd(old_dir, 128);
return (set_work_directory(data, dir_home));
}
return (0);
}
/**
* set_work_directory - set the work directory
* @data: struct for the program's data
* @new_dir: path to be set as work directory
* Return: zero if sucess, or other number if its declared in the arguments
*/
int set_work_directory(data_of_program *data, char *new_dir)
{
char old_dir[128] = {0};
int err_code = 0;
getcwd(old_dir, 128);
if (!str_compare(old_dir, new_dir, 0))
{
err_code = chdir(new_dir);
if (err_code == -1)
{
errno = 2;
return (3);
}
env_set_key("PWD", new_dir, data);
}
env_set_key("OLDPWD", old_dir, data);
return (0);
}
/**
* builtin_help - shows the environment where the shell runs
* @data: struct for the program's data
* Return: zero if sucess, or other number if its declared in the arguments
*/
int builtin_help(data_of_program *data)
{
int i, length = 0;
char *mensajes[6] = {NULL};
mensajes[0] = HELP_MSG;
/* validate args */
if (data->tokens[1] == NULL)
{
_print(mensajes[0] + 6);
return (1);
}
if (data->tokens[2] != NULL)
{
errno = E2BIG;
perror(data->command_name);
return (5);
}
mensajes[1] = HELP_EXIT_MSG;
mensajes[2] = HELP_ENV_MSG;
mensajes[3] = HELP_SETENV_MSG;
mensajes[4] = HELP_UNSETENV_MSG;
mensajes[5] = HELP_CD_MSG;
for (i = 0; mensajes[i]; i++)
{
/*print the length of string */
length = str_length(data->tokens[1]);
if (str_compare(data->tokens[1], mensajes[i], length))
{
_print(mensajes[i] + length + 1);
return (1);
}
}
/*if there is no match, print error and return -1 */
errno = EINVAL;
perror(data->command_name);
return (0);
}