-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell_cmds_1.c
134 lines (127 loc) · 3.23 KB
/
shell_cmds_1.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
#include "main.h"
/**
* sc_history - Prints the history of this shell
* @ac: The number of arguments passed
* @av: The arguments passed
*
* Return: The function's exit code
*/
int sc_history(int ac, char *av[])
{
int i, j, max_width, width;
int hist_count;
char **history = get_history(&hist_count);
char *num = NULL, *buf = NULL;
buf = long_to_str(hist_count);
max_width = str_len(buf);
if (buf != NULL)
free(buf);
for (i = 0; i < hist_count; i++)
{
num = long_to_str(i);
width = str_len(num);
write(STDOUT_FILENO, " ", 1);
for (j = 0; j < (max_width - width); j++)
write(STDOUT_FILENO, " ", 1);
write(STDOUT_FILENO, num, width);
write(STDOUT_FILENO, " ", 1);
write(STDOUT_FILENO, *(history + i), str_len(*(history + i)));
write(STDOUT_FILENO, "\n", 1);
if (num != NULL)
free(num);
}
(void)ac;
(void)av;
return (EC_SUCCESS);
}
/**
* sc_setenv - Adds or replaces an environment variable in this shell
* @ac: The number of arguments passed
* @av: The arguments passed
*
* Return: The function's exit code
*/
int sc_setenv(int ac, char *av[])
{
char *buf0 = NULL, *buf1 = NULL;
if (ac > 0)
{
if (is_variable(av[0]))
{
if (ac > 1)
add_env_var(av[0], av[1]);
return (EC_SUCCESS);
}
else
{
buf0 = *((char **)get_shell_prop(EXEC_NAME_ID));
buf1 = long_to_str(*((int *)get_shell_prop(LINE_NUMBER_ID)));
write(STDERR_FILENO, buf0, str_len(buf0));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, buf1, str_len(buf1));
write(STDERR_FILENO, ": setenv: ", 10);
write(STDERR_FILENO, av[0], str_len(av[0]));
write(STDERR_FILENO, ": not a variable\n", 17);
if (buf1 != NULL)
free(buf1);
return (EC_INVALID_ARGS);
}
}
else
{
buf0 = *((char **)get_shell_prop(EXEC_NAME_ID));
buf1 = long_to_str(*((int *)get_shell_prop(LINE_NUMBER_ID)));
write(STDERR_FILENO, buf0, str_len(buf0));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, buf1, str_len(buf1));
write(STDERR_FILENO, ": setenv: Too few arguments.\n", 29);
if (buf1 != NULL)
free(buf1);
return (EC_INVALID_ARGS);
}
}
/**
* sc_unsetenv - Removes an environment variable from this shell
* @ac: The number of arguments passed
* @av: The arguments passed
*
* Return: The function's exit code
*/
int sc_unsetenv(int ac, char *av[])
{
char *buf0 = NULL, *buf1 = NULL;
if (ac > 0)
{
if (get_env_var(av[0]) != NULL)
{
remove_env_var(av[0]);
return (EC_SUCCESS);
}
else
{
buf0 = *((char **)get_shell_prop(EXEC_NAME_ID));
buf1 = long_to_str(*((int *)get_shell_prop(LINE_NUMBER_ID)));
write(STDERR_FILENO, buf0, str_len(buf0));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, buf1, str_len(buf1));
write(STDERR_FILENO, ": unsetenv: ", 12);
write(STDERR_FILENO, av[0], str_len(av[0]));
write(STDERR_FILENO, ": not found\n", 12);
if (buf1 != NULL)
free(buf1);
return (EC_GENERAL_ERROR);
}
}
else
{
buf0 = *((char **)get_shell_prop(EXEC_NAME_ID));
buf1 = long_to_str(*((int *)get_shell_prop(LINE_NUMBER_ID)));
write(STDERR_FILENO, buf0, str_len(buf0));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, buf1, str_len(buf1));
write(STDERR_FILENO, ": unsetenv: Too few arguments.\n", 31);
if (buf1 != NULL)
free(buf1);
return (EC_INVALID_ARGS);
}
}