-
Notifications
You must be signed in to change notification settings - Fork 1
/
bui1tin_1_2.c
110 lines (99 loc) · 2.28 KB
/
bui1tin_1_2.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
#include "shell.h"
/**
* _myhistory - a function that displays the command history.
* @info: A pointer to a structure containing information about the program.
* Return: 0 always.
*/
int _myhistory(info_t *info)
{
print_list(info->history);
return (0);
}
/**
* unset_alias - a function that unsets an alias in the shell.
* @info: A pointer to a structure containing information about the program.
* @str: The string representing the alias to unset.
* Return: 1 if the alias is not found, otherwise the result of the
* deletion operation.
*/
int unset_alias(info_t *info, char *str)
{
char *ptr, ch;
int r;
ptr = _strchr(str, '=');
if (!ptr)
return (1);
ch = *ptr;
*ptr = 0;
r = delete_node_at_index(&(info->alias),
get_node_index(info->alias, node_starts_with(info->alias, str, -1)));
*ptr = ch;
return (r);
}
/**
* set_alias - a function that sets an alias in the shell.
* @info: A pointer to a structure containing information about the program.
* @str: The string representing the alias to set.
* Return: 1 if the alias cannot be set, 0 otherwise.
*/
int set_alias(info_t *info, char *str)
{
char *ptr;
ptr = _strchr(str, '=');
if (!ptr)
return (1);
if (!*++ptr)
return (unset_alias(info, str));
unset_alias(info, str);
return (add_node_end(&(info->alias), str, 0) == NULL);
}
/**
* print_alias - a function that prints an alias.
* @node: A pointer to the linked list node containing alias information.
* Return: 0 if the node is not NULL, 1 otherwise.
*/
int print_alias(list_t *node)
{
char *ptr = NULL, *x = NULL;
if (node)
{
ptr = _strchr(node->str, '=');
for (x = node->str; x <= ptr; x++)
_putchar(*x);
_putchar('\'');
_puts(ptr + 1);
_puts("'\n");
return (0);
}
return (1);
}
/**
* _myalias - a function that handles the 'alias' command in the shell.
* @info: A pointer to a structure containing information about the program.
* Return: 0 always.
*/
int _myalias(info_t *info)
{
int x = 0;
char *ptr = NULL;
list_t *nd = NULL;
if (info->argc == 1)
{
nd = info->alias;
while (nd)
{
print_alias(nd);
nd = nd->next;
}
return (0);
}
for (x = 1; info->argv[x]; x++)
{
ptr = _strchr(info->argv[x], '=');
if (ptr)
set_alias(info, info->argv[x]);
else
print_alias(node_starts_with(info->alias, info->argv[x], '='));
}
return (0);
}