-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSS_builtin1.c
More file actions
124 lines (104 loc) · 2.28 KB
/
SS_builtin1.c
File metadata and controls
124 lines (104 loc) · 2.28 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
#include <stdio.h>
#include "shell.h"
/**
* _myhistory - this function is responsible for showing the shell cmd history
* @info: function parameter indicating a Struct containing potential arguments
* Return: 0 if successful
*/
int _myhistory(info_t *info)
{
print_list(info->history);
return (0);
}
/**
* unset_alias - this function removes an alias from the list of alias
* @info: function parameter indicating the parameter structure containing argu
* @str: function parameter indicating the string alias to be removed
* Return: 0 if successful
*/
int unset_alias(info_t *info, char *str)
{
char *a;
char b;
int c;
a = _strchr(str, '=');
if (!a)
return (1);
b = *a;
*a = 0;
c = delete_node_at_index(&(info->alias),
get_node_index(info->alias, node_starts_with(info->alias, str, -1)));
*a = b;
return (c);
}
/**
* set_alias - this function sets alias to string
* @info: function parameter indicating a struct containing potential argument
* @str: function parameter indicating the string alias to be set
* Return: Always 0 on success, 1 on error
*/
int set_alias(info_t *info, char *str)
{
char *b;
b = _strchr(str, '=');
if (!b)
return (1);
if (!*++b)
return (unset_alias(info, str));
unset_alias(info, str);
return (add_node_end(&(info->alias), str, 0) == NULL);
}
/**
* print_alias - this function prints an individual alias from the alias list
* @node: parameter indicating the alias node
* Return: 0 if successful
*/
int print_alias(list_t *node)
{
char *d = NULL;
char *c = NULL;
if (node)
{
d = _strchr(node->str, '=');
for (d = node->str; c <= d; c++)
_putchar(*c);
_putchar('\'');
_puts(d + 1);
_puts("'\n");
return (0);
}
return (1);
}
/**
* _myalias - this function mimic the behaivour of the alias builtin command
* @info: function parameter indicating the struct containing potential argum
* Return: 0 if successful
*/
int _myalias(info_t *info)
{
int i;
char *p;
if (info->argc == 1)
{
list_t *node = info->alias;
while (node)
{
print_alias(node);
node = node->next;
}
return (0);
}
for (i = 1; info->argv[i]; i++)
{
p = _strchr(info->argv[i], '=');
if (p)
{
set_alias(info, info->argv[i]);
}
else
{
print_alias(node_starts_with(info->alias, info->argv[i], '='));
}
}
return (0);
}