-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell_cmds_alias.c
71 lines (68 loc) · 1.47 KB
/
shell_cmds_alias.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
#include "main.h"
/**
* sc_alias - Creates an alias for a command string, prints all the \
* defined aliases or the value of a single alias
* @ac: The number of arguments passed
* @av: The arguments passed
*
* Return: The function's exit code
*/
int sc_alias(int ac, char *av[])
{
int i, n, exit_code = EC_SUCCESS;
alias_t **aliases = (ac == 0 ? get_aliases(&n) : NULL);
char *name = NULL, *value = NULL;
if (ac <= 0)
{
for (i = 0; i < n; i++)
print_alias((*(aliases + i))->name, (*(aliases + i))->value);
}
else
{
for (i = 0; i < ac; i++)
{
if (is_alias_name(av[i]))
{
print_alias(av[i], get_alias_value(av[i]));
if (!is_alias(av[i]))
exit_code = EC_GENERAL_ERROR;
}
else if (is_alias_assignment(av[i], &name, &value))
{
add_alias(name, value);
if (name != NULL)
free(name);
if (value != NULL)
free(value);
name = NULL;
value = NULL;
}
else
{
exit_code = EC_GENERAL_ERROR;
}
}
}
return (exit_code);
}
/**
* print_alias - Prints the name and value of an alias
* @name: The name of the alias
* @value: The value of the alias
*/
void print_alias(char *name, char *value)
{
if (value != NULL)
{
write(STDOUT_FILENO, name, str_len(name));
write(STDOUT_FILENO, "='", 2);
write(STDOUT_FILENO, value, str_len(value));
write(STDOUT_FILENO, "'\n", 2);
}
else
{
write(STDERR_FILENO, "alias: ", 7);
write(STDERR_FILENO, name, str_len(name));
write(STDERR_FILENO, " not found\n", 11);
}
}