-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins_more.c
142 lines (142 loc) · 3.25 KB
/
builtins_more.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
#include "shell.h"
/**
* builtin_exit - exit of the program with the status
* @data: struct for the program's data
* Return: zero if sucess, or other number if its declared in the arguments
*/
int builtin_exit(data_of_program *data)
{
int i;
if (data->tokens[1] != NULL)
{/*if exists arg for exit, check if is a number*/
for (i = 0; data->tokens[1][i]; i++)
if ((data->tokens[1][i] < '0' || data->tokens[1][i] > '9')
&& data->tokens[1][i] != '+')
{/*if is not a number*/
errno = 2;
return (2);
}
errno = _atoi(data->tokens[1]);
}
free_all_data(data);
exit(errno);
}
/**
* 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++)
{
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);
}
/**
* builtin_alias - add, remove or show aliases
* @data: struct for the program's data
* Return: zero if sucess, or other number if its declared in the arguments
*/
int builtin_alias(data_of_program *data)
{
int i = 0;
/* if there are no arguments, print all environment */
if (data->tokens[1] == NULL)
return (print_alias(data, NULL));
while (data->tokens[++i])
{/* if there are arguments, set or print each env variable*/
if (count_characters(data->tokens[i], "="))
set_alias(data->tokens[i], data);
else
print_alias(data, data->tokens[i]);
}
return (0);
}